The Artima Developer Community
Sponsored Link

Java Answers Forum
Unboxing or toString()?

1 reply on 1 page. Most recent reply: Oct 2, 2008 2:19 AM by Vincent O'Sullivan

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 1 reply on 1 page
Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Unboxing or toString()? Posted: Oct 1, 2008 11:02 PM
Reply to this message Reply
Advertisement
I've just been shown the following code from an Open University Java text book (M257 Unit 5 Page 21, to be precise):
		ArrayList<Character> holder = new ArrayList<Character>();
		holder.add('Z');
		System.out.println("The answer is " + holder.remove(0));
It is part of the text and not an exercise. It is cited as an example of unboxing but appears to be an example of toString() being invoked.

I don't think that unboxing is happening but I'm not certain, since each of the following three examples produce the same output:
		ArrayList<Character> holder = new ArrayList<Character>();
		holder.add('X');
		System.out.println("The answer is " + holder.remove(0));
		holder.add('Y');
		System.out.println("The answer is " + holder.remove(0).toString());
		System.out.println("The answer is " + 'Z');
Can anyone else say for certain what is happening here (i.e. that unboxing is or isn't happening in the initial example)?


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Unboxing or toString()? Posted: Oct 2, 2008 2:19 AM
Reply to this message Reply
In answer to my own question. Putting the original code into the eclipse IDE yields a boxing warning but no unboxing warning. Substituting the println line with char c = holder.remove(0); does generate an unboxing warning. Therefore, I think that it's safe to assume that unboxing is not occurring in the original example.

Flat View: This topic has 1 reply on 1 page
Topic: guess the number game Previous Topic   Next Topic Topic: Rectangles in Java

Sponsored Links



Google
  Web Artima.com   

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