• 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Help in Final variables

 
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
 
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?
 
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..
 
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic