Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conversions with final keyword please helpme understand

 
Madhu Madala
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Madhu Madala
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Madhu Madala
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Madhu this is not the case each time, see my
thread named final int conversion.
Thanks
 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bindesh,
Can you please post that link here.I searched I couldn't get.
Thanks
Veena
 
Bindesh Vijayan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey!!!Congrats to me on becoming RanchHand
[ August 08, 2002: Message edited by: Bindesh Vijayan ]
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic