Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help in Final variables

 
Mani Venkata Kanth
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the compile error? What does it mean to you?
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
---------- 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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks... gowher amin naik .. for clearing my doubt..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic