String Comparision JAVA by Ravinder Nath Rajotiya - April 13, 2021April 14, 20210 Share on Facebook Share Send email Mail Print Print Table of Contents Toggle Comparing StringsSteps in string object creation in JAVASimilarily steps for name2 variable creation is:Comparing Strings: Comparing Strings String is created from String class. let us declare two string variables as name1 and name2. String name1=”Peter” String name2=”Peter”; Ravinder Rajotiya Name1 name2 Steps in string object creation in JAVA Step-1 Step-2 First the string object with name “Ravinder” createrd in memory Next the string variable name1 will be created in memory which will hold the address of the text “Ravinder” and not the text itself. Similarily steps for name2 variable creation is: Step-1 Step-2 First the string object with name “Rajotiya” createrd in memory Next the string variable name2 will be created in memory which will hold the address of the text “Rajotiya” and not the text itself. Comparing Strings: We don’t compare strings using relational operators. The simple reason is that the relational operators on strings compare their memory location and nt the value itself. Here are two cases Case-1: String Literals Case-2: User String Input String literals are part of string contant pool, so will always be te same memory address from literal pool. So Literals with exact same spelling are at the same memory location. Thus this method says “name1 equals nam 2” But Normally we get input from the users and not hard coded literals When we get input two strings from user using say JOptionpane.showInputDialog(“Pl enter”); hey are stored at two different memory location even if the strings entered are same. So the following methd is very poorly made program. Cannot compare strings with == operator package com.Example; import javax.swing.*; import java.util.Scanner; public class examResult { public static void main(String[] args) { String name1; String name2; name1 =”Ram” name2 = “Ram” if(name1==name2) { System.out.println(“Name1 is equal to name2”); } else { System.out.println(“Name1 is not equal to name2”); } System.exit(0); } } package com.Example; import javax.swing.*; import java.util.Scanner; public class examResult { public static void main(String[] args) { String name1; String name2; name1 =JOptionPane.showInputDialog(“Please enter name1”); name2 =JOptionPane.showInputDialog(“Please enter name2”); if(name1==name2) { System.out.println(“Name1 is equal to name2”); } else { System.out.println(“Name1 is not equal to name2”); } System.exit(0); } } Output: Name1 is equal to name2 Output: Please Enter name1 Ram Please Enter name-2 Ram Name1 is not equal to name2; Here we entered two names with same value “Ram” but still the system says Name1 is not equal to name2. This is because they are having different addresses and == operator compares addresses not the values We cannot use relational operators to compare string objects. These String variables contains address of the string object and not the object themselves. So if(name1 == name2) will return false even if the two names are with same value as “Ravinder”. Why? Because variables name1 and name2 contain the address for two objects and they are different in memory. Solution: So what is the way of comparing the strings. First method: equals() method: syntax if(name1.equals(name2)) This method compares every single character in string name1 with that in name2. And if every single character is equal, this method will return TRUE on comparison Second method: compareTo() This method will use integer result for comparison If the value =0, name1 will be equal to name2 if the value <0, name1 first & name2 second(in first letter/alphabet) if the value >0, name2 first & name1 second (alhabet of name 1 is smaller than alphabet of name2) Varios Solutions for comparing the strings If (name1.equals(name2)) if(name1.compareTo(name2)==0) Comparing all cases with compareTo() package com.Example; import javax.swing.*; import java.util.Scanner; public class examResult { public static void main(String[] args) { String name1; String name2; name1 =JOptionPane.showInputDialog(“Please enter name1”); name2 =JOptionPane.showInputDialog(“Please enter name2”); if(name1.equals(name2)) { System.out.println(“Name1 is equal to name2”); } else { System.out.println(“Name1 is not equal to name2”); } System.exit(0); } } package com.Example; import javax.swing.*; import java.util.Scanner; public class examResult { public static void main(String[] args) { String name1; String name2; name1 =JOptionPane.showInputDialog(“Please enter name1”); name2 =JOptionPane.showInputDialog(“Please enter name2”); if(name1.compareTo(name2)==0) { System.out.println(“Name1 is equal to name2”); } else { System.out.println(“Name1 is not equal to name2”); } System.exit(0); } } package com.Example; import javax.swing.*; import java.util.Scanner; public class examResult { public static void main(String[] args) { String name1; String name2; name1 =JOptionPane.showInputDialog(“Please enter name1”); name2 =JOptionPane.showInputDialog(“Please enter name2”); if(name1.compareTo(name2)==0) { System.out.println(“Name1 is equal to name2”); } if(name1.compareTo(name2)0) { System.out.println(“Name1 is first thane name2”); } if(name1.compareTo(name2)>0) { System.out.println(“Name2 is first than name 1″); } System.exit(0); } } Share on Facebook Share Send email Mail Print Print