Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between coersion and autoboxing  RSS feed

 
Nidheesh Krishna
Ranch Hand
Posts: 62
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between coersion and autoboxing in java?please explain this with examples
 
Stephan van Hulst
Saloon Keeper
Posts: 7805
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When boxing, Java wraps a primitive in a wrapper object.

Java only supports type coercion from narrow numeric primitives to wider numeric primitives, such as assigning an int to a double or long without casting.
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Java only supports type coercion from narrow numeric primitives to wider numeric primitives, such as assigning an int to a double or long without casting.

Is it really only supported for (numeric) primitives? What about assigning an instance of a subclass to a reference variable of a superclass?
 
Stephan van Hulst
Saloon Keeper
Posts: 7805
142
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't call that coercion. In coercion, you change the internal structure of the value, and this is a runtime operation. In upcasting you only change the formal type; this is done at compile time.
 
Claude Moore
Ranch Hand
Posts: 891
8
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Is it really only supported for (numeric) primitives? What about assigning an instance of a subclass to a reference variable of a superclass?


Cool. Never thought about it, I used to use auto-inboxing only on numeric types....
 
Stephan van Hulst
Saloon Keeper
Posts: 7805
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is not boxing. That's casting.

In Roel's example, String is being cast to CharSequence. This is a compile time operation, and nothing happens to the value itself.

Boxing a primitive is a runtime operation. The value is actually being wrapped in a new object.
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:I wouldn't call that coercion. In coercion, you change the internal structure of the value, and this is a runtime operation.

Ah ok! I had never heard of the term "(type) coercion" before, so I was just wondering if that would not be coercion as well. Thanks for the clarification!
 
Nidheesh Krishna
Ranch Hand
Posts: 62
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to everyone
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:. . . I had never heard of the term "(type) coercion" before, . . .
This Wikipedia article seems to suggest that coercion is another name for a widening primitive conversion (or maybe promotion, too). It seems to mean something different in Eiffel on .NET. The word coercion appears not to be mentioned in the Java® Language Specification (=JLS), so maybe we should never say coercion about Java®.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!