This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
First: As a Character object can only be constructed using a char value, is the assignment of a literal int ...
... replaced with the creation of a char that is used to create the Character ...
... at compile time through boxing?
And second: I've read over the API but cannot understand the reasoning behind the range of values the Character.getNumericValue() method returns. I've figured out that it is not to be used if I want to find out that 'A' is the 65th character, but what would I use it for?
Thanks in advance for any responses,
Have a good day, Geoff. [ October 08, 2008: Message edited by: Geoff Prest ]
Don't know, but I can suggest how you can try it. Write a class with two simple methods in, like thisSee whether it compiles at all; if not change 65 to (char)65 in both. Get the two class files and write javap -c MyClass or similar for each, and see whether the description of the bytecode is different. There should be enough detail for you to answer your question; be sure to post the results for everybody to see.
Joined: Oct 13, 2005
No, if I remember correctly, getNumericValue doesn't return 65 from A at all. If you read that description, you should be able to work out what the values mean.
Joined: Feb 20, 2007
Thanks for your reply Campbell,
As you suggested, I created a class that perfomed the same function (ie: creating a Character object with a value of 'A') using both direct assignment and construction. Out of curiosity, I extended the class to provide methods that would directly assign various literals of the same equivalent char value to see what that would look like as well.
Here is the class file followed by the javap output:
javap -c LiteralTest
To reiterate the question I had originally asked: When creating a Character by way of a direct assignment, does the compiler create a char with the provided value and use it to construct (ie: invoke new Character()) the Character object?
The answer is no.
According to the byte code, the process by which an object is created will differ depending on the method used. I don't understand enough about byte code to explain what specifically is going on, but it's evident to me that the process I laid out in the question does not take place.
I've found a starting point for my byte code education on wikipedia and Looking Under The Hood here at the ranch. Any other recommendations of suggested reading materials would be greatly appreciated.
Thanks again Campbell for answering my question in a way that forced me to learn something. It's much more rewarding this way.
[ October 09, 2008: Message edited by: Geoff Prest ] [ October 09, 2008: Message edited by: Geoff Prest ]
All your methods that use autoboxing are translated into a call to the static method Character.valueOf(). The only explicit "new Character()" is the one you coded yourself. It's not actually very mysterious; the compiler handles all autoboxing by an appropriate call to a valueOf() function (there's one in every wrapper class Integer, Short, Float, etc.)
What valueOf() does is clever; it tries to save on object allocation and make autoboxing cheap. For the integral types, if the value is small -- less than 128, I think, in general; then the method returns a pre-allocated wrapper object from a table. If the value is large, then valueOf() uses "new" to create a new wrapper object.