This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Final Keyword

 
Rajesh Pore
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Jeroen !
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • 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.
 
Neha Mohit
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • 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.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • 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
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • 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.
 
Kaise a.Zakkar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Hi Bert,

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

Regards
Rupal(previously scjp)
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic