java - Why doesn't assigning two concatenated strings to another string work? -
i experimenting string class in java , wanted override tostring() method when came across issue. did -
public class string1 { private string s = "hello world"; public static void main(string[] args) { string1 s1 = new string1(); system.out.println(s1.s.tostring()); } public string tostring() { string1 s2 = new string1(); s2.s = this.s + " lel"; return s2.s; } } output -
hello world.
how modify tostring() method outputs
hello world lel
use s1.tostring() instead of s1.s.tostring()
s instance of string
s1 instance of string1 [your class]
you have defined tostring() string1.
Comments
Post a Comment