• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Integer variable

 
Vidhya Ramaswamy
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 284
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because of autoboxing, one of the new features of Java 5.
 
Vidhya Ramaswamy
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies

Vidhya
 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic