The Artima Developer Community
Sponsored Link

Agile Buzz Forum
Primitives and auto-boxing transparency

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
James Robertson

Posts: 29924
Nickname: jarober61
Registered: Jun, 2003

David Buck, Smalltalker at large
Primitives and auto-boxing transparency Posted: Jul 7, 2004 6:02 AM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by James Robertson.
Original Post: Primitives and auto-boxing transparency
Feed Title: Cincom Smalltalk Blog - Smalltalk with Rants
Feed URL: http://www.cincomsmalltalk.com/rssBlog/rssBlogView.xml
Feed Description: James Robertson comments on Cincom Smalltalk, the Smalltalk development community, and IT trends and issues in general.
Latest Agile Buzz Posts
Latest Agile Buzz Posts by James Robertson
Latest Posts From Cincom Smalltalk Blog - Smalltalk with Rants

Advertisement

Patrick Logan points to some pages with some "surprises" for Java developers using auto-boxing. This is the one that tosses some people:

Integer i1 = new Integer( 2);
  Integer i2 = new Integer( 2);
  System.out.println( i1==i2);


  Integer j1 = 2;
  Integer j2 = 2;
  System.out.println( j1==j2);

The interesting issue is that the first answers false, the second true. Now, this isn't a huge surprise; the "==" is doing an identity check - in Smalltalk, this gives you false as well:


1.5 == 1.5

It's an artifact of optimization that 2 == 2 gives you true in Smalltalk. The reason this generates some confusion in Java-land is that an equality check takes more work to write out - recall that "=" is assignment in Java (I didn't recall this at first - it's been too many years since I did C). So to get an equality check:

Java


"hello".equals "hello"

In Smalltalk:


'hello' = 'hello'

And of course, you can override #= in your own classes in order to create a localized definition of equality. The upshot is, people tend to write == in Java where they should be using equals() - and now auto-boxing is going to bite them when they do that. Just look at the comment thread here for an example of the confusion. The problem arises because you can "get away" with using the == operator when using primitive data types. You'll then get bitten if you let the habit carry over to using the new auto-boxing feature. This is another one of those cases where creeping complexity ends up being a real problem...

Read: Primitives and auto-boxing transparency

Topic: What's new in the next BottomFeeder Previous Topic   Next Topic Topic: Give Sun credit

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use