Skip to content

Instantly share code, notes, and snippets.

@gauravkukade
Created August 21, 2019 11:02
Show Gist options
  • Save gauravkukade/6cca0a7aaa948f0cbac01d2f6958358f to your computer and use it in GitHub Desktop.
Save gauravkukade/6cca0a7aaa948f0cbac01d2f6958358f to your computer and use it in GitHub Desktop.
A Java program to compare strings using == operator. == operator ckecks weather both the strings referring to the same String Object. The code snippet is used at https://coderolls.com/comparing-strings-in-java
/**
* A Java program to compare strings using == operator.
*
* == operator ckecks weather both the strings referring
* to the same String Object.
*
* @author Gaurav Kukade at coderolls.com
*/
public class CompareUsingEqualsToOperator {
public static void main(String[] args) {
String firstString = "coderolls";
String secondString = "javablog";
String thirdString = "coderolls";
// creating new String object with the same value as firstString or thirdString
String fourthString = new String("coderolls");
System.out.println("Comparing strings using == operator \n");
System.out.print("firstString == secondString : ");
System.out.println(firstString == secondString);
/*
* Here firstString and thirdString is referring to the same String object
* hence it will print 'true'.
*/
System.out.print("firstString == thirdString : ");
System.out.println(firstString == thirdString);
/*
* Here firstString and fourthString have same value
* but they are referring to the different String object.
*
* hence it will print 'false'
*/
System.out.print("firstString == fourthString : ");
System.out.println(firstString == fourthString);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment