Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

clear my doubt

 
shameena Hussain
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends,

wat is the output of the following code,

System.out.print(Long.toHexString(Character.MAX_VALUE))

and also explain me how it comes..


thanx in advance
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you check the API for Character, you'll see the following for MAX_VALUE...
The constant value of this field is the largest value of type char, '\uFFFF'.

(Note that a char is 16 bits, but the values are all non-negative. So the maximum value is (2^16) - 1.)
 
Jon Egan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shameena,

I hope this doesn't come out sounding rude, I mean it quite sincerely...

One of the best bits of advice I can give for preparing for the SCJP exam is that any question you have of the form "what is the output of the following code" should be something you try yourself. It is likely you will learn at least 2 unrelated things about Java just from the experience of:

1. typing it in
2. compiling it
3. fixing typos
4. compiling it again
5. running it
6. fixing the classpath
7. running it again
8. ....

I think it's the combination of all those "other things" you learn in the process that makes you ready for the exam.

I know that a lot of people seem to try to learn java by preparing for this exam (a backwards approach, to me), so it's possible you are just starting out and wouldn't know how to run this for yourself. If that is the case, I suggest getting past that hurdle before you continue to study exam concepts.

The code you would need to answer that portion of your question is the following:



If you need help to get it to run, I suggest posting for help in the "Java in General - Beginner" forum.

Now to the second part of the question: To understand "why" it prints the value it does, work from the inside of the expression to the outside.

The innermost part of the expression is the Character.MAX_VALUE - be sure you understand that. In addition to the info Marc pointed out in the API doc, there's also the trick of clicking the "Constant Field Values" link in the doc to get the ACTUAL value (in decimal, which is a bit more human-readable). Character's MAX_VALUE is the highest value that fits in a 16 bit unsigned number - 65535.

Outside that expression is a call to Long.toHexString(). Take a look at the API for the class Long (in java.lang) and see what this method will do to the value that is provided to it (in this case, 65535).

Outside that is just System.out.print(), which puts the value that is passed to it out to the console.

If you need more help making sense of this, post your best attempt at an explanation, we can fill in anything you're missing.

Hope this helps.
-- Jon (SCJP 1.4)
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's the hexadecimal conversion you are unsure about you may like to refer to Corey's TipLine article
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic