The content inside double quotes is not printing to System.out - java

In this program I used System.out.println
The content inside the double quotes is not getting printed?
public class Demo1 {
public static void main(String[] args) {
String s1 = new String("suresh");
String s2 = "suresh";
System.out.println("s2==s1::"+s2==s1);
}
}
Expected output: s2==s1::FALSE
Actual output: FALSE

Because of operator precedence, the + operator has higher precedence than the == operator.
This expression: "s2==s1::"+s2==s1 is the same as: ("s2==s1::" + s2) == s1
In other words, it checks if "s2==s1::" + s2 is equal to s1, and then prints the result of that comparsion (which is false).
Try this instead:
System.out.println("s2==s1::" + (s2 == s1));

Doing this
System.out.println("s2==s1::"+s2==s1);
is the same as doing
System.out.println(("s2==s1::"+s2)==s1);
because of the precedence of the operators: the + operator has higher precedence than the == operator.
So you are only getting true or false
Because you are concatenating the string literal s2==s1:: with the variable s2 AND then comparing that reference to variable s1
Using parenthesis will resolve the issue

Put s2==s1 into parenthesis in order to change the operators' precedence.
System.out.println("s2==s1::"+(s2==s1));

Related

getting == operator comparison false while comparing reference concatenated with literal with other reference [duplicate]

This question already has answers here:
Java String literals concatenation
(2 answers)
Closed 6 years ago.
When I perform the == operator comparison, I'm seeing false though I'm expecting the value to be true.
Why does this happen?
public class String1 {
public static void main(String[] args) {
//case 1
String s1="Hello";
String s2=s1+"Java";
String s3="HelloJava";
System.out.println(s2.equals(s3));// true
System.out.println(s2==s3);// expected True but getting output False
System.out.println(s2);
System.out.println(s3);
// case 2
String x = "hello";
String y = "he" + "llo";
System.out.println(x == y);// TRUE as exepected
}
}
In Java the == operator tests reference equality -> same object
.equals() tests for value equality
in both of your cases you have different objects (s1, s2, x, y)
== compares references (i.e. pointer values) of objects.
In case 2 the compiler can shorten "he" + "llo" to "hello" as opposed to the first case where the concatination is performed at runtime.
Also string literals are cached in a pool. Thus two occurences of "hello" will normally refer to the same object. This is possible because strings are immutable.
Strings follow the same rule as every other object. If you want to compare pointers use ==, if you want to compare content use equals.

equality check for Strings in System.out.println

I was checking the == operator directly in System.out.println for comparing string references.My doubt is as per 'String Constant Pool' both strings will refer to the same instance in pool and the output is 'true' then why first output is 'false'.
Below is a sample code:
String s1 = "abc";
String s2 = "abc";
System.out.println("s1==s2 is:"+ s1==s2);
System.out.println("s1==s2 with brackets is:"+ (s1==s2));
Output is:
false
s1==s2 with brackets is:true
System.out.println("s1==s2 is:"+ s1==s2); compares "s1==s2 is:"+ s1 to s2, and therefore returns false. (s1==s2) is true since both get the instance they refer to from the String pool.
If you see the Java docs the + gets higher precedency then == so first the string "s1==s2 is:"+ s1 will be concatenated then it will be compared to s2 which is false for first case because the first string formed by concatenation would be produced in heap.
System.out.println("s1==s2 is:"+ s1==s2); -> it concatenates the "s1==s2 is:"+ s1 and checks for equality because of that you are getting false.
you compare "s1==s2 is:" + "abc" with abc in the first case --> false

Class Hello in a test

I have come across this question in a test:
class Hello {
public static void main(String[] args){
String hello = "Hello", lo = "lo";
System.out.println(hello == ("Hel" + "lo"));
System.out.println(hello == ("Hel" + lo));
System.out.println(hello == ("Hel" + lo).intern());
}
}
The output is:
true
false
true
Why is the second output false?
It prints 'false' because the concatenation of the String constant "Hel" and the String object 'lo' results in a seaparate, anonymous string object, with its own unique object reference. Thus, the "hello" String object and the concatenated string are different objects based on object reference (== operator, not by String value with String.equals()).
== compares the references of two sides.
Here, for hello == ("Hel"+lo), the references of two sides are not the same. So, it returns false.
For comparing values, use equals() method.
I think it Comparision Literal Problem.
It Works.
System.out.print((hello.equals("Hel"+lo)) + " ");
System.out.print((hello == ("Hel"+"lo")) + " ");
I think it is because in the second output ("Hel" + lo) is no more in the string. The equality "==" operator compares object memory location and not characters of String.By default Java puts all string literal into string pool, but you can also put any string into pool by calling intern() method of java.lang.String class, like string created using new() operator.

Java == behaving ambiguously

I came across this question in a Facebook group. I know I should be using equals() method but I want to know why this is happening
class Main
{
public static void main (String[] args)
{
String s1="abc:5";
String s2="abc:5";
System.out.println(s1==s2);
System.out.println("s1 == s2 " + s1==s2);
}
}
OUTPUT
true
false
This is due to operator precedence. '+' has a higher precedence than ==. You are actually comparing ("s1 == s2" + s1) to s2.
http://introcs.cs.princeton.edu/java/11precedence/
The confusion is in the order of operations. What is happening is that you're concatenating "s1 == s2 " and s1, then using == on that result and s2.
They are different objects, so false is printed (and "s1 == s2" is not printed). Put parentheses:
System.out.println("s1 == s2 " + (s1==s2));
This will print s1 == s2 true because both s1 and s2 refer to the same interned string literal, "abc:5".
Oh I just make some change in the code and get that + first doing "s1 == s2 s1" then == with s2 which is not true. New code
class Main
{
public static void main (String[] args)
{
String s1="abc:5";
String s2="abc:5";
System.out.println(s1==s2);
System.out.println("s1 == s2 " + (s1==s2));
System.out.println("s1 == s2 " + s1==s2);
}
}
OUTPUT
true
s1 == s2 true
false
this is easy to understand once you thought about it: the 2. println first adds "s1 == s2" and your string s1 and then compares it with s2, so it outputs false, because "s1 == s2abc:5" is not "abc:5"
It is adding the strings "s1 == s2 " + s1 and then computing whether that is equal to s2. It is not so it is printing false.
The Java Operator Precedence might help you here.
I think when using "==" the system will just check if the two things share the same location in the systems memory. However, if you use the ".equals" method the system will check if the two Strings share the same characters.
Your code
System.out.println(s1==s2);
System.out.println("s1 == s2 " + s1==s2);
Since s1 and s2 are the same String literals
`s1==s2` is true
Thus the code can be written as
System.out.println(true); => true
System.out.println("true" + s1==s2);
Now, "true" + s1==s2 is comprehended as ("true" + s1)==s2 due to higher precedence given to "+". Thus
"true"+s1 => trueabc:5 and
s2 => "abc:5"
Hence
System.out.println("true" + s1==s2); => false
I think its because of operator precedence of something like that, if you do System.out.println(""+(s1==s2));, it will print true.

Confused in appending Strings in Java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I have a situation of appending String. And i'm confused ..
public static void foo() {
String s = "str4";
String s1 = "str" + s.length();
System.out.println("(s==s1) = " + (s1 == s));
}
And
public static void bar() {
String s = "str4";
String s1 = "str" + "4";
System.out.println("(s==s1) = " + (s1 == s));
}
In 1st case it's returning 'false' but in 2nd case 'true'
As i understand in both cases 'str4' object is being created on the heap. So it should return true in both cases. But it's not.
Kindly someone help me out why it's so. ? Thanks.!
Use
s1.equals(s)
to compare strings, otherwise you compare references.
In second case it returns true because String s1 = "str" + "4"; would be optimized to String s1 = "str4"; and s and s1 would refer to the same String.
The == operator in Java only returns true if both references refer to the same object. If you are trying to compare two Strings for equivalent content, you must use the equals() method.
you need to use .equals() for this
.equals() // if you dont want to ignore case
.equalsIgnoreCase() // if you want to ignore case
== compare the references.
In the second case both strings are equal .So references are also equal.
String s = "str4";
String s1 = "str" + "4"; .//finally str4
Here s1 ans s2 contents are equal.So they have same reference.
In my own understanding :
"str" => String
"4" => String
However,
s.length() => int
With ==, memory locations are compared.
Using the first example, Java creates another String which is in another memory location other than the location of 's' because you are trying to do String + int = String.
The second example returns true because it is just the same memory location as your 's' only that the value is changed. String + String = Concatenated String
Since you are trying to compare if the two strings have the same characters inside but not necessarily the same location, then s.equals(s1) is the best solution.
However, should you want to test if both variables are pointing to the same object then == must be used because of its shallow comparison.

Categories

Resources