• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference between int and Integer

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Priya Jothi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arul and Priya, Are you guys testing/working on the same version of Java?
 
Arulkumar Gopalan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am using 1.5.0_04
 
Priya Jothi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have jdk of version 1.4.2_02.

Regards,
Priya.
 
Priya Jothi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic