Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public final class MyEqualityTest
- {
- public static void main( String args[] )
- {
- String s1 = new String( "Test" );
- String s2 = new String( "Test" );
- System.out.println( "n1 - PRIMITIVES ");
- System.out.println( s1 == s2 ); // false
- System.out.println( s1.equals( s2 )); // true
- A a1 = new A();
- A a2 = new A();
- System.out.println( "n2 - OBJECT TYPES / STATIC VARIABLE" );
- System.out.println( a1 == a2 ); // false
- System.out.println( a1.s == a2.s ); // true
- System.out.println( a1.s.equals( a2.s ) ); // true
- B b1 = new B();
- B b2 = new B();
- System.out.println( "n3 - OBJECT TYPES / NON-STATIC VARIABLE" );
- System.out.println( b1 == b2 ); // false
- System.out.println( b1.getS() == b2.getS() ); // false
- System.out.println( b1.getS().equals( b2.getS() ) ); // true
- }
- }
- final class A
- {
- // static
- public static String s;
- A()
- {
- this.s = new String( "aTest" );
- }
- }
- final class B
- {
- private String s;
- B()
- {
- this.s = new String( "aTest" );
- }
- public String getS()
- {
- return s;
- }
- }
- String mango = "mango";
- String mango2 = "mango";
- System.out.println(mango != mango2);
- System.out.println(mango == mango2);
- false
- true
- true
- false
- String mango = "mango";
- String mango3 = new String("mango");
- System.out.println(mango != mango3);
- System.out.println(mango == mango3);
- false
- true
- true
- false
- String mango = "mango";
- String mango2 = "mango";
- String mango3 = new String("mango");
- System.out.println(mango != mango2);
- System.out.println(mango == mango2);
- System.out.println(mango3 != mango2);
- System.out.println(mango3 == mango2);
- // mango2 = "mang";
- System.out.println(mango+" "+ mango2);
- System.out.println(mango != mango2);
- System.out.println(mango == mango2);
- System.out.println(System.identityHashCode(mango));
- System.out.println(System.identityHashCode(mango2));
- System.out.println(System.identityHashCode(mango3));
- false
- true
- true
- false
- mango mango
- false
- true
- 17225372
- 17225372
- 5433634
- mango = "mango";
- mango ----> "mango"
- mango2 = "mango";
- mango ----> "mango" <---- mango2
- mango3 = new String("mango")
- mango -----> "mango" <------ mango2
- mango3 ------> "mango"
- mango ---->"mango"
- mango2 ----> "mang"
- mango3 -----> "mango"
- String foo = new String("abc");
- String bar = new String("abc");
- if(foo==bar)
- // False (The objects are not the same)
- bar = foo;
- if(foo==bar)
- // True (Now the objects are the same)
- String foo = new String("abc");
- String bar = new String("abc");
- if(foo.equals(bar))
- // True (The objects are identical but not same)
- enum FooEnum {A, B, C}
- public boolean compareFoos(FooEnum x, FooEnum y)
- {
- return (x == y);
- }
- String w1 ="Sarat";
- String w2 ="Sarat";
- String w3 = new String("Sarat");
- System.out.println(w1.hashCode()); //3254818
- System.out.println(w2.hashCode()); //3254818
- System.out.println(w3.hashCode()); //3254818
- System.out.println(System.identityHashCode(w1)); //prints 705927765
- System.out.println(System.identityHashCode(w2)); //prints 705927765
- System.out.println(System.identityHashCode(w3)); //prints 366712642
- if(w1==w2) // (705927765==705927765)
- {
- System.out.println("true");
- }
- else
- {
- System.out.println("false");
- }
- //prints true
- if(w2==w3) // (705927765==366712642)
- {
- System.out.println("true");
- }
- else
- {
- System.out.println("false");
- }
- //prints false
- if(w2.equals(w3)) // (Content of 705927765== Content of 366712642)
- {
- System.out.println("true");
- }
- else
- {
- System.out.println("false");
- }
- //prints true
- String s1 = new String("Ali");
- String s2 = new String("Veli");
- String s3 = new String("Ali");
- System.out.println(s1.hashCode());
- System.out.println(s2.hashCode());
- System.out.println(s3.hashCode());
- System.out.println("(s1==s2):" + (s1 == s2));
- System.out.println("(s1==s3):" + (s1 == s3));
- System.out.println("s1.equals(s2):" + (s1.equals(s2)));
- System.out.println("s1.equal(s3):" + (s1.equals(s3)));
- /*Output
- 96670
- 3615852
- 96670
- (s1==s2):false
- (s1==s3):false
- s1.equals(s2):false
- s1.equal(s3):true
- */
- class A
- {
- int id;
- String str;
- public A(int id,String str)
- {
- this.id=id;
- this.str=str;
- }
- public static void main(String arg[])
- {
- A obj=new A(101,"sam");
- A obj1=new A(101,"sam");
- obj.equals(obj1)//fasle
- obj==obj1 // fasle
- }
- }
- class A
- {
- int id;
- String str;
- public A(int id,String str)
- {
- this.id=id;
- this.str=str;
- }
- public boolean equals(Object obj)
- {
- A a1=(A)obj;
- return this.id==a1.id;
- }
- public static void main(String arg[])
- {
- A obj=new A(101,"sam");
- A obj1=new A(101,"sam");
- obj.equals(obj1)//true
- obj==obj1 // fasle
- }
- }
- public class StringPool {
- public static void main(String[] args) {
- String s1 = "Cat";// will create reference in string pool of heap memory
- String s2 = "Cat";
- String s3 = new String("Cat");//will create a object in heap memory
- // Using == will give us true because same reference in string pool
- if (s1 == s2) {
- System.out.println("true");
- } else {
- System.out.println("false");
- }
- // Using == with reference and Object will give us False
- if (s1 == s3) {
- System.out.println("true");
- } else {
- System.out.println("false");
- }
- // Using .equals method which refers to value
- if (s1.equals(s3)) {
- System.out.println("true");
- } else {
- System.out.println("False");
- }
- }
- }
- Long a = 10L;
- Long b = 10L;
- if (a == b) {
- System.out.println("Wrapped primitives behave like values");
- }
- ArrayList<Long> c = new ArrayList<>();
- ArrayList<Long> d = new ArrayList<>();
- c.add(a);
- d.add(b);
- if (c == d) System.out.println("No way!");
- if (c.equals(d)) System.out.println("Yes, this is true.");
- String string1 = "Ravi";
- String string2 = "Ravi";
- String string3 = new String("Ravi");
- String string4 = new String("Prakash");
- System.out.println(string1 == string2); // true because same reference in string pool
- System.out.println(string1 == string3); // false
- System.out.println(string1.equals(string2)); // true equals() comparison of values in the objects
- System.out.println(string1.equals(string3)); // true
- System.out.println(string1.equals(string4)); // false
- assert "ab" == "a" + "b";
- Integer i = 1;
- Integer j = i;
- assert i == j;
- assert new String("a") != new String("a");
- Integer i = 128;
- Integer j = 128;
- assert i != j;
- assert (new String("a")).equals(new String("a"));
- Integer i = 128;
- Integer j = 128;
- assert i.equals(j);
- HashMap<Cat, String> cats = new HashMap<>();
- Cat cat = new Cat("molly");
- cats.put(cat, "This is a cool cat");
- System.out.println(cats.get(new Cat("molly"));
Add Comment
Please, Sign In to add comment