• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Character Wrapper and Literal int Assignment

 
Geoff Prest
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you are feeling brave, have a look at the BCEL Manual.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic