GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes TOTALLY CONFUSED.......PLEASE HELP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "TOTALLY CONFUSED.......PLEASE HELP" Watch "TOTALLY CONFUSED.......PLEASE HELP" New topic
Author

TOTALLY CONFUSED.......PLEASE HELP

Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104
1. class Test
2. {
3. public static void main(String []args)
4. {
5. final int i = 100;
6. byte b = i;
7. System.out.println("value of b : " + b);
8. }
9. }
The above code gets compiled and prints value of b : 100
which according to the rule of narrowing conversion should NOT have happened without casting.

BUT
if I remove final from the line no. 5 then compiler error is there.
friends please HELP ME.
Manish
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3308
    
    7
In you previous post you pointed out this link...
http://www.enthuware.com/jqplus/FAJQuestions.html
The same page addresses your this question also!!!
-Paul.
------------------
Get Certified, Guaranteed! http://www.enthuware.com/jqplus

[This message has been edited by Paul Anil (edited January 02, 2001).]


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
final variables are resolved at compile time. So, the statements
final int i = 100;
byte b = i;
are equivalent to
byte b = 100
which makes compiler happy
However when you remove the final keyword, the variable i is considered as a int variable and compiler requires you to use explicit casting when you assign int to byte.
Hope that helps
Ajith

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104
Thanks Ajith
I got something with that.
It would be more help to me if you please make your statement( final variables are resolved at compile time ) more clear and explainatory.
Thanks
Looking for an affirmative response.
[This message has been edited by Manish Singhal (edited October 04, 2000).]
b moon
Greenhorn

Joined: Oct 04, 2000
Posts: 1
I was reading some fine print last week on numeric type conversions on assignments which read something along these lines.
"Implicit narrowing primitive conversions on assignment can occur in cases where the source is an int constant expression whose value can be determined to be in the range of the destination type at compile time; the destination type is either byte, short or char type." In your example the assignment of a final would be a constant expression.
Sanjeev Verma
Ranch Hand

Joined: Aug 24, 2000
Posts: 87
Dear Manish
All that the guys above are trying to say is that when a variable is declared as final, since the compiler knows that the value of this variable is not meant to change, it replaces all occurences of that variable withthe actual value, which in this case is the integer constant 100. Now, since 100 is within range of the the byte variable, the compiler does not complain.
Hope this helps
Sanjeev
mohit joshi
Ranch Hand

Joined: Sep 23, 2000
Posts: 243
Going by Moons observation, the following code doesnt compile
class Test22 {
public static void main(String []args)
{
final double d = 100.0;
float f = d;
System.out.println("value of f : " + f);
}
}
Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104
Thanks a lot Sanjeev
I got you....
Thats a great explnation and my stupidity that I was not getting that simple thing... anyway guys u know it sometime happens
once again thanks,
All the Best
Manish

[This message has been edited by Manish Singhal (edited October 05, 2000).]
Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104

class Test22 {
public static void main(String []args)
{
final double d = 100.0;
float f = d;
System.out.println("value of f : " + f);
}
}
Above code posted by Mohit will not compile becasue float/double belongs to floating point category and byte/int belongs to integral category, and by default floating pt. literals are "double" so we always write float f = 100.0f
but in above code the value against float f= d is 100.0 and not 100.0f , According to the message posted by Sanjeev, i.e. when a variable is declared as final, since the compiler knows that the value of this variable is not meant to change, it replaces all occurences of that variable with the actual value, which in this case is the double constant 100
I hope I am right upto some extent.
Please correct me.
Manish

[This message has been edited by Manish Singhal (edited October 05, 2000).]
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
"b moon",
Welcome to JavaRanch!
As you are new, please register with a proper name. PROPER NAMES ARE NOW REQUIRED!! Read this post for details.
viyer
Greenhorn

Joined: Oct 05, 2000
Posts: 2
"Implicit narrowing primitive conversions on assignment can occur in cases where the source is an INT constant expression whose value can be determined to be in the range of the destination type at compile time; the destination type is either byte, short or char type." In your example the assignment of a final would be a constant expression.
NOTE this is true for only int. For all other conversions u have to explicitly cast.

Originally posted by mohit joshi:
Going by Moons observation, the following code doesnt compile
class Test22 {
public static void main(String []args)
{
final double d = 100.0;
float f = d;
System.out.println("value of f : " + f);
}
}

Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
"viyer",
Welcome to JavaRanch!
As you are new, please register with a proper name. PROPER NAMES ARE NOW REQUIRED!! Read this post for details.
Sorry for spoiling the party..I hate to do this policing as much as you hate to see my unrelated posts here. But compliance to Javaranch policy is a must and is highly appreciated.
Ajith
 
GeeCON Prague 2014
 
subject: TOTALLY CONFUSED.......PLEASE HELP