Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes clear my doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "clear my doubt" Watch "clear my doubt" New topic

clear my doubt

shameena Hussain

Joined: Jul 05, 2005
Posts: 14
hi friends,

wat is the output of the following code,


and also explain me how it comes..

thanx in advance
marc weber

Joined: Aug 31, 2004
Posts: 11343

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.)

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83

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

Joined: Aug 03, 2002
Posts: 7729
If it's the hexadecimal conversion you are unsure about you may like to refer to Corey's TipLine article

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link:
subject: clear my doubt
It's not a secret anymore!