This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between int and Integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between int and Integer" Watch "Difference between int and Integer" New topic
Author

Difference between int and Integer

Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
Hi All,
have a look at this



//1 works fine but //2 gives a compiler error.

What is the difference between the two lines of code ? When should I use //1 and when should I use //2 ?


[ August 10, 2005: Message edited by: Sherry Jacob ]

<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
Kayalvizhi Umashankar
Greenhorn

Joined: Aug 04, 2005
Posts: 25
In Java,

- int is primitive data type
- Integer is a Wrapper Class

Hence I don't think you can convert the return type of Math.random() which is double(primitive type) to an Object reference.

Please correct me if i am wrong

Kayal
Priya Jothi
Ranch Hand

Joined: Jul 13, 2004
Posts: 168
Hi Sherry,

What Kayalvizhi said is partially correct.But i would like to add one more important point.

You try to understand Arrays concept better.Array is the one where you can store collection of different items of the same data type.Now look at ur code.It tries to store items of two different data types(one is int(primitive type) & the other is Integer(Wrapper type)).Juz think..how this could be possible?.



That X array should be declared either as int[] or Integer[] but cant be both.As already said u can cast double to an int but not to an Integer.Integer is an object..what sense it makes converting primitive to an object?.

Hope this is clear & it helps you!!.

Revert if u have any more queries on this.

Regards,
Priya.
Arulkumar Gopalan
Ranch Hand

Joined: Oct 13, 2003
Posts: 104

What Kayalvizhi said is partially correct.But i would like to add one more important point.

Kayal�s response is perfect for the original question posted.

You try to understand Arrays concept better.Array is the one where you can store collection of different items of the same data type.Now look at ur code.It tries to store items of two different data types(one is int(primitive type) & the other is Integer(Wrapper type)).Juz think..how this could be possible?.

This is possible. Look at the code below.

That X array should be declared either as int[] or Integer[] but cant be both.

This is possible. Look at the code below.

As already said u can cast double to an int but not to an Integer. Integer is an object..

Yes.

what sense it makes converting primitive to an object?.

Conversion can be done as below, if there is a requirement to do so.


For original poster:
Data type casting - with in primitive data types
Object casting � with in objects


Anbudan & Mahalo,<br />Arul<br /> <br />-Not a sun certified Java professional :-)
Priya Jothi
Ranch Hand

Joined: Jul 13, 2004
Posts: 168
Hi Arul,

I agreed with what Kayal said.But i've just added some more points & explained the same.Thatz it!.

But I don't understand what u r trying to explain thru ur program.
It is throwing compile time errors in all the places as expected.
In the pgm posted initially is using single array named x to store values of different data types which is not possible.All these only i've explained in my prev post.

If possible could you elaborate ur answer & explain us?.

Regards,
Priya.
Arulkumar Gopalan
Ranch Hand

Joined: Oct 13, 2003
Posts: 104
Priya,

The code provided explains which you said somethings are not possible.
- code has two different type of arrays (Integer and int)
- Integer array accepts the int value
- int array accepts the Integer value

Please read your explanation throughly and check the code provided below. For your understanding, I have given the println methods also. Execute the code and let me know if you have any questions.

Sachin Ahuja
Greenhorn

Joined: Aug 12, 2005
Posts: 13
Arul and Priya, Are you guys testing/working on the same version of Java?
Arulkumar Gopalan
Ranch Hand

Joined: Oct 13, 2003
Posts: 104
Am using 1.5.0_04
Priya Jothi
Ranch Hand

Joined: Jul 13, 2004
Posts: 168
I have jdk of version 1.4.2_02.

Regards,
Priya.
Priya Jothi
Ranch Hand

Joined: Jul 13, 2004
Posts: 168
Hi Arul,

I feel there might be version problem also.I don't know much about 1.5 version.I've learnt abt autoboxing concept in java 1.5(it is not there in 1.4) which converts primitive to an object & vice versa.Try the same program in 1.4 if u have & let us know if it works.

Regards,
Priya.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Difference between int and Integer