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 in Java by Radhika
60 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 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,540 points
182 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 in Java by Sirajul
• 36,620 points
86 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 in Java by Frankie
• 9,810 points
958 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,350 points
59 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,880 points
48 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,880 points
57 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,540 points
76 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,350 points
87 views
0 votes
1 answer

What are the different types of constructor in StringBuilder class?

There are different types of StringBuilder constructor ...READ MORE

answered Mar 5 in Java by Mariyam
57 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 in Java by Disha
227 views