To see whether two String objects have matching values, an equals() method of the class is used. - Java Object Oriented Design

Java examples for Object Oriented Design:equals method

Description

To see whether two String objects have matching values, an equals() method of the class is used.

Demo Code

public class Main {
    public static void main(String[] arguments) {
        String str1, str2;/*  www . j a v a2  s  .  c o  m*/
        str1 = "this is a test.";
        str2 = str1;

        System.out.println("String1: " + str1);
        System.out.println("String2: " + str2);
        System.out.println("Same object? " + (str1 == str2));

        str2 = new String(str1);

        System.out.println("String1: " + str1);
        System.out.println("String2: " + str2);
        System.out.println("Same object? " + (str1 == str2));
        System.out.println("Same value? " + str1.equals(str2));
    }
}

Result


Related Tutorials