aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help in Final variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help in Final variables" Watch "Help in Final variables" New topic
Author

Help in Final variables

Mani Venkata Kanth
Ranch Hand

Joined: Aug 21, 2006
Posts: 39
class a{
static byte m1() {
final char c = '\u0001';
return c; // 1
}
static byte m3(final char c) {return c;} // 2
public static void main(String[] args) {
char c = '\u0003';
System.out.print(""+m1()+m3(c));
}}

Explain output.... output is Compile time error at 2
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
What is the compile error? What does it mean to you?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643

In method m1 final char c at line 3 is compile time constant
so c is within range of byte so there is no problem while
returning c at line 4

At line 7 you are returning char from method m3
But method m3 return byte.
Keep in mind final char c at line 6 which you are returning
at line 7 is constant "but not compile time constant".
This is why there is "possible loss of precision"
error at line 7.
Mani Venkata Kanth
Ranch Hand

Joined: Aug 21, 2006
Posts: 39
---------- Java Compiler ----------
a.java:6: possible loss of precision
found : char
required: byte
static byte m3(final char c) {return c;} // 2
^
1 error

Output completed (2 sec consumed) - Normal Termination
------------------------------------------------------------
why doesnt the error occur at //1 ???
Mani Venkata Kanth
Ranch Hand

Joined: Aug 21, 2006
Posts: 39
Thanks... gowher amin naik .. for clearing my doubt..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help in Final variables