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

compile time constant doubt

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class JSC203 {
static byte m1() {
final char c1 = '\u0001';
return c1; // 1
}
static byte m2()
{
final short s1 = 2;
return s1; //2
}
static int m3(final long l) {return l;} // 3
public static void main(String[] args) {
final long l = 4L;
System.out.print(""+m1()+m2()+m3(l));
}}

If 1 and 2 are possible why not 3? I think long is also compile time constant.Pls correct me. Iam getting error "possible loss of precision found:long,required int"
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cant return a value that is broader than the return type.

you cant return long from method declared to return int
but you can return narrower value for eg:

short can fit in int so its ok
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are 1 and 2 possible? Isn't the compiler throwing errors?

I ran the following :


And as expected the compiler threw possible loss of precision errors.


found : char
required: byte
at return c;




found : short
required: byte
at return s;




found : long
required: int
at return x;

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Khun Sudha,

For 1 and 2 error is not thrown because of "final". Because you have declared C1 and S1 as final inside the method, complier is sure that you cant change final value. So there is no error.
But if you declare like 'final short s1 = 128;' then complier will throw error because 128 out of range for byte.
 
sudha javvadi
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I declared long as final too. Was it not a compile time constant as char and byte?
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy!

Originally posted by sudha javvadi:
I declared long as final too. Was it not a compile time constant as char and byte?



These kind of things work with byte, char, short and int.
Not with long.

Yours,
Bu.
 
sudha javvadi
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou all for your replies.
 
sudha javvadi
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou all for your replies.
 
His brain is the size of a cherry pit! About the size of this ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic