Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Integer vs int

unable to find a concise difference between the two. My knowledge right now is that we need to use Integer when dealing with generics and we need to use Integer if we want a null value. Is there any other reason?

3 Answers

int is a primitive. Integer is an object.

A primitive data type uses a small amount of memory to represent a single item of data. For example, primitive type int represents integers using 32 bits. There are only eight primitive data types in Java:

  1. byte
  2. char
  3. boolean
  4. short
  5. int
  6. long
  7. float
  8. double

Make it stick: java 8 primitives (Be Careful, Bears Shouldn't Ingest Large Furry Dogs).

Object on the other hand is a large chunk of memory that can potentially contain a great deal of data along with methods.

so for example if u say :

Integer anIntegerObject = new Integer(2); // an integer object with the value of 2

// because anIntegerObject is of type "Integer" you can call methods in the Integer Class http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html on it.

System.out.println(anIntegerObject.MAX_VALUE); // returns the maximum value an int can take = 2147483647 System.out.println(anIntegerObject.MIN_VALUE); // returns the minimum value an int can take = -2147483648 System.out.println(anIntegerObject.floatValue()); // returns the float of 2 which is = 2.0;

now if u do this :

int intPrimitive = 10;

and try to call methods in the Integer Class api on it. u will get an error

System.out.println(intPrimitive.MAX_VALUE); // Oh! Oh! can't do this.

why? the intPrimitive is like a remote control without any buttons (methods) on it!

thank you this helped. so basically we use Integer if we want to call methods on a number. What are some of the most common methods that are used with Integer? Also I know that there are reasons to use Integer that are not method related. for example in generics it's always "< Integer >" and never "< int >" and if we want null values we would always use Integer. Are there any other common cases?

Luksy,

U are awesome !!!!

Please stay here in the forum and add more of your very detailed answers. They are very good to understand (If you are not an absolute java/programming beginner)

Grigorij

Hey Kevin,

Integer, like Luksy already said is an object and it is also a wrapper class. The wrapper class gives primitive types methods, they can invoke (because primitives are not objects - you can't invoke methods on them). All primitive types have there wrapper classes

PRIMITIV WRAPPER boolean Boolean
byte Byte
char Character
int Integer
float Float
double Double
long Long
short Short

You see the differenece. The wrapper is uppercased and you write the whole word.

 int x = 10;
//so x is a primitive data type without methods like byteValue(), intValue() etc.

Integer someIntegerObject = new Integer(10);
//but someIntegerObject is a object with all funny methods you need to play with numbers and more

You can´t code:

System.out.println(x. byteValue());
//the compiler can´t find any method for x, but you can code

System.out.println(someIntegerObject. byteValue());
//the compiler knows that someIntegerObject is an object with method byteValue() and uses it

See this link for more information:

http://www.w3resource.com/java-tutorial/java-wrapper-classes.php

Grigorij

using Integer: wanting to call methods on a number, when using generics, and wanting null values.

thats my understanding as of now. correct me if im wrong or if theres something more to add.

I am very unfamiliar with generics, cause I am a beginner myself.

But you need generics to avoid compile time type errors (the compiler proofs that correct Type is used in correct place and there should not be any ClassCastException. For example HashSet of String will only contain String object and if you try to put Integer or any other object, compiler will complain. So the code can´t pass the compile check and there will be less runtime errorse( baaaaaaad ).

When u care so much about performance, be stingy with object creation!

pls refer to this link http://stackoverflow.com/questions/239560/when-should-i-use-primitives-instead-of-wrapping-objects

sorry i dont understand what u said