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

Guest

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

Post by Guest »

You are not following naming conventions for packages.

"testPackage" should read "test_package"

Even better...

com.enthuware.test_package

Robert

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

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

Post by admin »

You are right. In an ideal world, you should have a name that follows the conventions. However, the exam has all sorts of weird names and code that breaks all the conventions. One should be able to sift through these and arrive at the right answer. Unless the question asks specifically about the conventions (which is not really on the exam), you should ignore such issues.

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

icepeanuts
Posts: 53
Joined: Thu Nov 22, 2012 12:01 am
Contact:

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

Post by icepeanuts »

I just don't fully understand the difference between these two statements.

System.out.print((hello == ("Hel"+"lo")) + " "); //line 3
System.out.print((hello == ("Hel"+lo)) + " "); //line 4

For line 4, lo is already defined as a literal String (i.e., lo = "lo"). So the compiler should compute "Hel"+lo at compile time, just as same as line 3. Why does it return false?

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

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

Post by admin »

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.

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

baptize
Posts: 32
Joined: Wed Mar 14, 2012 5:45 pm
Contact:

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

Post by baptize »

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.

HTH,
Paul.
Hi Paul, could you please elaborate on the following:
1. When i write import.other.Other; // i get compilation error! why?
2. "Hel" + lo //line 4: why JVM didn't search pool constant for "Hello" before creating a new String object?
3. Did the intern() method on line 5 force the JVM to look inside the pool first?

Thank you :)

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

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

Post by admin »

1. There should be no dot between import and other in import.other.Other; There shoud be a space.
2. As per JLS 3.10.5: "Strings computed by concatenation at run time are newly created and therefore distinct."
3. Yes.

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

baptize
Posts: 32
Joined: Wed Mar 14, 2012 5:45 pm
Contact:

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

Post by baptize »

admin wrote:1. There should be no dot between import and other in import.other.Other; There shoud be a space.
2. As per JLS 3.10.5: "Strings computed by concatenation at run time are newly created and therefore distinct."
3. Yes.

HTH,
Paul.
Hi Paul,
Thanks for 2 and 3.
#1 i still don't understand, please see attached photo.
Attachments
Screen Shot 2013-03-15 at 11.59.01 AM.png
Screen Shot 2013-03-15 at 11.59.01 AM.png (50 KiB) Viewed 20730 times

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

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

Post by admin »

I am not sure what is the issue. The error message says why it is not acceptable. There is no particular reason other than the fact that the language designers want it this way.
If you like our products and services, please help us by posting your review here.

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

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

Post by lovjit »

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?

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

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

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

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: 10036
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: 10036
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: 10036
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: 10036
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: 10036
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.

Post Reply

Who is online

Users browsing this forum: No registered users and 37 guests