Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Casting a Long to Integer

Posted on 2010-09-24
15
Medium Priority
?
620 Views
Last Modified: 2012-05-10
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
Comment
Question by:directxBOB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +3
15 Comments
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 800 total points
ID: 33753440
Long lnVar =  new Long(intValue);
0
 

Author Comment

by:directxBOB
ID: 33753497
Sorry should have posted an example

Integer freeMemory = Runtime.getRuntime().freeMemory();
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 33753527
Integer freeMemory = Runtime.getRuntime().freeMemory();

is not correct, since it already returns long
0
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!

 
LVL 40

Assisted Solution

by:Gurvinder Pal Singh
Gurvinder Pal Singh earned 800 total points
ID: 33753534
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
 

Author Comment

by:directxBOB
ID: 33753543
yes I'm trying to

assign Integer a Long Value
0
 

Author Comment

by:directxBOB
ID: 33753550
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
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 33753573
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
 
LVL 3

Assisted Solution

by:rgeers
rgeers earned 400 total points
ID: 33753601
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
 
LVL 10

Assisted Solution

by:Hegemon
Hegemon earned 400 total points
ID: 33753629
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
 
LVL 3

Expert Comment

by:rgeers
ID: 33753641
echange the myLongValue for freeMemory, well guess you understood this
0
 
LVL 10

Expert Comment

by:Hegemon
ID: 33753647
Sorry, the declaration should begin with

long longValue = ...
0
 
LVL 3

Expert Comment

by:rgeers
ID: 33754618
Is your question about casting a Long to an int or about storing into a map?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33755693
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
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 33757976
you cannout cast it, you'll need to create a new Long

Integer i = new Integer(5);
Long l = new Long(i.longValue());
0
 

Author Closing Comment

by:directxBOB
ID: 33838928
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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question