1) class MyClass { public static void main(String []args) { final int i = 100; byte b = i; System.out.println(b); } }
Above class get complied and output is 100 when run ,while below class gives compile time error (loss of precision which is normal)
2) class MyClass { public static void main(String []args) { int i = 100; byte b = i; System.out.println(b); } }
Thanks & Regards Rupal
Jeroen T Wenting
Ranch Hand
Joined: Apr 21, 2006
Posts: 1847
posted
0
In the first instance the compiler can determine at compile time that the assignment will not cause trouble. In the second example that's not possible because the int isn't final and thus its value isn't fixed at compile time.
The first could be translated into byte b = 100; by the compiler, which is valid. The second cannot be so handled, so the compiler sees you trying to assign a 2 byte value to a 1 byte address space, and complains about that.
Originally posted by John Meyers: You also need to make sure that the defenition and declaration occur in the same line.
final int i; i = 10;
is not the same as
final int i=10;
The former code fragment will not enable the compiler to know what the final value is.
.
But John down here in my code the compiler compalins if it try to reassign z.
class Demo { public static void main(String arg[]) { final int z ; z = 90 ; // z =990; // line 2 System.out.println(z); } }
Even here i cannot assign z again(at line 2) , so what actually is the differnece between
final int z = 90 ;
and
final int z ; z = 90 ;
Thanks in advance.
Bert Bates
author
Sheriff
Joined: Oct 14, 2002
Posts: 8712
posted
0
Hey there "scjp" -
Welcome to the ranch! We've found that things stay a lot friendlier if people use their real names, so I'd like you to change your display name to match our naming policy, thanks!
Hope to see more of you around the ranch.
Bert
Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
final int i = 10; byte b = i; final int eye; eye = 10; b = eye; // fails
b=eye fails because the final int eye; is not a compile time constant. The value of eye is initialized later, after it has been defined.
Kaise a.Zakkar
Ranch Hand
Joined: Oct 20, 2003
Posts: 47
posted
0
Well First There is a rule for the final variable ,which tells that those variables are treated as compile-time constants, that mean that the compiler will treat the final int i as 100 whenever it's used in the code . Second we have a rule that is called Implicit narrowing: that rule inquier a set of conditions to be approved befor it can be applied to the primitve data types variables ,those conditions are: 1- the right most operand must be a constant numeric value (byte,short,char ,,,etc) ,and final variables are treates just like a constant values . 2- the right most operand must be in range of the type specified by the left hand operand : for example consider that we have a byte x variable and number 125 so the assignmnet byte x = 125 will be accepted by the compiler because its fullfill the conditions of implicit narrowing action. Now we return back to the question: the second declaration for the variable i didnt follow neither the conditions mentionend at the rules of implicit narrowing, nor declared as final variable , so as we learned that the default data type for all the intgeral arithmatic operations is int,the variable i (also its declared as int ),will be promoted to int even if it is not declared to be so ,in this case the compiler refuse to assign an int variable to a byte one , and complain the lose of precision . i am sorry for the long comment i hope you will find it useful good luck
thanks to all ranches
[ April 28, 2006: Message edited by: Kaise a.Zakkar ]
"Nothing is harder on your aurels than resting on them."<br />SCJP 1.4 89%,,SCJA 80%,SCWCD1.4 81%
Neha Mohit
Ranch Hand
Joined: Apr 25, 2006
Posts: 87
posted
0
So , There is some difference between constant and compile time constant. a little more detail please
Thanks in advance -Neha
Neha Mohit
Ranch Hand
Joined: Apr 25, 2006
Posts: 87
posted
0
Originally posted by John Meyers: Sorry about not being more specific...
This is what i meant:
final int i = 10; byte b = i; final int eye; eye = 10; b = eye; // fails
b=eye fails because the final int eye; is not a compile time constant. The value of eye is initialized later, after it has been defined.
but eye is still constant(correct me if i am wrong ). So are there differences between constant and compile constant.
Please expalin, will be really obliged
Neha
Rajesh Pore
Greenhorn
Joined: Jul 19, 2005
Posts: 21
posted
0
Hi Bert,
Was not aware of the naming policy ,sorry for that .