Categories: Uncategorized

Learn Java – How To Compare Strings

The String class provides several methods for comparing strings. When a comparison is made between strings, the compiler compares the numbers of the objects, because each letter is represented by a number.

Comparisons with the equality operator (==) are used to compare values of primitive types; the result is true if both values are identical. If references are compared with the equal sign (==), the result is true if the two references indicate the same object in memory.

At other hand, .equals () is a method of the Object class, which compares the values of the literals stored by objects, so this method should be used to compare the literal values of type String variables.

Below some examples of comparisons looking for an exact match:

// These two have the same value
new String("test").equals("test") 
// Returns true

//... but they are not the same object
new String ( "test") == "test" 
// Returns false

//... Neither are these
new String("test") == new String("test") 
// Returns false

//... But these are because literals are interned by
// the compiler and thus refer to the same object
"test" == "test"
// Returns true

//... But you shouldnt really just call Objects.equals ()
Objects.equals("test",new String("test")) 
// Returns true
Objects.equals(null,"test") 
// Returns false

 

Other forms of comparison

Going a bit beyond the exact comparison of Strings, we have other interesting forms of comparison:

Case insensitive

System.out.println("STR".equalsIgnoreCase("str"));
//Returns true

 

A string that is contained in another

System.out.println("STR ### ###"contains("STR").);
// Returns true

 

Which string is “bigger” than the other?

System.out.println ("str1".compareTo("str2"));
//Returns -1 because "str1" is smaller "str2"
or
System.out.println("str1".compareToIgnoreCase("STR2"));
//Returns -1, Ignoring the capitalization

The compareTo method returns:

1 if the first String is bigger than the second

0 if they are equal

-1 if the first String is smaller than the second

Starts with

System.out.println("str1".startsWith("str"));
//Returns true, because "str1" begins with "str"

 

Ends with

System.out.println("str1".endsWith("r1"));
// Returns true because "str1" ends with "r1"

 

If you want to explore more, visit our Java edu & tutorials section! Below are some examples:

You can also follow some of the broadcasters who program in Java, like the ones below:

canal2048

Benjamin_Tennyson

Another cool way to find out interesting things about Java is to access our project page!

Dr. Michael J. Garbade

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.

Recent Posts

Blockchain in Elections: A Leap Toward Transparent Democracy

In 2024 we're witnessing a critical point in democratic technology: the integration of blockchain and…

3 weeks ago

Win Big with Our Amazon Fire Max 11 & AirPods Pro Giveaway!

We’re thrilled to announce an exciting opportunity for you to win not one but two…

2 months ago

Unleashing Potential: How Education Ecosystem Transforms Learning into Real-World Success

Acquiring practical skills is crucial for career advancement and personal growth. Education Ecosystem stands out…

4 months ago

The Role of Artificial Intelligence in Modern Software Development

Artificial Intelligence (AI) has been making significant strides in various industries, and the software development…

7 months ago

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

8 months ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

9 months ago