java - Overloading methods -
i saw below question posted on site.
"what happens when pass int arguments overloading method having float as parameter 1 method , having double param".
i thought understood concept , wrote code:
public class testclass { public static void main(string args[]) { testclass t=new testclass(); t.sum(1/4); } void sum(double d) { system.out.println("double==="+d); } void sum(int i) { system.out.println("integer==="+i); } void sum(short s) { system.out.println("short==="+d); } } according understanding explained on site (as mentioned above), thought print short===0, surprise prints integer===0. can 1 explain me?
if don't explicitly tell compiler types of 1 , 4, assumes of type int. then, / operator apply integer partition , produce int (which 0.)
after that, method specific integer parameter type invoked. in case, sum(int i).
if want invoke of other overloaded methods, have explicitly:
do cast. example,sum((short) (1/4)); invoke sum(short s) due cast. point type of operands. example, sum(1d/4) invoke sum(double d), since 1d/4 result double java methods method-overloading
No comments:
Post a Comment