wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes help me:what 's the difference among these 3 mothods? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "help me:what Watch "help me:what New topic
Author

help me:what 's the difference among these 3 mothods?

Mike Lin
Ranch Hand

Joined: Oct 29, 2002
Posts: 48
byte m1() {
final short s=2 ;
return s; // 1
}
byte m2() {
final short s ;
return s;
}
byte m3(final short s) {
return s;
}
comile m2()or m3() will complain of:
" possible loss of precision"
but m1() is ok.
why???
I really get sick !


SCJP1.4 <br />Best wishes!<br />中国人!
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
The short S in m1() is what we call a "Compile Time Constant", its value is known at compile time and is unchangiable during run time since the variable is declared final.
2 is in the range of byte, so there will NOT be any loss of precesion at run time, the compiler knows that.
In m1(), m2() although the variables are declared final, they are not assigned a value yet, so maybe they will have the value 140 which when converted to byte from short will be chopped and loses preceision, so the compiler warns.
HTH


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Mike Lin
Ranch Hand

Joined: Oct 29, 2002
Posts: 48
thanks!:-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help me:what 's the difference among these 3 mothods?
 
Similar Threads
Implementing Interface
Argument Scope
Dan's Mock Exam
compile time constant doubt
Compile-time Constants