This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

final Keyword in type conversion?  RSS feed

 
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 ]
 
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
 
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!
 
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.
 
We begin by testing your absorbancy by exposing you to this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!