File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes pls some1 clarify  this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "pls some1 clarify  this " Watch "pls some1 clarify  this " New topic
Author

pls some1 clarify this

Ishita Gupta
Greenhorn

Joined: Jan 03, 2003
Posts: 18
The following code behaves as i had put in comments.

I am unable to undstand why is it that when int i becomes final the code compiles giving the output.
Thanx in advance.
[ Jess added UBB [code] tags to preserve the whitespace, check 'em out! ]
[ January 05, 2003: Message edited by: Jessica Sant ]
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
Originally posted by like_java:
The following code behaves as i had put in comments.

I am unable to undstand why is it that when int i becomes final the code compiles giving the output.
Thanx in advance.
[ Jess added UBB [code] tags to preserve the whitespace, check 'em out! ]
[ January 05, 2003: Message edited by: Jessica Sant ]


final int i = 100;
byte b = i;
I noticed there is no casting for int i. So this has to do with up casting.
srini kami
Greenhorn

Joined: Dec 29, 2002
Posts: 26
Hi Like_java
I think it's because when you are assigning int(size32) i to byte(16) i, it needs explicit casting as you are narrowing the conversion.you can make above code work by removing the final as follows with explicit cast.
class MyClass
{
public static void main(String []args)
{
int i = 100;
byte b =(byte) i;
System.out.println(b);
}
}
It is working with final because you can't change value of final(constant), so when you are assigning it to byte, its not assigning it. Thats my best guess


Thank you<br />Srini
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

"like_java"-
two things...
1st thing:
Welcome to the JavaRanch! Please adjust your displayed name to match the JavaRanch Naming Policy.
You can change it here.
2nd thing:
Originally posted by like_java:

case 1: without final on line 5
Will give compilation error at line 7,
explicit cast needed for int to byte type
case 2: with final added before int on line 5
compiles, with o/p 100

final int i = 100;
byte b = i;
when i is marked as final, the compiler is sure of its value -- it knows without a doubt that the value 100 is small enough to be held within a byte -- so it knows there will be no loss of precision, and it'll let you do it.
however, if i is NOT marked as final, the compiler can't be sure of its value, and can't be sure that the value held in i is small enough to go into a byte without losing any precision. So... just to get itself off the hook -- it makes you take the heat if there's a problem (loss of precision) and you have to explicitly cast it.
As Kathy and Bert write in their new certification book:
"A cast is nothing more than your way of saying to the comipler. 'Trust me. I'm a professional. I take full responsibility for anything weird that happens when [you convert that int to a byte].' "
...again welcome to the JavaRanch!
[ January 05, 2003: Message edited by: Jessica Sant ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
bhatti mansha
Greenhorn

Joined: Jan 03, 2003
Posts: 7
hi like_java
there is another reason
byte have capacity to store 225 and u put in 100
so complier have no problem to complie this code easyly
if there u pass 256 then this is error
and for this u can use costing
thanx
MANSHA BHATTI
i hope you will clear more
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Originally posted by bhatti mansha:
hi like_java
there is another reason
byte have capacity to store 225 and u put in 100
so complier have no problem to complie this code easyly
if there u pass 256 then this is error
and for this u can use costing
thanx
MANSHA BHATTI
i hope you will clear more

Actually a byte can hold -128 to 127. So if you put in a final value between those digits -- no problem. But if you put a final value above 127 or below -128 you'll have to cast it (and you will lose precision -- but because you explicitly cast it -- that's your responsibility).
jason stark
Greenhorn

Joined: Dec 01, 2002
Posts: 10
I'm wondering if its assigning the same way as
byte b = 100;
as the value of i is final and the compiler is relaxing its usual assignment rules.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Originally posted by jason stark:
I'm wondering if its assigning the same way as
byte b = 100;
as the value of i is final and the compiler is relaxing its usual assignment rules.

right... because the compiler knows that 100 can fit in a byte without a problem... however if you try the following you'll get an error:
byte b = 128;
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
This code shows the compiler replacing the access to final primitive variables with the proper value itself, instead of with a reference to the variable.

3 getstatic #3 Field int i
accesing i as a variable because it is not final
12 iconst_2
accesing the value of j because it is final
19 sipush 32767
accesing the value of k because is final
28 ldc #5 Integer 32768
accessing the value of m because is final.
This one has to load the value from the constant pool because there are no bytecode able to assign this value directly. "push a short as int" --sipush-- can only assign a signed short (up to 32767 when possitive)


SCJP2. Please Indent your code using UBB Code
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: pls some1 clarify this
 
Similar Threads
switch construct
Literal Assignment for following Example
TOTALLY CONFUSED.......PLEASE HELP
Q how does the above code give error and the code below doesn't???
cast of numeric literal int