String concatenation and comparision using == [duplicate] - java

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

Related

In java String comparison, how == operator and hashcode() methods are behaving differently [duplicate]

This question already has answers here:
What is the use of hashCode in Java?
(8 answers)
Closed 2 years ago.
In String class hashCode() method is readable format of memory address.
As per the below code == operator compares memory location or hashcodes.
then how == operator returning false and hashCode() method returns true.
public class TestStringEquals {
public static void main(String[] args) {
String s1="Hello World";
System.out.println(s1.toUpperCase()==s1.toUpperCase()); //false
System.out.println(s1.toUpperCase().hashCode()==s1.toUpperCase().hashCode()); //true
}
}
== compares the starting pointer of two objects in Memory.
hascode is like checksum.

Difference in the results of String comparison in Java? [duplicate]

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.

can someone explain why this returns the "else" value and not the "then" value? [duplicate]

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.

Does Java Operator "==" really compare two objects based on memory reference? [duplicate]

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);
}
}

Java: Converting Integer to String. Comparing with == equals operator [duplicate]

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())

Categories