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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -