Java - Compare item of String array with a String [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
String.equals versus == [duplicate]
(20 answers)
Closed 8 years ago.
I really don't understand what's wrong right now.
String s = "a,b,c,d";
String[] test = s.split(",");
System.out.println(test[0]);
System.out.println(test[0] == "a");
Console prints:
a
false
That doesn't make sense at all. test[0] is "a" and ("a" == "a") is false ?
Excuse me for my bad english.
Thanks!

== tests for reference equality.
.equals() tests for value equality.
Look this question.

Related

Java KeyEvent - why "Unknown" != "Unknown"? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
Why does
KeyEvent.getKeyText(0).substring(0, 7) == "Unknown"
return false when
System.out.print(KeyEvent.getKeyText(0).substring(0, 7));
prints exactly "Unknown"?
In Java Strings are objects, so you should not compare with ==. You have to call equals on strings to compare there content.
If you compare them with == you compare them by reference.
KeyEvent.getKeyText(0).substring(0, 7).equals("Unknown");
An alternative would be contains:
KeyEvent.getKeyText(0).contains("Unknown");
See this post: Java String.equals versus ==

Conversion of character to String not working [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
String s="rahul".substring(0, 1);
s==Character.toString('r') //2nd
2nd evaluates to false but it should evaluate to true since i am converting 'r' to a String.
Why i am getting false as a result?
By doing this i am able to make the condition true
//char s=name.charAt(0);
// s=='r'
;
Try it as:
s.equals(Character.toString('r'). For more details What's the difference between ".equals" and "=="?
Instead of comparing Strings with == you should use equals().
String s="rahul".substring(0, 1);
System.out.println(s.equals(Character.toString('r')));
Use .equals instead, see code below:
//current code
System.out.println(s==Character.toString('r'));
//change to be as follows
s.equals( Character.toString('r'));
System.out.println(s.equals( Character.toString('r')));

String manipulation boolean [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
String a ="abc";
return (a.substring(1)=="bc");
I tried to print the result of
a.substring(1)
which is also
"bc"
Why the result is false?
I think it's true.
== compares references and the value of primitives (int, long etc), use a.substring(1).equals("bc") instead.
It should be like this:
String s = "abc";
System.out.println(s.substring(1).equals("bc"));

Comparing two string literals [duplicate]

This question already has answers here:
Comparing strings with == which are declared final in Java
(6 answers)
Closed 9 years ago.
while comparing to strings we can do using == or .equals()
In == we know that it checks for references but in .equals() it checks for contents.
So suppose if there are 2 strings say
String s="SO";
String s1="SO";
so in this case s1==s and s.equals(s1) both will give true.
But here it gives me false
So what I assume is + is high priority than ==
so in this case
System.out.println(""+s1==s);
it will be splitted like (""+s1)==s and now ""+s1 will be a new String and hence the new String will never be equal to s so its printing false
I am just interested to know whether I thought is right or not
""+s1 creates a new String Object on the heap (since it is not declared as final). So, the references will not be same.

Two strings are equal but wont work on if statement [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
System.out.println(characters.get(selected).getName());
// The name printed is "Mario" But the if statement will not
// work as though the two strings are diffrent but they are the same.
if(characters.get(selected).getName() == "Mario"){
playSound("sounds/clickmario.wav");
}
Comparing two strings and when I debug the comparison is "Mario" to "Mario" so the if statement should be true but its false because nothing inside the if statement is being read. Why is this happening? I have tried assigning this .getname to a tempString and comparing it but still when they are the same string the statement results as false. Please help
You have to use .equals() for string comparison in java
if(characters.get(selected).getName().equals("Mario")){
playSound("sounds/clickmario.wav");
}
Refer this for String comparison.
and
for basics of String refer this.
use
if (stra === strb)
it should work in javascript, for java
if (stra.equals(strb))
Then it should work too

Categories