String Comparison In Java
Chapter:
String Handling
Last Updated:
03-08-2016 18:59:42 UTC
Program:
/* ............... START ............... */
public class JavaStringComparison {
public static void main(String args[]) {
String s1 = "JavaScan";
String s2 = "JavaScan";
String s3 = new String("JavaScan");
String s4 = "Java";
System.out.println(s1.equals(s2));// true
System.out.println(s1.equals(s3));// true
System.out.println(s1.equals(s4));// false
}
}
/* ............... END ............... */
Output
Notes:
-
The String equals() method compares the original content of the string.
- There are three ways to compare string in java:
- 1. By equals() method.
- 2. By = = operator.
- 3. By compareTo() method.
Tags
String Comparison, Java