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 4 of 4

Thread: Explicit Conversion?

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Explicit Conversion?

    the book that im reading says
    "" + <number>
    << "this is the easy way to convert a numerical value to STRING"?

    assuiming that the variable number is an integer.

    is it what they called explicit conversion?
    Last edited by chronoz13; November 11th, 2009 at 05:17 AM.


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

    Default Re: Explicit Conversion?

    No, that's an implicit conversion (technically, it's not a cast at all in this case, see the reason below). An explicit conversion is one you specifically tell Java to do:

    int a = 5;
    String b = (String) a; // explicit

    Note: because of the funny implementation of Strings in Java, this cast here will fail (the compiler will complain).

    Reason: Java normally doesn't allow operator overloading. However, the + has been over-ridden for Strings to mean concatenation. The String concatenation method is able to take any primitive data type and convert it to a string equivalent value. However, because String is not a primitive data type, you cannot explicitly tell Java you want to cast an int to a String and vise versa.
    Last edited by helloworld922; November 11th, 2009 at 10:22 AM.

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Explicit Conversion?

    oh sorry thats what im trying to imply, i just misinterpret the difference between "implicit" and " explicit" conversion..

    yah yah, thats what i want to ask.. "implicit" haha,, ,

    i thought this was "explicit"
    [CODE] "" + number[\CODE]
    so this is the "implicit"

    anyway, so thats how the java manipulate strings, and tnx for the knowledge about "concatenating", every time i concatenate a primitive type i thougt that it still have value (i mean its data type) as it is when you declared it..

    like the code above, i thought that the variable "number" is still the data type as when you declared it , but when you concatenate it with strings. it will become string too(and that is "implicit") hehe, thought it was "explicit" ,

    i just understood it reversely..

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

    Default Re: Explicit Conversion?

    concatenation doesn't really apply to primitives... It means "adding to the end of".

    So:
    "Hello" + " world"
    is the same as
    "Hello".concat(" world");

Similar Threads

  1. numerical conversion methods..
    By chronoz13 in forum Java SE APIs
    Replies: 12
    Last Post: September 27th, 2009, 04:29 AM
  2. binary conversion..
    By chronoz13 in forum Java Theory & Questions
    Replies: 8
    Last Post: September 16th, 2009, 10:47 AM