• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Final variable

 
Swapna James
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Can you tell me the difference between these codes.


This one complies fine


This one gives a compile time error. Can anybody tell me what the difference in int to byte conversion when "final" is added.

Regards,
Swapna.
 
Vasanth Raja.K.
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
All final variabales expressions are evaluated at compile time.In ur first code final int i=100 is declared and intialized at compile time.So,the value of i is known at compile time,when assigned to b.It's similar like byte b=100.So there is no compile time error.
In second example,the member variables are intialized when u are creating the object(ie., at runtime)So the value of i when assigned to b is not known by the compiler.Hence it complains.
Hope it helps.
Reg
Vasanth
 
Abhishek Jain
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Swapna

This is Abhishek again.
This question is regarding automatic narrowing
e.g.
byte b= i;

taking i as integer

this code will compile fine only if both of these
1)
i is an COMPILE TIME CONSTANT again if i is COMPILE TIME CONSTANT
i.e. i is declared as final

2)
i is in the range of byte ie -128 to 127

for eg
int i=100;
final int j=100;
final int k=1000;
byte b=i // error as i not a compile time constant
byte b=j // COMPILEs FINE
byte b=100 // fine
byte b=129 // error not in range
byte b=k; // although k is constant but error because not in range


I think u can now understand how its working
u can refer Khalid Mughal for better understanding

Bye & take care
Keep mailing
Regards
Abhishek
 
Chindhu Nair
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Swapna,

If the source and target types are different, type conversion may be necessary when it comes to assigning values to a variable. While Java takes care of primitive widening conversion (conversion from narrower type to wider type), it requires you to explicitly use a cast for narrowing conversion (conversion from wider type to narrower type).

But Java will perform an implicit narrowing conversion if the following are true.

a) Source value is of type int, char, short or byte
b) Source value is declared final or is a literal value.
c) Source value is within the range of the target type.

If all the above conditions are met, it compiles without an explicit cast. Else a compile error would be thrown.

Since in your code (case1), i is both final as well as int, it compiles.

In code case2, i is int but not final. Hence the compile error.
If you modify this code to include an explicit cast, it will also compile fine.

public class Test{
public static void main(String []args) {
int i = 100;
byte b = (byte)i; // explicit cast used.
System.out.println(b);
}
}

Hope this helps.

Thanks,
Chintu.
 
Swapna James
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys.... I got the point.

And I will certainly look into Khalid Mughal's book.

Swapna.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic