aspose file tools*
The moose likes Beginning Java and the fly likes Character Wrapper and Literal int Assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Character Wrapper and Literal int Assignment" Watch "Character Wrapper and Literal int Assignment" New topic
Author

Character Wrapper and Literal int Assignment

Geoff Prest
Greenhorn

Joined: Feb 20, 2007
Posts: 17
Hi all,

I've a two part question today:

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 ]

SCJP 6, SCJA, SCJD in progress
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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.
Geoff Prest
Greenhorn

Joined: Feb 20, 2007
Posts: 17
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:

LiteralTest.java

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.

Cheers,
Geoff

[ October 09, 2008: Message edited by: Geoff Prest ]
[ October 09, 2008: Message edited by: Geoff Prest ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Geoff,

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.


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
And if you are feeling brave, have a look at the BCEL Manual.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Character Wrapper and Literal int Assignment