Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: why casting int to String is not possible through brackets method

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default why casting int to String is not possible through brackets method

    when I try to cast int to String through this way, the compiler tells me they are inconvertible types , I know that there are other ways for holding int value as a String, but why not casting

    [FONT="Courier New"]int num;
    String hold;
     
    num = 14;
     
    hold = (String) num; // doesn't work !![/FONT]


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: why casting int to String is not possible through brackets method

    The simple answer is because Sun (now Sun/Oracle) decided not to implement this type of casting

    Casting (either implicit or explicit) can only take place along the inheritance hierarchy. As Integers (the wrapper class for int's) don't inherit from Strings at any point along their heirarchy, they can't be casted to Strings.

    Integers do inherit from Object, so you can do something like this:

    int a = 5;
    Object o = (Object) a; // no compile or runtime errors!

    As a side note, Sun did decide to implement some pretty funny operator overloading of the + operator for Strings, which calls the toString() method on both operands and concatenates the two strings together.

    Integer num = 14;
    String myString = "" + num;
    // same as;
    String myString = "".toString().concat(num.toString());

    There is also some pretty interesting handling of primitives to conform with how casting of primitives operates on C/C++ and other C-based languages.

    int value = 0xFF;
    byte myByte = (byte) value;
    System.out.println(myByte);

    This will result in the value -1 (since by taking the 8 LSB's you get the byte value 11111111, which in two's compliments notation for signed values is -1)
    Last edited by helloworld922; May 1st, 2010 at 09:30 PM.

  3. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    JavaPF (May 1st, 2010), voltaire (May 2nd, 2010)

  4. #3

    Default Re: why casting int to String is not possible through brackets method

    String.valueOf(num)

  5. The Following User Says Thank You to scribbleno1 For This Useful Post:

    voltaire (May 2nd, 2010)

Similar Threads

  1. passing string into method
    By tabutcher in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 08:43 AM
  2. reading string input then casting it to an int?
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: March 27th, 2010, 11:49 PM
  3. string method
    By dstha in forum Object Oriented Programming
    Replies: 3
    Last Post: February 25th, 2010, 08:07 PM
  4. Replies: 1
    Last Post: February 4th, 2010, 04:23 PM
  5. how to reuse method returned string ??
    By zeeshanmirza in forum Collections and Generics
    Replies: 7
    Last Post: September 4th, 2009, 03:54 PM