File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Integer variable" Watch "Integer variable" New topic

Integer variable

Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65

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,
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15080

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

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Vidhya Ramaswamy
Ranch Hand

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

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%)
I agree. Here's the link:
subject: Integer variable
It's not a secret anymore!