• 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

pls some1 clarify this  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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 ]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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).
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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;
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!