java - Why have we used double quotes in return i+""; -
when run program after removing +"" return statement of tostring() compile time error comes. no idea why.. please tell why that.
class test { int i; test(int i) { this.i = i; } public int hashcode() { return i; } public string tostring() { return i+""; } public static void main(string []args) { test t1 = new test(100); test t2 = new test(110); system.out.println(t1); system.out.println(t2); } }
the tostring
method's return
type string
, can't
return i;
because returning int
; needs converted string
, & that's
return i+"";
does. can use
return string.valueof(i); // if wanted more "explicit"
Comments
Post a Comment