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

lovjit
Posts: 2
Joined: Sat Mar 22, 2014 12:07 pm
Contact:

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

Post by lovjit »

Thank you.Got it.

KalpanaAr
Posts: 1
Joined: Fri Jun 27, 2014 10:44 am
Contact:

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

Post by KalpanaAr »

Since we already have a class named 'Other', importing another class named 'Other' should cause compilation problem right?

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

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

Post by admin »

KalpanaAr wrote:Since we already have a class named 'Other', importing another class named 'Other' should cause compilation problem right?
No, why do you think so?
If you like our products and services, please help us by posting your review here.

chipchipchonkeykonky
Posts: 4
Joined: Tue Oct 07, 2014 6:53 am
Contact:

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

Post by chipchipchonkeykonky »

admin wrote:
lovjit wrote:I still can't figure out why the line System.out.print((testPackage.Other.hello == hello) + " "); is valid?
Other class lies in package named "other" not in "testPackage".Please clarify my doubt.What am i missing here?
There is a class Other in testPackage as well. It is there at the bottom of the code listing.
good one :D

elit3x
Posts: 7
Joined: Tue Oct 04, 2016 6:11 pm
Contact:

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

Post by elit3x »

Im stuck on;

System.out.print((other.Other.hello == hello) + " "); //line 2


the first hello variable is explicitly calling the hello variable in package other.
The second hello is retrieving the hello var local to the main method.

These are two different objects, i would expect == to return false.

What am I missing?

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

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

Post by admin »

That is just how the language is designed. Java keeps only one copy of all strings in a pool, no matter where created unless created using the new keyword.
You should check out section "3.10.5. String Literals" of Java Language Specification for complete details.

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

uqzma1xg
Posts: 6
Joined: Fri Jan 20, 2017 8:45 am
Contact:

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

Post by uqzma1xg »

I'm a little confused with the answer explanation:
5. Strings computed at run time are newly created and therefore are distinct. (So line 4
prints false.)
at oracle, only the concatenation is at runtime, §3.10.5. String Literals
Strings computed by concatenation at run time are newly created and therefore distinct.

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

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

Post by admin »

It doesn't say "only".
The following code print false:

Code: Select all

	String str = "123";
        String str2 = "12345";
        String str3 = str2.substring(0, 3);
        System.out.println(str+" "+str3+" "+(str == str3)); //prints 123 123 false
This proves that even a string created using substring is distinct from the one created using a constant expression.
If you like our products and services, please help us by posting your review here.

uqzma1xg
Posts: 6
Joined: Fri Jan 20, 2017 8:45 am
Contact:

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

Post by uqzma1xg »

admin wrote:It doesn't say "only".
The following code print false:

Code: Select all

	String str = "123";
        String str2 = "12345";
        String str3 = str2.substring(0, 3);
        System.out.println(str+" "+str3+" "+(str == str3)); //prints 123 123 false
This proves that even a string created using substring is distinct from the one created using a constant expression.
So how can i know for sure what will create new Object?
also substring just like concat and replace, by definition should return new String..
so maby they are pointing to different objects?

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

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

Post by admin »

That is why the explanation is given that whenever you compute a string at run time, it will create a new string object.
Computation implies that you don't know the string value at compile time. The value has to be computed (using concatenation) at run time and that will create a new string.
Also, if you ignore method calls and the new operator, the only way to create a new string is using the + operator. It will create a new string object if any of its operand is not a compile time constant.
If you like our products and services, please help us by posting your review here.

uqzma1xg
Posts: 6
Joined: Fri Jan 20, 2017 8:45 am
Contact:

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

Post by uqzma1xg »

admin wrote:That is why the explanation is given that whenever you compute a string at run time, it will create a new string object.
Computation implies that you don't know the string value at compile time. The value has to be computed (using concatenation) at run time and that will create a new string.
Also, if you ignore method calls and the new operator, the only way to create a new string is using the + operator. It will create a new string object if any of its operand is not a compile time constant.
Great, thanks

Radioactive
Posts: 2
Joined: Fri Feb 03, 2017 12:43 pm
Contact:

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

Post by Radioactive »

Hello! I think in file Test.java missing public modifier for class Test. In this way program will compile but throw exception when trying to run.

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

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

Post by admin »

Did you try compiling and running it?
If you like our products and services, please help us by posting your review here.

Radioactive
Posts: 2
Joined: Fri Feb 03, 2017 12:43 pm
Contact:

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

Post by Radioactive »

admin wrote:Did you try compiling and running it?
Yes, but i made a mistake at first attempt compile and run. Now i'm repeated this process, and it work fine.

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

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

Post by dxie1154 »

I'm a little confused on:
((hello == ("Hel"+"lo")
and:
(hello == ("Hel"+lo)
If newly created Strings that are created at runtime (like the "Hel") are separate objects, then shouldn't the third line also print false?
I think this will happen because you are creating two new Strings at runtime at line 3. That is why at line 4 it prints false, because of the "Hel", which is a newly created String object that has been created at runtime and is a new String object.
I just don't get why the third line is true, because you're making two completely new String objects, yet it passes the == test.
Why does it print true, instead of false?

Thanks in advance.

admin
Site Admin
Posts: 10055
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: 10055
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: 10055
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: 10055
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: 10055
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.

Post Reply

Who is online

Users browsing this forum: No registered users and 38 guests