aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Conversions with final keyword please helpme understand Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Conversions with final keyword please helpme understand" Watch "Conversions with final keyword please helpme understand" New topic
Author

Conversions with final keyword please helpme understand

Madhu Madala
Greenhorn

Joined: Jul 26, 2002
Posts: 9
public class Test
{
public static void main(String args[])
{
final int i=100;
byte b=i;
System.out.println(b);
}
}
The output is 100.

Code 2:
public class Test
{
public static void main(String args[])
{
int i=100;
byte b=i;
System.out.println(b);
}
}
I am wondering why it gives out put in first case ie., code 1.
Jonathas Carrijo
Greenhorn

Joined: Jul 25, 2002
Posts: 25
sorry... didn't get your doubt!
Was this code from a mock exam, a book? Please contextualize it
R u trying to figure out if there is a reason the code author didn't tell the output in the second case?
Thanx


Java has reached Brazil =D
Madhu Madala
Greenhorn

Joined: Jul 26, 2002
Posts: 9
Sorry I didnt mention when you run the code 2 there will be compilation error,but for Code 1 it will compile and gives out put as 100
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Madhu
I think the question you want answered is why does the 1st code compile and print 100 and the 2nd code not compile at all, right?
In the 1st example the int i is a final varialbe so the compiler knopws that its value will not change. when you assign the value of i to the byte variable b the compiler 'knows' that it is ok, becasue i is final, and will allow the assignment.
In the 2nd piece of code the int is not final so the assignment of its value to the byte requires a cast. Because it is a narrowing conversion and i might not always fit into a byte you have to tell the compoiler that it is ok and that it is what you want to do even if you might lose data.
hope that helps


Dave
Madhu Madala
Greenhorn

Joined: Jul 26, 2002
Posts: 9
Thanks Dave,I got it.
From explaination can i conclude like this
1.Any final variable which can fit into another data type say byte is okie as long as value fits in at that time as it is final and compiler knows that it wont change further.
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 34
Madhu this is not the case each time, see my
thread named final int conversion.
Thanks
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Bindesh,
Can you please post that link here.I searched I couldn't get.
Thanks
Veena


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 34
Veena ,I guess by this time you would have got the thread, still:
fina int conversion
[ August 08, 2002: Message edited by: Bindesh Vijayan ]
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 34
Hey!!!Congrats to me on becoming RanchHand
[ August 08, 2002: Message edited by: Bindesh Vijayan ]
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Bindesh:
Your issue was different than hers. Yours was about assigning non-static value to a static variable and her's is explicit cast needed for narrowing.
Barkat Mardhani
 
Consider Paul's rocket mass heater.
 
subject: Conversions with final keyword please helpme understand