Listlist1 = new ArrayList<>(); List list2 = list1; //Assert both list1 and list2 are same instance Assert.assertSame(list1, list2); //pass
String str1 = "hello"; String str2 = new String("hello"); //new instance //Assert both str1 and str2 are not same instance Assert.assertNotSame(str1, str2); //passIn this example, we have two string objects, `str1` and `str2`, where `str2` is a new instance created using the `new` keyword. Here, we are checking if both `str1` and `str2` are not the same instance using assertNotSame() method. The `assertSame()` and `assertNotSame()` methods are part of the `org.junit.Assert` class that belongs to the `junit` package library.