Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

How to compare two Strings in Java

Author: Infocampus Bangalore
by Infocampus Bangalore
Posted: Nov 24, 2018

String comparison could be a common programming task, and Java provides many ways that to compare two strings in Java. The string could be a particular category in Java, String is immutable, and it’s used tons in each single Java program ranging from simple check to enterprise Java application.

Strings in Java are changeless. Which means whenever you are trying to modify the string you get a replacement instance? You cannot alter the original string. This has been done so that these string instances may be cached. A typical program contains tons of string references and caching these instances will decrease the memory footprint and increase the performance of the program.

Here are four samples of comparing String in Java

1. String comparison using the equals method

2. String comparison using the equalsIgnoreCase method

2. String comparison using the compareTo method

4. String comparison using the compareToIgnoreCase method

Compare two String using equals technique in Java

Equals () method compare 2 Strings for content equality. Thus if two string contains the same letters, in the same order and the same case they're going to be equal by equals () technique. Equals () procedure is outlined in Object category, and String category overrides that for character primarily based comparison. Check five tips to override equals () method in Java for additional details on equals and its implementation.

Compare String using the equalsIgnoreCase technique in Java

equalsIgnoreCase is additional liberal than equals and compare two strings ignoring their case. Thus if 2 String contains the same characters and in same order despite their situation, e.g. lower case, upper case, Capital case or camel case they're going to be equal by equalsIgnoreCase. For example "sony" and "SONY" 2 Strings Advanced Java Training in Bangalore are same by equalsIgnoreCase, and it'll come back true, however "Sony" and "Samsung" won't be same, and it'll come back false as a result of they do not contain same characters.

Comparing String using compareTo

CompareTo square measure actual comparison technique in contrast to equals ()and equalsIgnoreCase() and tell America whether or not 2 Strings are lexicographically equal, precedes or follows one another. If you would like to type Strings lexicographically compareTo () technique is employed. This is often additionally known as cosmos of String. Returns zero if 2 Strings are same, but zero if vocation string comes before Java Courses in Bangalore argument string and more significant than zero if calling string comes later than argument string as shown in the example below.

Compare String using compareToIgnoreCase

Similar to compareTo () technique with ignoring case like equalsIgnoreCase() and come back same values as came back by compareTo throughout String comparison.

Don't use "==" for String comparison

Many Java computer users make the mistake of using "==" for string comparison. Since String is changeless in Java and maintained in String pool 2 String literal refer same String object which provides a sense that "==" may be wont to compare string that is wrong. Continuously use equals () technique for equality check and compare To method for actual string comparison.

Another way of scrutiny String is writing custom Comparator in Java. Write your comparison logic in compare () technique so you'll use that logic to match two strings.

Conclusion:

In this quick tutorial, we tend mentioned alternative ways of comparing Strings. Java includes a String pool under that Java manages the memory allocation for the String objects.

Author:

Java Training in Bangalore has been designed as per the latest business trends and keeping in mind the advanced Java course content and info supported the skilled demand of the student.

Java Training Institute in Bangalore Marathahalli. Join us 100% Job help.

Contact: 9738001024

Visit: http://infocampus.co.in/java-training-bangalore.html

About the Author

Searching for center propelled java/j2EE training in Bangalore Marathahalli, a short time later your first plausibility to be an Infocampus.

Rate this Article
Leave a Comment
Author Thumbnail
I Agree:
Comment 
Pictures
Author: Infocampus Bangalore

Infocampus Bangalore

Member since: Dec 28, 2015
Published articles: 81

Related Articles