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
698 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,620 points
1,328 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
• 59,230 points
863 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 18, 2019 in Java by Frankie
• 9,830 points
18,443 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
927 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,910 points
1,084 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,910 points
1,108 views
+1 vote
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,620 points
1,027 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
1,322 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, 2020 in Java by Sri
• 3,190 points
1,068 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
2,198 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP