Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Final Keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Final Keyword" Watch "Final Keyword" New topic
Author

Final Keyword

Rajesh Pore
Greenhorn

Joined: Jul 19, 2005
Posts: 21
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

Joined: Apr 21, 2006
Posts: 1847
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.


42
Rajesh Pore
Greenhorn

Joined: Jul 19, 2005
Posts: 21
Thanks Jeroen !
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

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.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Neha Mohit
Ranch Hand

Joined: Apr 25, 2006
Posts: 87
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: 8815
    
    5
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


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

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

Joined: Oct 20, 2003
Posts: 47
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
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
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
Hi Bert,

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

Regards
Rupal(previously scjp)
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Final Keyword