Java — Heap & String Constant Pool (SCP) | Code Factory

String s1 = new String("Code Factory");
String s2 = new String("Code Factory");
String s3 = "Code Factory";
String s4 = "Code Factory";
String s = new String("Code");
s.concat("Factory");
s = s.concat("Hello");
String s1 = new String("one");
s1.concat(", two");
String s2 = s1.concat(", three");
s2.concat(", four");
System.out.println(s1); // one
System.out.println(s2); // one, three
String s1 = new String("code factory");
String s2 = new String("code factory");
System.out.println(s1 == s2); // false
String s3 = "code factory";
System.out.println(s1 == s3); // false
String s4 = "code factory";
System.out.println(s3 == s4); // true
String s5 = "code" + " factory";
// s5 will perform at compile time and JVM is responsible
System.out.println(s4 == s5); // true
String s6 = "code";
String s7 = s6 + " factory";
// s7 will perform at runtime
System.out.println(s4 == s7); // false
final String s8 = "code";
// s8 will perform at compile time
String s9 = s8 + " factory";
// s9 will perform at compile time
System.out.println(s4 == s9); // true

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store