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: Type casting error in Java

  1. #1
    Junior Member
    Join Date
    Jul 2008
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Type casting error in Java

    I am learning java (or should I say, filling in the holes) from a reputable learning source (say no more) it said, if fact I am going to feed it back to source I made my purchase from, they said

    int myInt;
    long myLong = 123987654321L;
    myInt = (int) (myLong);
    saying that the number might be chopped

    I thought why not give it a go, to find myInt comes out to be -566397263
    Well one thing I learnt, never trust down type casting.
    Last edited by Eric; December 7th, 2008 at 01:09 PM. Reason: in red added


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: type casting fibs

    Quote Originally Posted by Eric View Post
    Well one thing I learnt, never trust down type casting.
    Downcasting should only be done where it can be trusted - generally only at system boundaries where objects are being generated and introduced to the system.

    But casting a long to an int is not downcasting, it is a 'casting conversion' (specifically a narrowing primitive conversion). Downcasting is when you cast a superclass type to a subclass type.

    A narrowing primitive conversion is converting a larger primitive numeric type to a smaller one. For full details, see Narrowing Primitive Conversions.

  3. #3
    Junior Member
    Join Date
    Jul 2008
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: type casting fibs

    Now I am not sure why I posted this thread?

    I suppose the best idea might be to test to see if that Long variable could fit down into a Int, and if it is outside the range of -2147483648 to 2147483647 print off something like "done but with error!"

  4. #4
    Junior Member
    Join Date
    Jul 2008
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: type casting fibs


Similar Threads

  1. Typecasting of double variable to integer
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 03:41 AM
  2. Java error "java.lang.StackOverflowError"
    By sanatkumar in forum Exceptions
    Replies: 2
    Last Post: July 7th, 2009, 02:40 PM
  3. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 03:18 PM