2

I tried some thing like below :

String s1="test5";
String s2="test"+5;
String s3="test"+s1.length();
System.out.println("s1==s2 "+(s1==s2)+"  s2==s3 "+(s2==s3)+" s1==s3 "+ (s1==s3));

The output is:

s1==s2 true

s2==s3 false

s1==s3 false

Why s2 and s1 are not equal to s3? Why a new refrence is being generated for s3?

PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
Sangam C.
  • 41
  • 4

1 Answers1

0

here the String s2 has always constant value test5. But in the s3 the value is dependent on the String s1. if the length of s1 changes then it will change too thus it is dynamic

stinepike
  • 54,068
  • 14
  • 92
  • 112