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
24 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,520 points
100 views
0 votes
1 answer

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

There are four different ways to create ...READ MORE

answered Oct 16, 2018 in Java by Daisy
• 8,020 points
45 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,590 points
111 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,310 points
24 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,820 points
34 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,820 points
35 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,520 points
34 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,310 points
46 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
22 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
22 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.