How do I compare strings in Java?

Ads
 

How do I compare strings in Java?

How do I compare strings in Java?

View Answers

December 23, 2013 at 6:11 PM


December 23, 2013 at 6:12 PM

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

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

// ... neither are these
new String("test") == new String("test") ==> false 

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

// concatenation of string literals happens at compile time resulting in same objects
"test" == "te" + "st"  ==> true

// but .substring() is invoked at runtime, generating distinct objects
"test" == "!test".substring(1) ==> false

Ads









Related Tutorials/Questions & Answers:
How do I compare strings in Java?
compare two strings in java
Advertisements
How do I upgrade mysql?
How can I do it? .click();
how do i solve this problem?
how do i solve this question?
Java2
How do I decompile Java class files?
How do I initialize a byte array in Java?
difference between java5 and java6 - Java Beginners
How do i do the coding for 'leaving a comment' in java
PureStack question---i dont know how to do this
PureStack question---i dont know how to do this
How do I compile the registration form?
How do I learn Spring Framework?
How do I learn Spring Framework?
How do I study big data?
How do I start learning MongoDB?
How do I generate random number?
how do i grab the url in php?
how do i begin a two dimensional array?
about java1
how do i write a java program for this??
How do I do this program? I'm new to Java programming...
How do I import a CSV file in R?
how I do select from select in hql
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
Write a program that replaces a, e, i, o, u in Java2

Ads