• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

doubt in method argument declared final  RSS feed

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello ranchers,

this snippet was there in one of the post , but no explanation was therefor that, I also tried it but couldn't figure out the problem.
the snippet is as follows:

public class MyClass{
public static void main(String[] args){
char c='A';
m1();m2(c);
}
static byte m1() {
final char c='B';
return c;
}
static byte m2(final char c) {return c;}//1
}


It's giving compiler error at 1 stating loss of precision at 1 ,but the method m1() works just fine.
Plz reply

Regards
Akshay
 
author
Posts: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
m1() compiles because the compiler knows that it is "final" and knows that it fits into a byte when casted.

m2() does not compile because althought the compiler knows that it is "final", it doesn't know what it is, or if it will fit into a byte when casted.

Henry
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with u henry,thanks
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't use modifiers in method arguments. (SCJP 1.4)
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Steve Liem:
You can't use modifiers in method arguments. (SCJP 1.4)



Try it. final is a perfectly fine modifier for a formal parameter in Java.
 
Akshay Singhvi
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:
m1() compiles because the compiler knows that it is "final" and knows that it fits into a byte when casted.

m2() does not compile because althought the compiler knows that it is "final", it doesn't know what it is, or if it will fit into a byte when casted.

Henry



Hello Henry,
Sorry but i couldnt get ur explanation,can u plz elaborate ur reasoning...
Regards
Akshay
 
Steve Liem
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:


Try it. final is a perfectly fine modifier for a formal parameter in Java.



Wow I see.
But is this possible for all kinds of modifiers for variables?
 
Henry Wong
author
Posts: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Akshay Singhvi:

Hello Henry,
Sorry but i couldnt get ur explanation,can u plz elaborate ur reasoning...
Regards
Akshay



What part are you still confused about?

Not sure if I can elaborate my reasoning, as there is no reasoning involved -- I am just stating what is happening.

Henry
 
Akshay Singhvi
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:


What part are you still confused about?

Not sure if I can elaborate my reasoning, as there is no reasoning involved -- I am just stating what is happening.

Henry



hi henry,
well what i didn't get is how at m1 compiler is sure that casting is possible,but not at m2.
What best i can percieve is that at m2 since it is final formal parameter which is resolved not at compiler time, that's why ..
regards
Akshay
 
Henry Wong
author
Posts: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Akshay Singhvi:

hi henry,
well what i didn't get is how at m1 compiler is sure that casting is possible,but not at m2.
What best i can percieve is that at m2 since it is final formal parameter which is resolved not at compiler time, that's why ..
regards
Akshay



In the case of m1, the compiler knows that the char value is 'b' which has a unicode that will fit into a byte. It also know the variable is final so it won't change.

In the case of m2, the compiler doesn't know the value, hence, does not know if it will fit into a byte. You can argue that the method is being called with a value of 'b', but the compiler can't tell if the method will be called from somewhere else too. Hence, the complaint about a possible loss of precision.

Henry
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider this cut-down version of MyClass:


Now it should be clear that the return needs a cast: return (byte)c. There is no knowledge of the value of the actual parameter here. The final just ensures that the local variable c cannot be assigned to within the method body.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is really an interesting question.
Let me explain,

First of all we know a char can be assigned to an int,float,long,double.
Remember,
-----------------

byte-->short-->int-->long-->float-->double
char-->

-----------------
so we can't write,
char c='a'; byte b=c; or short s=c;
all of these will give compile time error.

Again we can write, int i=c; or long l=c; or ..so so.

-----------------
But if the char is decleared as final. then an interesting thing will happen.If the char is decleared final then in compile time the compiler will check its range.
If its range is within the range of the byte or short then we can assign a char into a byte & short respectively.

so, final char c='A'; [ the int value of char c here is 65 which is within the range of byte ].
so, we can write byte b=c; short s=c;

--------------------------------------


Now try with this,
final char c1='\u0094';[ the int value of char c1 here is 148 which is not within the range of byte but short ].
byte b=c1; will give error.
but, short s=c1 will compile fine.


================================


public class MyClass{
public static void main(String[] args){
char c='A';
m1();m2(c);
}
static byte m1() {
final char c='B';
return c;
}
static byte m2(final char c) {return c;}//1
}



In m1(),
final char c='B' [ which int value is 66 ] thats why its not giving any error.

But in m2(final char c) where the char is defined as final but its not been assigned any value so, the copiler is not sure whether it will fit into a byte when casted. so it will throw error.


Hope it helps you.

thnx
PS
[ December 04, 2005: Message edited by: Purujit Saha ]
 
Akshay Singhvi
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ranchers,

I got it.Thankx Henry,Barry,Saha
Regards
Akshay
 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!