File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

automatic primitive conversion from higher to lower precision?????

 
Amit Das
Ranch Hand
Posts: 206
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,



o/p is 100

but


we get compilation error.....loss of precision....justified!!!

but how about the final local int variable automatically converting to byte???

thanx
amit
 
lauren bai
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I change 1 line from you code and got "100" output.
class MyClass{
public static void main(String []args) {
int i = 100;
byte b = (byte)i;
System.out.println(b);
}
}


But I don't know why?

[ April 17, 2005: Message edited by: lauren bai ]
[ April 17, 2005: Message edited by: lauren bai ]
 
Nimai Etheridge
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since i is final and cannot change once it has been assigned, the compiler is smart enough to know that it can safely fit within a byte. Without the final modifier, the compiler has no idea what i might end up as at runtime, hence casts are required.
 
Manish Nijhawan
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If the Left Hand Side of the assignment operator is byte , char or short and the right hand side of the assignment operator can be resolved at compile time itself then the implicit narrowing conversion is applied by the compiler.
consider the following examples

byte b;
int ten = 10;
final int TEN = 10;
b = 10; // No compliation error as 10 is a constant and can be resolved at compile time

b = TEN; // No Compilation error as TEN is a final variable and can be resolved at complie time

b = ten; // Compilation error as ten is a variable which cannot be resolved at compliettime. ten can have any value which might not fit into byte.

b = 10 + TEN ; // No compilation error as 10 and TEN both can be resolved at compiletime.
 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has nothing to do with i being final and everything to do with i being a constant.

Doesn't this belong on a JavaRanch FAQ or something? I feel like I've said this 43 zillion times.

For the inquisitive, http://qa.jtiger.org/GetQAndA.action?categories=Trivia&showCodeLineNumbers=true
 
ken ren
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI sometime the compiler may not smart enough. I modify my code.
class MyClass
{
final static int iFS=100;
final static int iFSX;
final int iNum=100;

public static void main(String []args)
{
final int i = 100;
byte b = i;
//My code starts here
MyClass mc= new MyClass();
b=mc.iNum // Type mismatch: cannot convert from int to byte
b=iFSX; //Type mismatch: cannot convert from int to byte
b=iFS //it's OK
//My code ends here
System.out.println(b);
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic