java - Concatenating chars to a String - Using StringBuilder's append() -


i doing research on concatenating char primitive values form string , came across post:

concatenate chars form string in java

i understand correct way of producing final string value use tostring() method, how come if not use method, still same output. have thought following code output heap address of object sb still prints 'ice'.

thank you.

public class charstostring {      public static void main (string args[]) {      char a, b, c;     = 'i';     b = 'c';     c = 'e';      stringbuilder sb = new stringbuilder();     sb.append(a);     sb.append(b);     sb.append(c);      system.out.println(sb);     }    } 

you need system.out.println(sb); does. calls in class java.io.printstream method. (because stringbuilder extends object)

 public void println(object x) {         string s = string.valueof(x);         synchronized (this) {             print(s);             newline();         }     } 

and, string.valueof(x) calls tostring() method on stringbuilder.

public static string valueof(object obj) {     return (obj == null) ? "null" : obj.tostring(); } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -