aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Final with primitives Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Final with primitives" Watch "Final with primitives" New topic
Author

Final with primitives

Sridhar Padala
Greenhorn

Joined: Aug 03, 2007
Posts: 20
The below code works fine

class Final
{

public static byte mone()
{
final int i=127;
return i;

}
}

But why the below code is not working as final means compile time constant and can't be changed in the future and the number is in byte range also.

class Final
{

public static byte mone()
{
final float i=127; //not working for long.float and double.
return i;

}
}
Explain in detail the reason. Thanks in Advance
Benoît de Chateauvieux
Ranch Hand

Joined: Aug 10, 2007
Posts: 183
Hi Sridhar Padala,

It's a question for the SCJP forum, not for the SCBCD one...
But the answer is:

Implicit Cast (Narrowing) is only possible with integers literals.
It's mean you can cast implicitly to:
- byte (if the value is < 128)
- short (if the value is < 32768)
- char (if the value is < 65536)

There's no implicit cast with two variable.
But in your case, the int i is final, so it's considered as literal by the compiler.

There's no implicit cast with float and double.

Hope it helps,

Beno�t


SCJP5 | SCBCD5 | SCEA5 Part 1
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Final with primitives