This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
public static void main(String[] args) {
Integer i = new Integer(4);
System.out.println(i.toString());
if (i.toString() == i.toString()) {
System.out.println("true how");
} else {
System.out.println("false how");
}
}
While executing above code, I am getting output as "false how".
Can you explain how Jvm treats this object?
toString() creates a new string object every time and your code is actually checking if both references are the same, which is never the case so it runs the else case. If you try
i.toString().equals(i.toString())
you'll get the desired output.
You must compare objects with equals() method.
i.toString().equals(i.toString())
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
How many Strings are created in memory?
(4 answers)
Closed 4 years ago.
How does Java implement the below string comparisons
public class MyClass {
public static void main(String args[]) {
String a = "Chaipau";
String b = "pau";
System.out.println(a == "Chai"+"pau"); //true
System.out.println(a == "Chai"+b); //false
}
}
This is different from How do I compare strings in Java? , as the answer does not contain the why a new object is created in the second case , when it could have pointed to the same object reference as it is done in the first case.
"Chai"+"pau" is semantically identical to "Chaipau", and thus is the same instance that a refers to.
"Chai"+b is evaluated at runtime (because b is not a compile-time constant expression), creating a new instance of String, and thus is not the same instance that a refers to.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
the java subSequence is clearly true but only returns the false value. why?
trying to see if a sequence is equal to a subsequence of a bigger string
package testifthen;
public class TestIfThen {
public static void main(String[] args) {
String result = "01900287491234567489";
String result1 = "90028749";
if (result.subSequence(2, 10) == result1) {
System.out.println("excel");
}else {
System.out.println("not found");
}
}}
It's hard to say without more information (for example what language is this in).
Assuming this is Java, I would say your problem is using == with strings instead of the .equals function.
== doesn't check the contents of the string, only if they are referencing the same object. .equals should be used instead as it actually checks whether the characters match in the two strings
Try using
if (result.subSequence(2, 10).equals(result1)) {
System.out.println("excel");
} else {
System.out.println("not found");
}
The == symbol might be the one causing it to return false because of the different references.
This post should explain more about differences between == and equals(): What is the difference between == vs equals() in Java?
In Java, the .equals method should be preferred to the == operator when checking for semantic equality. .equals should be used when you are checking if two values "mean" the same thing, whereas == checks if they're the same exact object.
This question already has answers here:
will two strings with same content be stored in the same memory location?
(9 answers)
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
As the java-doc tells Java Operator == tests for reference equality (whether they are the same object). so "==" operator will return true only if two object reference it is comparing represent exactly same object otherwise "==" will return false.
But while running a piece of code all i found is that this statement doesn't satisfy the Output of the code.
Here's the code:
public class Test2 {
public static void main(String[] args)
{
String s="Sachin";
String t="Sachin";
System.out.println(s==t);
}
}
And Surprisingly i found a output "true".
Please Help me understand why it is so?
Here's a Screenshot to my program output:
https://i.stack.imgur.com/OZ0PW.jpg
You assuption is that
String s="Sachin";
String t="Sachin";
creates two string objects, but this is not true.
Java optimises the usage of string so that is puts literal strings in the string pool so that it assinges an already created string object from that pool if the compiler finds the same string a second time. This is called string interning.
You better try this:
public class Test2 {
public static void main(String[] args)
{
String s="Sachin";
String t=new String(s);
System.out.println(s==t);
}
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
why the result is false?
could someone please explain?
public class StringTest1 {
public static void main(String[] args) {
String a="a";
String b=a+"b";
String c="ab";
System.out.println(b==c);
}
}
Because they don't point to the same object in the memory.
== is used for comparison of either primitive types, or object references.
What you want to do, is to compare their values, for which you 'll need to use the equals(Object o) or equalsIgnoreCase(Object o) method(s)
Output of this comparison is FALSE because you have created two
objects which have different location in heap so == compare their
reference or address location and return false.
Read more: http://java67.blogspot.com/2012/11/difference-between-operator-and-equals-method-in.html#ixzz3xmRzfSkP
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I have this
public void otis() {
println("What is Otis?");
String otis = readLine(">");
println("You said " + otis);
println(otis);
println(otis);
if (otis == "dog"){
println("you got it right!");
}
else {
println("try it again!");
otis();
}
}
But for some reason even when I respond "dog" it doesn't find a match. I can print the "otis" variable and it says "dog" but apparently that's not equivalent to "dog" somehow?
Can you try the code below? Java doesn't recognize strings as equivalent from two different instantiations even if their values are equivalent. This is because each string is a pointer, and their pointer values aren't equivalent. Try using the String.equal method!
otis.equals( "dog" )
Because == means "is the same exact object in memory", the constant string "dog" and the string it reads from the console are not the same object, even if they have the same contents. When doing comparisons in Java, always use .equals().
As a possible side effect of this, you have to be careful when comparing things that might be null in Java. If you try to do
String dog = null;
if(dog.equals("dog")) { do_something(); }
You'll end up with a NullPointerException. For this reason, many coders prefer to compare strings like this:
if("dog".equals(dog)) { do_something(); }
since you always know the constant string will not be null.