Long vs. long and int vs. Integer

Can someone tell me specific differences of these and when to use which?

Which data type is most easily converted to other datatypes, and which can passed more efficently?

If nothing else, help find out where I can find the differences.

Thanks.

[269 byte] By [ntsd2a] at [2007-11-27 11:52:02]
# 1

byte, char, short, int, long, float, double, and boolean are primitives. They are not objects. They do not have methods or member variable fields. They're simply data.

Byte, Character, Short, Integer, Long, Float, Double, and Boolean are wrapper classes for those primitives.

There are places where an object is required--for instance, adding an element to a collection. You can't use an int there, so you use an Integer.

Primitives use less memory than objects, are faster to work with (though often not noticeably so, both in space and in time), and can be operated on with +, -, /, *, etc. where objects cannot, so they're simpler to use for straight mathematical operations.

Autoboxing/unboxing (introduced in 1.5) makes some of the conversions automatic and transparent to you.

http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html

jverda at 2007-7-29 18:41:55 > top of Java-index,Java Essentials,New To Java...
# 2

> Can someone tell me specific differences of these and

> when to use which?

int is a primitive

Integer is a class/object

As for when to use which - that's like asking, "when should I use a nail and when should I use a screw?" That's entirely up to your design and whatever seems more appropriate at the time.

Personally, I only ever use the Integer class for it's static methods, but - again - that's just me.

>

> Which data type is most easily converted to other

> datatypes, and which can passed more efficently?

>

Define convert. (You may also want to read up on AutoBoxing.)

> If nothing else, help find out where I can find the

> differences.

>

Google.

> Thanks.

You're welcome.

Navy_Codera at 2007-7-29 18:41:55 > top of Java-index,Java Essentials,New To Java...
# 3

Autoboxing takes care of most things. The main difference with respect to programming is that you can cast the primitive types to one another, but not the wrapper classes. So if you're using wrapper classes make sure your data types are proper.

so

int i=1;

long l=i;

Integer x=i;

Long y =l;

is fine but

Integer i=1;

Long l=i;

is not, if you try explicit cast, you'll get a ClassCastException.

saugatakca at 2007-7-29 18:41:55 > top of Java-index,Java Essentials,New To Java...
# 4

Basically, use primitive types when you can, and transform them when you need to work with objects.

Boxing/unboxing (which did not exist in older versions of Java) let you pass automatically from one kind to another. Look at that:

ArrayList<Integer> arr = new ArrayList<Integer>();// stores Integer objects

int i = 42;// an int

arr.add(new Integer(i));// you would have needed to wrap the int...

arr.add(i);// ...but boxing lets you also do this, and it's perfectly legal

java_knighta at 2007-7-29 18:41:55 > top of Java-index,Java Essentials,New To Java...