File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TOTALLY CONFUSED.......PLEASE HELP

 
Manish Singhal
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3673
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Ajith Kallambella
Sheriff
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Manish Singhal
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 243
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic