Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final Keyword in type conversion?

 
Rohit Nath
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, looks like this behavior is particular to int.
Don't ask me why
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compile-time optimization

makes sense.

Thanks for the information!
Rohit Nath
 
Rohit Nath
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Aditya!
That is useful information.

Rohit Nath.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic