Results 1 to 3 of 3
  1. #1
    peterhabe is offline Member
    Join Date
    Jul 2010
    Posts
    43
    Rep Power
    0

    Default Converting from long to int

    At the moment i'm converting the longs to strings then to ints using String.valueOf() and Integer.parseInt(), but is there a better way of doing this using just one moethod?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    Cast. Either way, note that the number of bytes each primitive has and the consequences should the long value be beyond the extremes of the max and min values of an int

    Java Code:
    long value = 1000l;
    int valAsInt = (int)value;
    
    long value2 = System.currentTimeMillis();
    int value2AsInt = (int)value2;//print value2 against value2AsInt to see the behavior

  3. #3
    peterhabe is offline Member
    Join Date
    Jul 2010
    Posts
    43
    Rep Power
    0

Similar Threads

  1. Declaring a long
    By gbtimmon in forum New To Java
    Replies: 3
    Last Post: 01-13-2011, 06:20 PM
  2. Long
    By maya700 in forum New To Java
    Replies: 1
    Last Post: 08-31-2010, 02:49 PM
  3. bigger than long
    By scorp200 in forum New To Java
    Replies: 3
    Last Post: 12-06-2009, 07:17 AM
  4. Long Cannot Be Dereferenced?
    By caps_lock in forum New To Java
    Replies: 1
    Last Post: 01-18-2009, 01:49 AM
  5. stumpt. converting an object to a long int.
    By root_demon in forum New To Java
    Replies: 7
    Last Post: 10-31-2008, 05:32 AM

Posting Permissions

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