About Question enthuware.ocajp.i.v7.2.1303 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1303 :

Post by admin »

When you see a string in double quotes, that means it is a compile time constant. That means "hel" and "lo" are compile time constant strings. Since the compiler knows their values, it creates "hel"+"lo" i.e. "hello" at compile time as well, which is same as the one that it created at the line just before line 1.

This is not the case with lo because lo is a variable. Compiler cannot assume the value of this variable. Therefore, it cannot compute "hel"+lo at compile time. The JVM at runtime computes its value and that is why the resulting "hello" is not the same string as the one created by the compiler earlier.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

dxie1154
Posts: 9
Joined: Sat Jan 14, 2017 5:01 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1303 :

Post by dxie1154 »

So basically the things that are causing this are the compiler and the JVM?
The compiler adds the two compile time constant strings together, but the variable is added to "Hel" by the JVM, and because the way the two Strings were added together is different, it causes true or false to be returned.

Right?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1303 :

Post by admin »

That is correct.
If you like our products and services, please help us by posting your review here.

jkronegg
Posts: 1
Joined: Fri Sep 01, 2017 5:56 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1303 :

Post by jkronegg »

admin wrote:Because lo is non-final variable. So the compiler cannot use it as a constant for optimizations. Had it been final, your argument would have been correct.
Actually, the compiler does "constant compile-time Strings" optimization only when it recognizes such "constant compile-time Strings". Having a "final String" may not be sufficient to make the compiler recognize them. For example:

Code: Select all

		boolean c = "ab"==("a"+"b"); // true ("b" is recognized as compile-time constant)
		
		final String b1 = "b";
		boolean c1 = "ab"==("a"+b1); // true (final String b1 is recognized a compile-time constant)
		
		final String b2 = "b"+1;
		boolean c2 = "ab"==("a"+b2); // false (although b2 is final, it is not recognized as a compile-time constant string expression)

		String b3 = "b";
		boolean c3 = "ab"==("a"+b3); // false (although b3 is never modified is not recognized as a compile-time constant string expression)
What counts is what the compiler recognizes, not what we know to be a constant String value.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1303 :

Post by admin »

why would you expect "ab"==("a"+b2); to return true? b2 is "b1" as per the code that you've posted. "ab" cannot be equal to "ab1".
If you like our products and services, please help us by posting your review here.

Belcheee
Posts: 4
Joined: Thu Sep 14, 2017 6:41 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1303 :

Post by Belcheee »

While trying to understand this, I added the following code in a method:

Code: Select all

String s1 = "Hello";
String s2 = "Hello";
System.out.println("result is " + s1==s2)
Expecting this to output true as per the explanations on this post, but instead it outputs false.

If I remove the "result is" string from the println argument, it returns true (as expected).

I understand that a literal String either side of the + operand converts the other variable to a String, but it's already a String, so what has it changed?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1303 :

Post by admin »

+ has higher precedence than ==. "result is hello" is not equal to "hello".
If you like our products and services, please help us by posting your review here.

Belcheee
Posts: 4
Joined: Thu Sep 14, 2017 6:41 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1303 :

Post by Belcheee »

admin wrote:+ has higher precedence than ==. "result is hello" is not equal to "hello".
Of course! Thank you.

Rinkesh
Posts: 35
Joined: Sat Nov 25, 2017 4:13 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1303 :

Post by Rinkesh »

6. The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents. (So line 5 prints true.)
How can 5th one be a true if this statement says that explicitly interning a COMPUTED String?lo is a variable so it will be computed at run-time,right?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1303 :

Post by admin »

Rinkesh wrote:6. The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents. (So line 5 prints true.)
How can 5th one be a true if this statement says that explicitly interning a COMPUTED String?lo is a variable so it will be computed at run-time,right?
Yes, and the intern method will be executed at run time as well. The string "Hel"+lo will be computed at run time and the call to intern will return a reference to the interned string containing "hello", which is same as the one pointed to by the variable hello.
If you like our products and services, please help us by posting your review here.

bryanpage1
Posts: 1
Joined: Wed Nov 11, 2020 12:08 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1303 :

Post by bryanpage1 »

Please note that the question has the wrong answer listed.

Here is the question

Consider following classes:

//In File Other.java
package other;
public class Other { public static String hello = "Hello"; }


//In File Test.java
package testPackage;
import other.*;

class Test{
public static void main(String[] args){
String hello = "Hello", lo = "lo";
System.out.print((testPackage.Other.hello == hello) + " "); //line 1
System.out.print((other.Other.hello == hello) + " "); //line 2
System.out.print((hello == ("Hel"+"lo")) + " "); //line 3
System.out.print((hello == ("Hel"+lo)) + " "); //line 4
System.out.println(hello == ("Hel"+lo).intern()); //line 5
}
}
class Other { static String hello = "Hello"; }
What will be the output of running class Test?

This will fail to compile because //line 1 is attempting to retrieve the Other class from testPackage while Other only exists in package other.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1303 :

Post by admin »

No, it is exists in testPackage as well. See the last line of the code that you have shown above.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 26 guests