wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Integer variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Integer variable" Watch "Integer variable" New topic
Author

Integer variable

Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
Hi,

I am preparing for SCJP 1.5 exam.

I have a query: The following lines of code compiles fine.

int i1 = 2000;
Integer var = i1;

I was expecting it should have been : Integer var = new Integer(i1)

Please explain.
Thanks in advance,
Vidhya
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
Vidhya,
You would be using Java 5. So in Java 5 it automatically wraps the int literal i1 into the wrapper class(Integer here) object(autoboxing). Whereas if you try this in Java 4 it will throw an error unless you give new to it.
Thanks.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

This is because of autoboxing, one of the new features of Java 5.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
Thanks for your replies

Vidhya
Manikandan Jayaraman
Ranch Hand

Joined: Sep 15, 2004
Posts: 230
Though this looks programmer-friendly, I would say that it is conceptually wrong.

Future generation of programmers might not get the TRUE ESSENCE of Objects and Primitives and what they actually mean.

For that matter I also heard that Java 8 or 9 targets on XML to write java programs. This also makes java executions faster it seems.

All targets now on programmer's convenience.


Regards,<br />Mani<br />SCJP 1.4 (95%)<br />SCWCD 1.4 (94%)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Integer variable
 
Similar Threads
Strange Behaviour Of Wrapper Class
Immutable classes in java
not clear with hashcodes
Changing int into object
Strange Behaviour Of Wrapper Class