About Question enthuware.ocajp.i.v7.2.1303 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1303 :
You are not following naming conventions for packages.
"testPackage" should read "test_package"
Even better...
com.enthuware.test_package
Robert
"testPackage" should read "test_package"
Even better...
com.enthuware.test_package
Robert
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 53
- Joined: Thu Nov 22, 2012 12:01 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
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?
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?
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 32
- Joined: Wed Mar 14, 2012 5:45 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
Hi Paul, could you please elaborate on the following: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.
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
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
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.
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.
-
- Posts: 32
- Joined: Wed Mar 14, 2012 5:45 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
Hi Paul,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.
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 (50 KiB) Viewed 21647 times
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
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.
-
- Posts: 2
- Joined: Sat Mar 22, 2014 12:07 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
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?
Other class lies in package named "other" not in "testPackage".Please clarify my doubt.What am i missing here?
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
There is a class Other in testPackage as well. It is there at the bottom of the code listing.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?
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Sat Mar 22, 2014 12:07 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
Thank you.Got it.
-
- Posts: 1
- Joined: Fri Jun 27, 2014 10:44 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
Since we already have a class named 'Other', importing another class named 'Other' should cause compilation problem right?
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
No, why do you think so?KalpanaAr wrote:Since we already have a class named 'Other', importing another class named 'Other' should cause compilation problem right?
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Tue Oct 07, 2014 6:53 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
good oneadmin wrote:There is a class Other in testPackage as well. It is there at the bottom of the code listing.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?
-
- Posts: 7
- Joined: Tue Oct 04, 2016 6:11 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
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?
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?
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
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.
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.
-
- Posts: 6
- Joined: Fri Jan 20, 2017 8:45 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
I'm a little confused with the answer explanation:
at oracle, only the concatenation is at runtime, §3.10.5. String Literals5. Strings computed at run time are newly created and therefore are distinct. (So line 4
prints false.)
Strings computed by concatenation at run time are newly created and therefore distinct.
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
It doesn't say "only".
The following code print false:
This proves that even a string created using substring is distinct from the one created using a constant expression.
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
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Fri Jan 20, 2017 8:45 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
So how can i know for sure what will create new Object?admin wrote:It doesn't say "only".
The following code print false:This proves that even a string created using substring is distinct from the one created using a constant expression.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
also substring just like concat and replace, by definition should return new String..
so maby they are pointing to different objects?
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
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.
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.
-
- Posts: 6
- Joined: Fri Jan 20, 2017 8:45 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
Great, thanksadmin 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.
-
- Posts: 2
- Joined: Fri Feb 03, 2017 12:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
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.
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
Did you try compiling and running it?
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Fri Feb 03, 2017 12:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
Yes, but i made a mistake at first attempt compile and run. Now i'm repeated this process, and it work fine.admin wrote:Did you try compiling and running it?
-
- Posts: 9
- Joined: Sat Jan 14, 2017 5:01 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1303 :
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.
((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.
Who is online
Users browsing this forum: No registered users and 26 guests