• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

clear my doubt

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic