The Artima Developer Community
Sponsored Link

Java Buzz Forum
Best Way to Compare Two Strings in Java Alphabetically with Example

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Javin Paul

Posts: 1090
Nickname: javinpaul
Registered: Jan, 2012

Javin Paul is Java Programmer working on Finance domain.
Best Way to Compare Two Strings in Java Alphabetically with Example Posted: Aug 23, 2013 9:03 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Javin Paul.
Original Post: Best Way to Compare Two Strings in Java Alphabetically with Example
Feed Title: Java67
Feed URL: http://www.java67.com/feeds/posts/default?alt=rss
Feed Description: Java and technology tutorials, tips, questions for all programmers.
Latest Java Buzz Posts
Latest Java Buzz Posts by Javin Paul
Latest Posts From Java67

Advertisement
There are multiple way to compare two stings alphabetically in Java e.g. == operator, equals() method or compareTo() method, but which one is the best way to check if two strings are equal or not? Programmers often confused between == operator and equals() method, and think that comparing strings using == operator should be faster than equals() method, and end up using that. Though they are not completely wrong, they often missed point that == operator is designed to compare object equality, not String equality, which is actually defined in equals()method and compare Strings alphabetically. When you compare two strings using == operator, it may or may not return true, especially if you are expecting result based on contents. It will only return true if both reference variables are pointing to same object, like in case of interned string or String literals. Otherwise it will return false, even if content of String remain same. It's one of the coding best practice in Java to use equals() method to check String equality, i.e. to check if two String variable contains same values or not. This method also come with another flavor called equalsIgnoreCase(), which perform case insensitive equality check and should be used to perform case insensitive equality check.
Read more ยป

Read: Best Way to Compare Two Strings in Java Alphabetically with Example

Topic: Coach or Consultant? Agile or not? What am I? Previous Topic   Next Topic Topic: Almost named method arguments in JDK 8

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use