my dog learned polymorphism*
The moose likes Beginning Java and the fly likes compilation error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compilation error" Watch "compilation error" New topic
Author

compilation error

salvador rcn
Ranch Hand

Joined: Feb 18, 2004
Posts: 51
hi, here is a code i am testing...

i am getting compilation error..
javac test.java
test.java:8: cannot assign a value to final variable MIN_VALUE
Integer.MIN_VALUE = -2147483648 ;
^
test.java:9: cannot assign a value to final variable MAX_VALUE
Integer.MAX_VALUE = 2147483647 ;
^
2 errors
how can i remove compilation error? what changes i have to do?
Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
I guess you are new to Java. final variables are those which cannot be re-assigned any value once a value has been assigned to it. It is better you use another variable. You have to use a new variable for that.
Shekar.


Thanks,
Shekar
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

Why are you tring to do this? Integer.MIN_VALUE already has this value assigned to it - defined by Java. You should be able to just remove those two lines entirely, and it should be fine.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
salvador rcn
Ranch Hand

Joined: Feb 18, 2004
Posts: 51
Integer.MIN_VALUE already has this value assigned to it - defined by Java. You should be able to just remove those two lines entirely, and it should be fine.

yes .... t removed those lines and it worked.
my output:
Round f4 is -2147483648
Round f5 is 2147483647
Round f7 is -2147483648
why Round f7 is -2147483648 ?? given float f7 = -2147483655f, so the answer should be -2147483655 like others (bcoz nothing to round off...its whole number).
but why the last output is -2147483648 ?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
If you look at the method signatures for Math.round(), the one that accepts
a float as an argument returns an int value (the nearest int), hence -2147483648
Try it like this
double f7 = -2147483655D;
salvador rcn
Ranch Hand

Joined: Feb 18, 2004
Posts: 51
If you look at the method signatures for Math.round(), the one that accepts
a float as an argument returns an int value (the nearest int), hence -2147483648

hi, its not clear to me , how you are calculating this output ?
float f7 = -2147483655f;
see , the last two digit of f7 ..its 55..ok.The output i have got having last two digit 48....clearly these are not nearest integer. so, i think your explanation is not correct.
one more thing to note,float f7 = -2147483655f; ....it has no decimal point at all...so how can i think of rounding off?
anyway, i am not clear about the third output...will you be explicit more?
can you tell me how are you calculating the third output in a simple way.
thanks
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
The problem with float is that it is stored in 32 bits, a mantissa of about 24 bits and an exponent of 8 bits. They are both signed but because they have been normalized (another big topic) you get one more bit accuracy in your mantissa. However, that is the binary world. In the equivalent decimal world I think 25 bits equates to round about 32 million, so your number's magnitude is at best accurate to about 7 digits.
If you want more accuracy you need to work in doubles and longs.


The nice thing about Standards is that there are so many to choose from!
salvador rcn
Ranch Hand

Joined: Feb 18, 2004
Posts: 51

so your number's magnitude is at best accurate to about 7 digits.

do you mean the output i am getting is a kind of garbage ?
if it is not a garbage then can you tell me the mathematics , how i am getting that?
i simply want to know the math...can you show me how that calculation is going on to produce that output?
is it too difficult?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
As pointed out, the Math.round() method returns an int. Since the smallest integer value possible is -2147483648, this is the output it gives for any number that is smaller than this. In other words, an int variable cannot store the number -2147483655f. If you want to print out this value, don't use Math.round().


Java API Documentation
The Java Tutorial
Ashish Chande
Ranch Hand

Joined: Nov 26, 2000
Posts: 51
The range for int is -2,147,483,648 to 2,147,483,647.
You can check the o/p by using
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632

hi, its not clear to me , how you are calculating this output ?
float f7 = -2147483655f;
see , the last two digit of f7 ..its 55..ok.The output i have got having last two digit 48....clearly these are not nearest integer. so, i think your explanation is not correct.

My explanation is correct, perhaps not complete.
Here are the relevant parts from the API (from the Math class)
static long round(double a)
Returns the closest long to the argument.
static int round(float a)
Returns the closest int to the argument.
The difference is the method that accepts a double returns a long, so, the
number ending in 55 fits into a long, whereas it doesn't fit into an int
when the same number is 'typed' as a float.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compilation error