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 Casting?

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Explicit Casting?

    SITUATION:

    Reading my text book and it says that implicit casting will work if the result of a casting fits into the target variable. If it doesn't I'm told to use explicit tasking.

    EXAMPLE (IMPLICIT)

    byte b = 'a';
    int i = 'a';

    EXAMPLE (EXPLICIT)

    byte b = (byte)'\uFFF4';

    QUESTION:

    Data types have a range. Therefore, if a value is not within this range how on earth can it be assigned explicitly at all? I just do not understand this completely.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Explicit Casting?

    Range and size can be (usually are?) related. The type byte allows values from -128 to 127. 'a' = 97 and 'z' = 122 so both will fit in a byte variable using implicit casting. Try to implicitly cast the char = 128 into a type byte, and you'll get an error. Explicitly cast char = 128 int a byte, and what do you get?

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Explicit Casting?

    It tells me there is a loss of precision. So is this the same as rounding???




    Screen Shot 2013-07-14 at 4.39.19 PM copy.jpg
    Last edited by syregnar86; July 14th, 2013 at 05:42 PM. Reason: info

  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 Casting?

    It's not quite the same as rounding, though it is somewhat related because you just can't represent some values in a "smaller" type.

    The fact is modern computers represent numbers in binary. A "smaller" type simply has less bits. According to the 5.1.3 of the Java Language Specifications, down-casting for integral types (byte, char, short, int, long) takes the n least significant bits, where n is the number of bits in the target value.

    So:

    // this is hexedecimal so I don't have to write 32-bit values in binary :P
    int(0x12345678) = byte(0x78)
    int(0x12345678) = short(0x5678)

    In addition to this, Java uses two's compliment to represent signed types.

    So even though int(0x7FFF) = byte(0xFF), the integral value of the int is 32767, and the integral value of the byte is -1!

    In Java you must make an explicit case from int to byte because of this. The reasoning is that this behavior is usually not expected (in what world does 32767 even come close to equal -1?), but it is very important to be able to make this case because the int value may not necessarily be out of range (int(1) = byte(1)), and there are many useful things you can do using bit-fields and bit-twiddling (mucking with the bits), though I have qualms with using Java with bit-fields and bit-twiddling.

Similar Threads

  1. Casting Theory
    By BigDru in forum Java Theory & Questions
    Replies: 1
    Last Post: October 25th, 2012, 08:21 AM
  2. Dymaic Casting of List
    By jaypee81 in forum Collections and Generics
    Replies: 1
    Last Post: February 23rd, 2012, 04:46 PM
  3. Class Casting
    By Sana1990 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 9th, 2012, 08:13 AM
  4. Explicit Conversion?
    By chronoz13 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 11th, 2009, 11:40 PM
  5. Type casting error in Java
    By Eric in forum Java Theory & Questions
    Replies: 3
    Last Post: December 13th, 2008, 04:11 PM

Tags for this Thread