Results 1 to 6 of 6

Thread: about String..~

  1. #1
    SuperPig is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default about String..~

    about genetics ..~

    Java Code:
    public class Box{
      private Object object;
      
      public void add( Object object ){
        this.object = object;
      }
    
      public Object get(){
        return object;
      }
    Java Code:
    public class BoxDemo{
      public static void main(String[] args ){
        Box integerBox = new Box();
        integerBox.add("10");
        
        Integer someInteger = (Integer)integerBox.get();
        System.out.println(someInteger);
      }
    }
    According to Introduction (The Java™ Tutorials > Learning the Java Language > Generics) , there are error because of cast exception due to java.lang.String cannot be cast to java.lang.Integer .

    i really cant understand why "10" cant be converted into 10 ..~

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    really cant understand why "10" cant be converted into 10
    It can be done very easily. Look at the Integer class for a method to do that.
    However the compiler is a pretty simple program and is not going to try to figure out what you want to do. You must tell it explicitly with the correct code.
    For the case of converting an int to an Integer (called boxing) the compiler will do it for you.

  3. #3
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    "10" is a String type that contains two UNICODE characters, the number 10 is a numeric type of value 10. Java has been designed so that conversions between different types are not automatic, but must be explicitly requested. This reduces the number of coding errors and makes programs more robust.

  4. #4
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    about genetics ..~
    I take everything literally. That makes me stupid.

    i really cant understand why "10" cant be converted into 10 ..~
    The computer takes everything literally. That makes it stupid. That's where programmers come in...
    No bug ever had to calculate its fitnessfunction.

  5. #5
    SuperPig is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default

    o..~ i tot the compiler will automatically convert "10" to 10 for me..~
    i just need to change [ integerBox.add("10") ] become [ integerBox.add(Integer.parseInt("10")) ] ?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Your code will generate an int which the compiler will convert to an Integer.
    You could just do this and create the Integer directly yourself:
    integerBox.add(new Integer(10))
    or the compiler could do that for you if you coded:
    integerBox.add(10);

Similar Threads

  1. Replies: 12
    Last Post: 11-26-2010, 08:33 AM
  2. Replies: 1
    Last Post: 11-04-2010, 06:53 PM
  3. Replies: 7
    Last Post: 11-03-2009, 02:18 AM
  4. Replies: 5
    Last Post: 01-11-2009, 10:12 AM
  5. Replies: 0
    Last Post: 11-20-2007, 04:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •