Page 1 of 2

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

Posted: Fri Nov 02, 2012 6:12 pm
by Guest
You are not following naming conventions for packages.

"testPackage" should read "test_package"

Even better...

com.enthuware.test_package

Robert

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

Posted: Fri Nov 02, 2012 8:09 pm
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.

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

Posted: Mon Jan 21, 2013 12:45 am
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?

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

Posted: Mon Jan 21, 2013 7:14 am
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.

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

Posted: Thu Mar 14, 2013 6:49 pm
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 :)

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

Posted: Thu Mar 14, 2013 7:47 pm
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.

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

Posted: Fri Mar 15, 2013 11:00 am
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.

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

Posted: Fri Mar 15, 2013 1:19 pm
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.

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

Posted: Wed Apr 02, 2014 9:22 pm
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?

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

Posted: Wed Apr 02, 2014 9:33 pm
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.

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

Posted: Wed Apr 02, 2014 9:46 pm
by lovjit
Thank you.Got it.

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

Posted: Fri Jun 27, 2014 10:47 am
by KalpanaAr
Since we already have a class named 'Other', importing another class named 'Other' should cause compilation problem right?

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

Posted: Fri Jun 27, 2014 1:32 pm
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?

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

Posted: Wed Oct 08, 2014 6:34 am
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

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

Posted: Thu Oct 13, 2016 11:54 am
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?

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

Posted: Thu Oct 13, 2016 9:04 pm
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.

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

Posted: Sat Jan 21, 2017 3:03 am
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.

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

Posted: Sat Jan 21, 2017 3:15 am
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.

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

Posted: Sat Jan 21, 2017 3:33 am
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?

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

Posted: Sat Jan 21, 2017 4:33 am
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.

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

Posted: Sat Jan 21, 2017 5:23 am
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

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

Posted: Fri Feb 03, 2017 12:49 pm
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.

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

Posted: Sat Feb 04, 2017 12:19 am
by admin
Did you try compiling and running it?

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

Posted: Sat Feb 11, 2017 7:00 am
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.

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

Posted: Tue Mar 14, 2017 8:51 pm
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.