# What happens when compose String with difference sequence?

Stack Overflow Asked by shiwee on August 9, 2020

I am confused by below two segments of codes, what makes the difference?

String s1 = new String("1") + new String("2");
s1.intern();
String s0 = "12";
Assert.assertTrue(s1 != s0);
Assert.assertTrue(s1.equals(s0));

String s20 = new String("2") + new String("1");
s20.intern();
String s21 = "21";
Assert.assertTrue(s21 == s20);
Assert.assertTrue(s21.equals(s20));

let me explain my question, the above codes pass the test without assert exception, that means "s1 != s0" is true, "s21 == s20" is true as well! "s1 != s0" should be false in my understanding!
if i am not clear, below codes may explain well than my words:

String s20 = new String("2") + new String("1");  // "21"
s20.intern();
String s21 = "21";  // "21"
Assert.assertTrue(s21 == s20);  // equal reference
Assert.assertTrue(s21.equals(s20));

s20 = new String("1") + new String("2");  // "12"
s20.intern();
s21 = "12";  // "12"
Assert.assertTrue(s21 != s20);  // NOT equal, why??
Assert.assertTrue(s21.equals(s20));

String is an object, not primitif, == or != compare reference and .equals the value. But you use .intern() but intern() return an String but you can't not assign.

Answered by Anthony Deseille on August 9, 2020

Not completely sure what you want but at least you have different comparisons in the assertions:

Assert.assertTrue(s1 != s0);
// VS.
Assert.assertTrue(s21 == s20);

Answered by drodil on August 9, 2020

## Related Questions

### Animation to play once per session

0  Asked on January 15, 2021 by alejandro-vargas

### postgres random text in jsonb column

2  Asked on January 15, 2021 by ed1t

### Valid way of accessing the address of the one-past-end element of a vector

1  Asked on January 15, 2021 by jean-jol-borter

### Optimize pairings of two types of points with respect to the maximum distance inside any pair

0  Asked on January 15, 2021 by sonya-gold

### SweetAlert2 confirm dialog not working properly in onclick

1  Asked on January 15, 2021 by coffeecode

### wrap children for react component which has children restricted to certain types

1  Asked on January 15, 2021 by danielo515

### How to make TestNG print detail message about the failure

2  Asked on January 15, 2021 by gelin-luo

### How do I get rid of SNI.dll when publishing as a “single file” in Visual Studio 2019?

1  Asked on January 15, 2021 by humanbean

### Strange igraph behaviour, generating duplicate vertices

1  Asked on January 15, 2021 by simon-mills

### Azure Kubernetes – prometheus is deployed as a part of ISTIO not showing the deployments?

1  Asked on January 14, 2021 by karthikeyan-vijayakumar

### angular Property ‘value’ does not exist on type ‘Object’

5  Asked on January 14, 2021 by homealone

### C++ printing float as nan

1  Asked on January 14, 2021 by acarter

1  Asked on January 14, 2021 by shynex

### How to change the entire div content on button click

2  Asked on January 14, 2021 by liel-barouch

### Continuous TypeError when attempting to add integers in a list?

3  Asked on January 14, 2021 by frannmich

### Sorting and paging nested documents

2  Asked on January 14, 2021 by taras-kohut

### How to fetch data from Kafka in NodeJS

0  Asked on January 14, 2021

### JS function addEventListener just works on the first element of my list

2  Asked on January 14, 2021 by dgoldfeder

### How to adjust for loop so that it prints list only once?

1  Asked on January 14, 2021 by benito-cano