• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

return type

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

Here is the code I am trying to compile :

class JSC202 {
static byte m1() {final short s1 = 2; return s1;} // 1
static byte m2(final short s2) {return s2;} // 2
public static void main(String[] args) {
short s3 = 4;
System.out.print(""+m1()+m2(s3)); // 3
}}

Now the compiler gives an error at line 2 : possible loss of precision found short, required byte.

When I replace line 2 with " static byte m2(final short s2) { return (byte)s2;}, it compiles fine.

Why is it complaining precision loss at line 2 only why not at line 1 also since im trying to return short on line 1 also.

Thanks,

Raghav
 
Raghav Aggarwala
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why is it doing an implicit cast at line 1?

if I change line 2 return with " return 4; " it doesn't compaint.

Can anyone explain..

thanks,

raghav
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can refer to K&B's guide book - Chapter 3,Section 1-Literals, Assignments, and Variables (Exam Objectives 1.3 and 7.6)

It explain clearly about your questions.

byte b = 3; //No problem, 3 fits in a byte
byte c = 8; // No problem, 8 fits in a byte
byte d = b + c; // Should be no problem, sum of the two bytes
// fits in a byte

The last line won't compile! You'll get an error something like this:

TestBytes.java:5: possible loss of precision
found : int
required: byte
byte c = a + b;
^

We tried to assign the sum of two bytes to a byte variable, the result of which (11) was definitely small enough to fit into a byte, but the compiler didn't care. It knew the rule about int-or-smaller expressions always resulting in an int. It would have compiled if we'd done the explicit cast:

byte c = (byte) (a + b);




Since it is not clear for compile to know your value of s2 in line 2 but it knows the value of s1 in line 1.

Am I clear?
 
Raghav Aggarwala
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. It's clear now.

Thanks,

Raghav
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At line 1,
since it is a final variable with a value which is within the limits of byte ( <128) the compiler did not complain. Whereas at line 2, since the value is not known, there is a possibility that some value higher than what a byte could hold could be passed at runtime. Therefore as a precautionary measure the compiler complains. Again, when you casted it the compiler is assured of a value in byte.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class JSC202 {
static byte m1() {final short s1 = 2; return s1;} // 1
static byte m2(final short s2) {s2=20;return s2;} // 2
public static void main(String[] args) {
short s3 = 4;
System.out.print(""+m1()+m2(s3)); // 3
}}

I get the compilation error though i assigned value to s2. any thoughts!!!
 
Sam Sunamin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry. I make a mistake. I am not sure what's going on after you changed the code. I just checked on jdk. It reports compile error on return s2 not on s2=5.
[ April 12, 2007: Message edited by: Sam Sunamin ]
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
iam confusing ,, i thought there is no error at all ,

Whereas at line 2, since the value is not known, there is a possibility that some value higher than what a byte could hold could be passed at runtime.



at line 2 value known for the compiler
, s2 still final and you can't assign it in method behavior ,



where is the problem since s2 in final and the compiler know the value<128

??
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arad,
At line 2, s2 is in the method's argument, which can be passed a value through a method call even though it is final but cannot be assigned a value inside the method. At line 1, it worked because both declaration and assignment happened on the same line inside the method.

(modified comment)
Local final variables can be declared in one of two ways

In both cases, after the very first assignment(initialization) no further assignments are allowed.

Final variables appearing as method's argument can be passed a value through a method call.


Refer to an earlier post for details.
Earlier Post

[ April 12, 2007: Message edited by: M Krishnan ]
[ April 12, 2007: Message edited by: M Krishnan ]
 
Sam Sunamin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,M Krishnan :

Please check your code using JDK.

//following is allowed
final int i;
i =100;
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess I mixed it up with class instance variable, sorry about that.
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I fixed my earlier comment to avoid any confusion.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic