- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 20
Lesson 20: How to compare string values in Java
In the previous lesson, we looked at how we can convert string type values into uppercase or lowercase.
In this lesson, we shall learn how we can compare values of string type using inbuilt methods provided in Java programming language.
When we compare string values in Java, all Java do is to compare the Unicode hexadecimal values of the passed string values.
We are going to use an inbuilt Java method called compareTo() to compare two string values and determine if they are equal or not.
Let us write a program to compare strings “read” and “lead”. Create another class in java, call it CompareStrings and write the following code and run. You can go ahead and use the defined variables or you can define your own string variables.
Later, you can interchange and modify these string variables and compare again to see how java compare mixed cases strings i.e uppercase and lowercase.
Java program to compare string values
In the above program, we have created two string variables that store our two string values “read” and “lead”. We have then used the method compareTo() to compare the two strings and store the returned value in another variable named myresult.
Using the IF …ELSE statement, we are checking the value returned by the method compareTo(). If the value returned is a zero then the two strings are equal i.e. are the same.
Any other value returned will mean the strings are not the same. Now, change the string “read” to be “lead” and run the program again. You will see that this time you will get a different output.
Java program to compare string values
package myfirstprogram; import javax.swing.JOptionPane; public class CompareStrings { public static void main(String[] args) { int myresult; String mystring1 = "Lead"; //Create string variables String mystring2 = "lead"; myresult = mystring1.compareTo(mystring2); //Compare strings if(myresult == 0){ JOptionPane.showMessageDialog(null, "The two strings are the same"); }else{ JOptionPane.showMessageDialog(null, "The two strings are NOT the same"); } } }
Try the comparison between “Lead” and “lead” i.e. L in uppercase and l in lowercase. Because uppercase L has a different Unicode hexadecimal value from lowercase l, the method compareTo() returns a different value other that zero meaning that the two strings are not equal because string with uppercase characters and another with lowercase characters are not the same.
NB: If you need to compare two strings but ignore case sensitivity, you can use another inbuilt Java method called compareToIgnoreCase().
Write another Java program using the above method to compare strings but this time ignoring case sensitivity. In the next lesson we shall look at how we can use another inbuilt Java method called indexOf() to determine if a given character is appearing in a given string.
<< Lesson 19 | Lesson 21 >>
Related hubs...
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 6
In this lesson we’ll learn about another useful class for accepting user inputs and displaying results. This class is called JOptionPane class and is located in the javax.swing library. - Programming In Java - A Step By Step Tutorial For Beginners: Lesson 7
IF statement executes the code only if the condition specified is met i.e. is TRUE, otherwise if the condition is not met, i.e. it is FALSE, the code will not be executed. - Programming In Java - A Step By Step Tutorial For Beginners: Lesson 10
The FOR loop is one of the most common looping controls used in Java. The FOR loop forces the program to repeat a statement or a group of statements a specified number of times. - Programming In Java - A Step By Step Tutorial For Beginners: Lesson 1
Java is increasingly becoming the world’s programming language of choice. It has been used in developing desktop applications, client-server applications and even mobile phone applications including games.