• 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

Reg. maximim int. value

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int has the range : -2147483648 to 2147483647
Why does the below line not give a compile-time error ?
int i = 2147483647+1;
System.out.println(" Compiled successfully : i = " + i1 );
And the below gives compile time error ?
byte b1 = 127+1;
System.out.println(" (Byte.MAX_VALUE value +1)" + b1 );
short s1 = 32767+1;
System.out.println(" (Short.MAX_VALUE value +1)" + s1 );
[This message has been edited by Angela Narain (edited August 25, 2001).]
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI ,
do this way
byte b1 =(byte) (127+1);
It won't give error
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code 1:
Assuming that it's i in the println & not i1


JLS 15.18
The type of each of the operands of the + operator must be a primitive numeric type, or a compile-time error occurs.
In every case, the type of each of the operands of the binary - operator must be a primitive numeric type, or a compile-time error occurs.


2147483647 & 1 are of type int(numeric types) , these meet the requirement.
Next check the target type , which is an int & operands are int , compiler satisfied.
The compiler won't check for overflow unless the literals used are 9223372036854775808L (for long) & 2147483648(for int) or higher.
Code 2:
Ditto Rajani.
128 does not fit in the byte range (-128 to 127)
Code 3:
range of short is -32768 to 32767
( 32767 + 1 )won't result in a short so you'll need a cast again.
[This message has been edited by Ashish Hareet (edited August 25, 2001).]
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case you aren't aware already, integer literals are of type 'int' by default. When you type 'byte b = 3', 3 is actually an int. The reason it compiles is that the compiler is allowed to implicitly cast the integer to a byte value in this case, provided it is in the range of 'byte'. The same applied to 'short'. 'int i = 2147483647+1' works because you are already dealing with 'int's. 2147483647+1 = -2147483648. It is valid Java math. I hope this was clear enough.
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So does it means adding 1 to the MAXIMUM value of int,byte,short
gives us the MINIMUM value.

byte b = (byte ) (127+1) ==> -128
short s = (short ) (32767+1) ==> -32768

[This message has been edited by Angela Narain (edited August 27, 2001).]
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With an explicit cast you are pretty much correct Angela .
Here's an interesting piece I came across while working around with this sorta stuff . Might aswell share it with you lot .
[/B]
Ashish
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello Ashish!
First of all I see a semi colon at the end of your class. Which means that you are coming from C++ background. Please remove it. Secondly, the code that you have given below does not compile. It gives error message saying
"Voodoo.java:4: No variable MAX_VALUE defined in class Short.
int short_range = Short.MAX_VALUE * 2 + 2;"
But we have field called MAX_VALUE in class Short.
I tried to compile it with jdk1.2.2 and jdk1.3.1 didn't work with both. I don't know what exactly is the problem and would like Ranchers to throw some light on this stuff.
thanks
Chandra!

-----------------------------------------------------------

Originally posted by Ashish Hareet:
[B]With an explicit cast you are pretty much correct Angela .
Here's an interesting piece I came across while working around with this sorta stuff . Might aswell share it with you lot .
[/B]
Ashish [/B]


 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Works fine for me...(Of Course! After removing the semi-colon at the end of the class )
The Output generated was...


Cheers!
Shyam

[This message has been edited by Shyamsundar Gururaj (edited August 28, 2001).]
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandrashekhar,
I don't come from a programming background . Java is my first programming language ever (I don't think HTML counts).As for the last semi-colon I have no clue 'bout it . My editor(Edit-Plus) seems to put it at the end & my thinghy's work alrite . Plz lemme know what difference it makes with that semi-colon at the end .
Thanx
Ashish
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Futher to Angela's question, try this...

Will the above code give compile time error?What will be the answer?

What will be the answer for the above?
Cheers,
Sandeep
SCJP2, OCSD, OCED
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi there,
I could compile and run Ashish's code when I put the Java source file in the jdk1.2.2\bin directory and run it from command prompt.
But when I put the file in some other folder and give the path for that folder in command prompt then it is not getting compiled. Where as all my other source files in that folder are getting compiled and I am able to run them. I didn't know what is the reason for such behavior. Please try the thing that i said and answer my question.
I put the source file Voodoo.java in some folder say 'Chandra'
So in Command prompt I gave
C:\Chandra> path = C:\jdk1.2.2\bin
and then I tried to compile the file using
C:\Chandra> javac Voodoo.java
But the compiler issued me error saying that it cannot find the MAX_VALUE in class Short.
But if I tried to compile other small java source files it is compiling and giving me the output.
Am I doing something wrong in giving the 'path'. Please let me know. Once again I could compile and run the Voodoo.java when I place it in the C:\jdk1.2.2\bin folder.
thanks for your help
Chandra!
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello chandrashekar,

The only possibility according to me is that some time ago you might made a jave file by the name "Short.java" may be unintentionally and that file is in the folder Chandra and that file has no variable named MAX_VALUE.That file is not in scope when you compile your program by putting in "c:\jdk1.2.2\bin" so it works fine there. That is why it giving you error that Short class does not have MAX_VALUE.
So, please check in the Chandra folder that there may not be a class file by the name Short.
NOTE: Never use same name as that of inbuilt classes.In this case there would not be any error or exception but in future when you want to use anything from the inbuilt class it will not found that thing(i.e. method or variable).
I hope this would solve your problem.
Gurpreet Sachdeva
For Mock Exams and some useful information about Bitshift operator, inner classes, garbage collection,etc please visit: http://www.go4java.20m.com
[This message has been edited by Gurpreet Sachdeva (edited August 29, 2001).]
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer.MAX_VALUE + int( so also byte & short) makes the result go back to the negative end of the number line.
That answers both of your queries Sandeep .
Did we forget a "L" somewhere ?
Ashish
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awesome!
I did the same mistake. Sorry for troubling you guys. I will follow the Note.
thanks
Chandra!
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ashish,

Originally posted by Ashish Hareet:
Integer.MAX_VALUE + int( so also byte & short) makes the result go back to the negative end of the number line.
That answers both of your queries Sandeep .
Did we forget a "L" somewhere ?
Ashish


That's correct!The first one is OK.
But what do you think of the second one?I have defined a long reference and am storing a value which could hold much more that specified by int; still I happen to get negative value!Doesn't that seem to be fishy - I know that RHS is getting converted into int; but seeing the LHS should it not be converted into long instead!!
Putting a L in the second addition value would do the job but I would've liked it to be done without the help of L!
The point that I am trying to make is the Developer is losing information even though he has specified a reference to hold a larger value!This means by default all the constants are taken as int (not long!), when you don't specify a L - a good case of loss of precision!
Hope this helps,
Sandeep
SCJP2, OCSD, OCED
[This message has been edited by Desai Sandeep (edited August 29, 2001).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic