• 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

Integer min and max

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am not able to understand why answer f is the correct answer. Can someone explain?

Thanks,
Ramya.

class JJF5 {
public static void main(String args[]) {
System.out.print(Integer.toHexString(Integer.MIN_VALUE)+",");
System.out.print(Integer.toHexString(Integer.MAX_VALUE));
}}

What is the result of attempting to compile and run the program?

a. Prints: 0000,ffff
b. Prints: 00000000,ffffffff
c. Prints: 7fff,8000
d. Prints: 8000,7fff
e. Prints: 7fffffff,80000000
f. Prints: 80000000,7fffffff
g. Compile-time error
h. Run-time error
i. None of the above
 
Ranch Hand
Posts: 431
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

First of all Welcome to the ranchers' community.

Integer.toHexString(..) is a function that converts the parameter passed into an equivalent Hexa-decimal string.

INTEGER.MIN_VALUE is a predefined constant (final) that has the value of
-2^31.

Similarly INTEGER.MAX_VALUE has a value of (2^31)-1.

If you convert the above said values to hexa decimal you will get the answer that you are pointing to.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll probably want to read up on two's complement binary notation for signed integers, as well as binary-to-hex conversion.

The minimum value for an int is -2147483648. The bit pattern for that number is
1000 0000 0000 0000 0000 0000 0000 0000

The hex equivalent to that bit pattern (1 hex digit per 4 bits) is
80000000

The maximum value for an int is 2147483647. The bit pattern for that number is
0111 1111 1111 1111 1111 1111 1111 1111

The hex equivalent to that bit pattern (1 hex digit per 4 bits) is
7FFFFFFF

Search the forum or google for "two's complement" and you should be able to find some articles explaining how it works. You'll also want to get at least a little bit comfortable converting from binary to hex and from hex to binary, using a pencil and paper, or doing it in your head once you're used to it.
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeo ,
u have given the correct explanation, I ll appreciate it.
Sethu.
 
Ramya Iyer
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! it is clear now.
reply
    Bookmark Topic Watch Topic
  • New Topic