Get your CodeRanch badge!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes automatic primitive conversion from higher to lower precision????? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "automatic primitive conversion from higher to lower precision?????" Watch "automatic primitive conversion from higher to lower precision?????" New topic
Author

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

Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
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

Joined: Apr 08, 2005
Posts: 18
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 ]

SCJP (In Progress)
Nimai Etheridge
Greenhorn

Joined: Feb 16, 2005
Posts: 16
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

Joined: Apr 04, 2005
Posts: 10
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.


Manish Nijhawan<br />B-Tech
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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


Tony Morris
Java Q&A (FAQ, Trivia)
ken ren
Greenhorn

Joined: Apr 18, 2005
Posts: 10
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
 
subject: automatic primitive conversion from higher to lower precision?????
 
Similar Threads
Conversion
final Keyword in type conversion?
What is the output for First Snippet and Second Snippet?
how about this test?
Final Keyword