• Post Reply Bookmark Topic Watch Topic
  • New Topic

How does this compile?  RSS feed

 
Richard Hayward
Ranch Hand
Posts: 187
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I don't see what's going on here that allows the following line to compile.

Integer y = 123;

y is an Integer wrapper object, but there is no constructor. I thought I'd have to say something like

Integer y = new Integer(123);


(The description for this forum says 'No question is too simple...' )

Regards
Richard
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
auto-boxing

(as of Java 1.5)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is similar to what lets you say

String myString = "Fred";

The powers that be decided this sort of thing was common enough that they'd give you a short-cut.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guy Hayward wrote:
I don't see what's going on here that allows the following line to compile.

Integer y = 123;

y is an Integer wrapper object, but there is no constructor. I thought I'd have to say something like

Integer y = new Integer(123);


To add what others have said. This...



is syntactic sugar for ...



Henry
 
Richard Hayward
Ranch Hand
Posts: 187
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:it is similar to what lets you say

String myString = "Fred";

The powers that be decided this sort of thing was common enough that they'd give you a short-cut.



Now you mention it, I've always been uneasy with statements like that as well.

Are you in agreement with Bear Bibeault that this is auto-boxing?
I thought statements like
String myString = "Fred";
were available in Java before Java 5, where autoboxing was introduced.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, definitely auto-boxing. Fred's example is a parallel, not the exact same mechanism. String myString = "Fred" is using a string literal.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guy Hayward wrote:I thought statements like
String myString = "Fred";
were available in Java before Java 5, where autoboxing was introduced.

It was. Java 5 just added it for all the wrapper classes. I think Fred also said that it's similar; it's not identical, because "Fred" is actually a proper String instance, so it's quite permissible to say "Fred".length(). '123' is not an Integer.

It's also worth remembering that it occurs, because it can add significant execution time to a program if you're not careful. My general rule of thumb is that I always try to box and unbox explicitly, so that I don't forget where it's happening. Another good rule to follow is: once an Integer, always an Integer.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!