If statement getting true value [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
My program is using the following code
String mobile="";
if(mobile!="")
System.out.println("++++++");
System.out.println("------");
But if statement always set true. how it is possible? how to correct the code to get false for if condition?

try this way
if(!(mobile.equals("")))
there are difference between == and .equals()
As whoAmI has suggested its better to
if(!("".equals(mobile)))
because it can handle mobile as null

you can't use "!=" on strings. This only compares the references of the strings. If you want to compare the content of the strings you need to use equals
if(!("".equals(mobile)))

Related

Why do I have to use SomeRandomString.equals("something") instead of SomeRandomString == "something" in Java? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Why doesn’t == work on String? [duplicate]
(7 answers)
Closed 2 years ago.
I'm relatively new to Java so I'm kind of lost here. Is it wrong when you are trying to compare two strings just use the identical symbol "==" instead of using a method for checking if two strings are identical. Because for what I have tried the first option doesn't work and I'm very curious on the reason behing it
In java when you use String, you work with reference.
String one = "one";
String two = "two";
if(one == two)
// true if references `one` and `two` point to the same object
if(one.equals(two))
// true if strings `one` and `two` are equals (it could be to different objects)

How to Compare two strings in android with optimization? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I already tried with string.equal function which will compare two strings, but what will be optimize way of doing same.
Suggestions are welcome.
Hi all got it may help others What's the quickest way to compare strings in Java?
Thanks Saurabh for sharing link.
According to me, You should use the equals() method of the String class to compare Strings.
if (xyz.equals("something")) {
//Do what you want if string is equal...
}
see this also..

Comparing strings in onPostExecute (resulting from mysql operation) [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I have been trying to connect to a mysql database and it was succesfull. In the onPostExecute method, I get a result which seems to me exactly the same as the real input. In this example the emailadress jb#jb.jb. Logcat shows both the real input as the result. Yet my code doesn't agree that it is the same. Any advice?
EDIT: This link apparently gave the answer: answer on the question
You have to use String.equals() instead of == to compare Strings. check this link

What's wrong with this Java Coded if STatement? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I am not a professional programer. I am still learning, so my code is a little basic right now.
Scanner UserInput = new Scanner(System.in);
String UserChoose = UserInput.next();
if (UserChoose=="Quit"){
I have deduced that there is something missing in the if statement, but I cannot figure out what. Can someone please tell me what I am missing? I have been searching online for an hour with no luck.
To compare objects in java use .equals() method instead of "==" operator
Replace if (UserChoose=="Quit"){ with if (UserChoose.equals("Quit")){
Common mistake, use UserChoose.equals("Quit") to compare strings.
Since String is an object, using == will likely compare the memory location the 2 strings or something that would always result with false.
if (UserChoose.equals("Quit")){
In java, the default == operator compare if they both are the same object, even if the content is the same, if the object reference is not the same, it isn't ==.
See this link for more complete explanation: http://blog.enrii.com/2006/03/15/java-string-equality-common-mistake/

Comparing strings - Java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I compare strings in Java?
I'm new to Java. And I've following problem:
string s = "someword";
if (s == "someword")
// do something
Sometimes doesn't work for me. Don't know why.
Thanks for responds.
In Java == compare reference. Use .equals() for compare value.
Duplication of this: How do I compare strings in Java?

Categories

Resources