• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Final Keyword

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

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
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rajesh Pore
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeroen !
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

You need to change your name from scjp to one that has a first and last name by the way.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Neha Mohit
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So , There is some difference between constant and compile time constant. a little more detail please



Thanks in advance
-Neha
 
Neha Mohit
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bert,

Was not aware of the naming policy ,sorry for that .

Regards
Rupal(previously scjp)
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rupal:
Hi Bert,

Was not aware of the naming policy ,sorry for that .

Regards
Rupal(previously scjp)



Well you are almost there. The Naming Policy requires a real first and a real last name.

I am assuming Rupal is your first name, so all we need is a last name.

Mark
 
reply
    Bookmark Topic Watch Topic
  • New Topic