Difference between concat() and concat operator ‘+’ in java

When there is a need to concatenate two or more strings in java we generally use the concatenation operator '+'. But, for the same purpose we should prefer using concat() method from String class. After a closer look at the size of .class files which is generated after compiling the code, it is revealed that the size of the .class file containing '+' operator exceeds by a greater margin than the .class file performing the same concatenation but by using concat() method. ConcatFunDemo uses the concat() of String class: class ConcatFunDemo { public static void main(String[] s) { String s1 = "www."; String s2 = "pcsalt."; String s3 = "com"; System.out.println(s1.concat(s2).concat(s3)); } } ConcatOpDemo uses the concatenation operator (+): class ConcatOpDemo { public static void main(String[] s) { String s1…
Read More