What are the different ways of comparing Strings in Java?

0 votes
I am new in Java, I know this might be very easy for most of you but as a begineer I am finding it difficult to understand the different methods of comparing strings.
Mar 5, 2019 in Java by Radhika
170 views

1 answer to this question.

0 votes

The different ways of comparing string in Java are as follows:

Using user-defined function

  1. if (string1 > string2) - it will return a positive value.
  2. if both the strings are equal lexicographically
    i.e.(string1 == string2) it will return 0.
  3. if (string1 < string2) it will return a negative value.

The value is calculated as (int)str1.charAt(i) – (int)str2.charAt(i)

Using String.equals()

Using String.equalsIgnoreCase()

Using Objects.equals()

Using String.compareTo()

answered Mar 5, 2019 by Wasim

Related Questions In Java

0 votes
1 answer

What is the concept of Immutability for strings in Java ? Why are strings immutable ?

According to Effective Java, chapter 4, page 73, ...READ MORE

answered May 11, 2018 in Java by Rishabh
• 3,580 points
447 views
0 votes
2 answers

What are all the different ways to create an object in Java?

There are different ways you could do this ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 58,210 points
221 views
0 votes
1 answer

What are the different methods of session management in servlets?

Session is a conversational state between client ...READ MORE

answered Feb 17, 2019 in Java by Frankie
• 9,810 points
5,072 views
0 votes
1 answer

what are the ways in which a list can be iterated

  There are 5 ways to iterate over ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,370 points
206 views
0 votes
2 answers

Store String inside a File using Java

We can use Apache Commons IO. It ...READ MORE

answered Jul 20, 2018 in Java by Sushmita
• 6,890 points
118 views
0 votes
3 answers

Adding text to a file using Java

try { final Path ...READ MORE

answered Sep 6, 2018 in Java by Sushmita
• 6,890 points
185 views
0 votes
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,580 points
238 views
0 votes
1 answer

How to split Strings by space in Java ?

You can use split() method. str = "Hello ...READ MORE

answered May 16, 2018 in Java by sharth
• 3,370 points
252 views
0 votes
2 answers

What are the different types of constructor in StringBuilder class?

StringBuilder  we 4  as mentioned below. READ MORE

answered Sep 1 in Java by Sri
• 2,970 points
206 views
0 votes
1 answer

How to get rid of TLE in Java?

The TLE (Time limit exceed) problem occurs ...READ MORE

answered Mar 5, 2019 in Java by Disha
803 views