In this Java example we will see how Java String is compared with another Java String object or Java Object in Java.
String class in Java have following methods to compare Java Strings.
int compareTo( String anotherString )
– compare two string based upon the unicode value of each character in the String.
Returns negative int if first string is less than another
Returns positive int if first string is grater than another
Returns 0 if both strings are same.int compareTo( Object obj )
– Behaves exactly likecompareTo ( String anotherString)
if the argument object is of type String, otherwise throwsClassCastException
.int compareToIgnoreCase( String anotherString )
– Compares two strings ignoring the character case of the given String.
/** * Java String compare example by codebind.com. */ public class JavaStringCompare { public static void main(String args[]) { String str = "Hello World"; String anotherString = "hello world"; Object objStr = str; /* compare two strings, case sensitive */ System.out.println(str.compareTo(anotherString)); /* compare two strings, ignores character case */ System.out.println(str.compareToIgnoreCase(anotherString)); /* compare string with object */ System.out.println(str.compareTo(objStr.toString())); } } /* Output: -32 0 0 */
- Java Simple Programs And Examples
- Java Example – Java Hello World Example
- Java Example – Math and Arithmetic Operators in Java
- Java Example – Variables and Types in Java
- Java Example – Scanner class and Getting User Input using Java
- Java Example – The If-Else If Statement, Nested If Statements, Logical Operators
- Java Example – Arrays in Java Example
- Java Example – ListIterator in Java Example
- Java Example – How to get current timestamp using Java
- Java Example – HashSet in Java with Example
- Java Example – How to read file in Java using BufferedReader
- Java Example – How to get Current Directory in Linux/Windows
- Java Example – Program to reverse an array or string
- Java Example – Sort String Array
- Java Example – Comparing two strings
- Java Example – String concatenation (join strings)
- Java Example – Java String Contains example
- Java Conversion
- Java Example – Convert String to int
- Java Example – Convert Date to String
- Java Example – Convert String to Character Array
- Java Example – Convert into string
- Java Example – Convert ArrayList to String Array
- Java Example – Convert Char Array To String
- Java Example – String Array To List
- Java Sorting algorithms & Techniques
- Java Example – Bubble Sort Algorithm
- Java Example – Insertion Sort Algorithm
- Java Example – Selection Sort Algorithm
- Java Example – Quick Sort Algorithm
- Java Example – Merge Sort Algorithm
- Java Handling Files
- Java I/O – Check If File Path Absolute or not
- Java I/O – Get file name and file path
- Java I/O – Get parent directory of the file or directory
- Java I/O – How to write to a file using BufferedWriter
- Java I/O – How to write to a file using FileOutputStream
- Java I/O – Check file permission and Set file permission
- Java I/O – Read File Using Java BufferedInputStream
- Java I/O – How to create a file in Java
Leave a Reply