Write a Java Program to Compare Strings

Java Program to Compare Strings

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to Compare Strings
Java Program to Compare Strings

Write a Java Program to Compare Strings

PROGRAM: Java Program to Compare Strings

/*Java Program to Compare Strings*/

1: Compare two strings

public class CompareStrings {

public static void main(String[] args) {

String style = “Bold”;
String style2 = “Bold”;

if(style == style2)
System.out.println(“Equal”);
else
System.out.println(“Not Equal”);
}
}

2: Compare two strings using equals()

public class CompareStrings {

public static void main(String[] args) {

String style = new String(“Bold”);
String style2 = new String(“Bold”);

if(style.equals(style2))
System.out.println(“Equal”);
else
System.out.println(“Not Equal”);
}
}

3: Compare two string objects using ==

public class CompareStrings {

public static void main(String[] args) {

String style = new String(“Bold”);
String style2 = new String(“Bold”);

if(style == style2)
System.out.println(“Equal”);
else
System.out.println(“Not Equal”);
}
}

4: Different ways to compare two strings

public class CompareStrings {

public static void main(String[] args) {

String style = new String(“Bold”);
String style2 = new String(“Bold”);

boolean result = style.equals(“Bold”); // true
System.out.println(result);

result = style2 == “Bold”; // false
System.out.println(result);

result = style == style2; // false
System.out.println(result);

result = “Bold” == “Bold”; // true
System.out.println(result);
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *