Written by Sai Sandeep Thota on July 20, 2013 in Java > Programming

Java Program to Compare Two Strings

GET ALERTS:

Get our Latest updates delivered to your mailbox!

In this article we are going to illustrate how to compare two strings in Java. Java Library has a inbuilt function called equals which compares a string with the other. The Syntax for using equals function is : if(string1.equals(string2)).

The above code compares string1 with string2 and enters the true block.

Java Program to Compare Two Strings

import java.util.*;

class compareStrings {
	public static void main(String args[])
	{
		String s1, s2;
		Scanner get = new Scanner(System.in);
		System.out.println("Enter Two strings:");
		s1 = get.nextLine();
		s2 = get.nextLine();
		System.out.println();
		if(s1.equals(s2))
			System.out.println("Strings are Equal.");
		else
			System.out.println("Strings are not Equal!!");	
	}
}

Output:
Java Program to Compare Two Strings

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: