This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes final Keyword in type conversion? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "final Keyword in type conversion?" Watch "final Keyword in type conversion?" New topic
Author

final Keyword in type conversion?

Rohit Nath
Ranch Hand

Joined: Jun 16, 2006
Posts: 387

works fine with final keyword but gives compile time error if final keyword removed!
Error at line "byte b = i;"

MyClass.java:21: possible loss of precision
found : int
required: byte
byte b = i;


How is final related to this ?!

[ December 26, 2006: Message edited by: Rohit Nath ]
[ December 26, 2006: Message edited by: Bear Bibeault ]

R.N
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
A "final" variable is a constant.
A constant 100 is small enough to fit into a byte.
Your compiler accepts this.

An integer variable is, in general, too big to fit into a byte.
Your compiler knows this and gives the above error.
Rohit Nath
Ranch Hand

Joined: Jun 16, 2006
Posts: 387
Originally posted by Edvins Reisons:
A "final" variable is a constant.

Could you please elaborate on this with respect to int.
would declaring it long also be equivalent?
[ December 27, 2006: Message edited by: Rohit Nath ]
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
Indeed, looks like this behavior is particular to int.
Don't ask me why
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
Making i final allows javac to do a compile-time optimization -- replacing all instances of i with 100.

So instead of compiling 'byte b = i', the compiler really sees 'byte b = 100'.

And the compiler knows that there cannot be any loss of precision.
[ December 27, 2006: Message edited by: Scott Johnson ]
Rohit Nath
Ranch Hand

Joined: Jun 16, 2006
Posts: 387
compile-time optimization

makes sense.

Thanks for the information!
Rohit Nath
Rohit Nath
Ranch Hand

Joined: Jun 16, 2006
Posts: 387
Ok.. I am in big question?!

1.class MyClass
2. {
3. public static void main(String []args)
4. {
5. final int i = 127;
6. byte b = i;
7. System.out.println(b);
8. }
9. }

1) above code works fine but when I change value of i to 128 instead of 127.
final int i = 128;
It gives compile time error: possible loss of precision at line 6.

2) If I change int to long it does not work?

What is the reason for this? Could anyone help.
Thanks!
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

Let us revisit what has been said in earlier messages, in a bit more detail:

Compiler makes an optimization and tried to replace references of the constant with its 'final' value.

This means:

final int i = 100;
System.out.println(i); => System.out.println(100);

However, with long:

final long l = 100;
System.out.println(l); => System.out.println(100L);

Notice the difference? Compiler appends an 'L' to long literals.

Now, statement
byte b = 100;
compiles fine.

But statement
byte b = 100L;
does not compile.

Hence, it makes sense that

final int i = 100;
byte b = i;

compiles fine. Whereas,

final long l = 100;
byte b = l;

does not.

Now coming back to your recent question...

Please note that

byte b = 127;

compiles fine. Whereas,

byte b = 128;

does not.

Reason: Java does provide some support to assign literal values to a byte (or short, for that matter), but it doesn't support it all the way to implicit convert larger values. 128 is a value that can not be put in a byte, hence the statement

byte b = 128;

is interpreted as an attempt to assign an 'int' to a 'byte' (which ofcourse is something compiler does not like much).

Ergo,

final int i = 127;
byte b = i; => byte b = 127;

compiles fine, whereas

final int i = 128;
byte b = i; => byte b = 128;

does not.

Converting the 'int' constant to a 'long' does not help either for the reason specified earlier.
Rohit Nath
Ranch Hand

Joined: Jun 16, 2006
Posts: 387
Thanks Aditya!
That is useful information.

Rohit Nath.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final Keyword in type conversion?
 
Similar Threads
What's up with these literal assignments?
Why final variable in enhanced for isn't acting final?
tipsmart Q25
Boxing and Autoboxing
Plz explain me the answer of this code........