• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 623
  • Last Modified:

Casting a Long to Integer

I know that Long (int) isn't of type object. So perhaps cast is the wrong term to use.

But is there anyway to take an Object Integer and assign it a value Long?

Cheers
0
directxBOB
Asked:
directxBOB
  • 4
  • 4
  • 3
  • +3
5 Solutions
 
Gurvinder Pal SinghCommented:
Long lnVar =  new Long(intValue);
0
 
directxBOBAuthor Commented:
Sorry should have posted an example

Integer freeMemory = Runtime.getRuntime().freeMemory();
0
 
Gurvinder Pal SinghCommented:
Integer freeMemory = Runtime.getRuntime().freeMemory();

is not correct, since it already returns long
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Gurvinder Pal SinghCommented:
it should be Long only

public class IntTOLong
{
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            Long lnVar = Runtime.getRuntime().freeMemory();
      }
}


see this test program

let me know if i missed on something
0
 
directxBOBAuthor Commented:
yes I'm trying to

assign Integer a Long Value
0
 
directxBOBAuthor Commented:
The reason for using Integer is because I need to store these within a MAP and you cannot store an int or long in an MAP as it's not of type object.
0
 
Gurvinder Pal SinghCommented:
But it is already returning the Long only, so why int is required. Did you checked the program i shared.

Long can be put into map

0
 
rgeersCommented:
Long freeMemory = Runtime.getRuntime().freeMemory();
int InVar = int myIntValue = unchecked((int)myLongValue);

It'll throw OverflowException in checked context if the value doesn't fit in an int
If you want to handle this you could check against int.MaxValue.
0
 
HegemonCommented:
int and long - (lowercase) - are primitives, that's why you can't store them in a map.
Integer and Long (uppercase) are classes wrapping these primitives.
You can put them into your map instead, i.e.

long longvalue = .... (get your long value);

myMap.put ("key", new Long (longValue));
0
 
rgeersCommented:
echange the myLongValue for freeMemory, well guess you understood this
0
 
HegemonCommented:
Sorry, the declaration should begin with

long longValue = ...
0
 
rgeersCommented:
Is your question about casting a Long to an int or about storing into a map?
0
 
CEHJCommented:
There's no problem about Map
Map<Long, String> m = new HashMap<Long, String>();
m.put(Runtime.getRuntime().freeMemory(), "foo");


Map<String, Long> m = new HashMap<String, Long>();
m.put("foo", Runtime.getRuntime().freeMemory());

Open in new window

0
 
objectsCommented:
you cannout cast it, you'll need to create a new Long

Integer i = new Integer(5);
Long l = new Long(i.longValue());
0
 
directxBOBAuthor Commented:
Thanks, the problem turned out that I was trying to put a primitive in the Map. So by using Integer and Long got around the problem. Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now