• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

compile time constant doubt

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou all for your replies.
 
sudha javvadi
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou all for your replies.
 
It is no measure of health to be well adjusted to a profoundly sick society. -Krishnamurti Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic