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

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: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Please read the explanation carefully. It is correct. It is talking about a different case. Had newStre been defined as String newStre = ""...
If you like our products and services, please help us by posting your review here.

vandreev
Posts: 1
Joined: Mon Jul 02, 2018 10:36 am
Contact:

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

Post by vandreev »

all the chars will be concatenated to null, so newStr = nullgood in the end

st.lisker
Posts: 22
Joined: Sat Jun 30, 2018 6:11 am
Contact:

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

Post by st.lisker »

String myStr = "good";
char[] myCharArr = {'g', 'o', 'o', 'd' };

System.out.println(myStr == "good");

String newStr = "";
for(char ch : myCharArr){
newStr = newStr + ch;
}

System.out.println("_____________________________");
System.out.println(newStr.hashCode()); //3178685
System.out.println(myStr.hashCode()); //3178685
System.out.println(newStr==myStr); // false -------------> But why?

The explanation says that these are two different objects, but why? There was not new(). They are put in the pool. They even have the same hash code. Why the false?

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

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

Post by admin »

Just because the hashCodes of two objects are equal doesn't mean that the objects themselves are equal. (There reverse is actually true i.e. if the objects are equal then the hashCodes will be equal).
A String created using concatenation where its operands are not compile time constants is a new String.
If you like our products and services, please help us by posting your review here.

st.lisker
Posts: 22
Joined: Sat Jun 30, 2018 6:11 am
Contact:

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

Post by st.lisker »

Thanks for the answer!
"Just because the hashCodes of two objects are equal doesn't mean that the objects themselves are equal." -> I thought, it means that they are the same object 8-(
"A String created using concatenation where its operands are not compile time constants is a new String" -> OK, got it (This point can be on the exam? )

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

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

Post by admin »

>This point can be on the exam?
Yes, it is possible but most probably not.
If you like our products and services, please help us by posting your review here.

rali14043
Posts: 2
Joined: Sat Jan 19, 2019 4:38 pm
Contact:

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

Post by rali14043 »

This is a bug in Java since If I write the following line they don't compile:

String newStr = null (compiles successfully)
String newStr1 = Null (does not compiles)
String newStr2 = NULL (does not compiles)
String newStr3 = abcd (does not compiles)

I don't understand while String newStr = null compiles while any other values don't compile.

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

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

Post by admin »

Java is case sensitive. null is a keyword but Null and NULL are not. Neither is abcd. So, the compiler thinks that Null, NULL, and abcd are variable names.

Also, pay close attention to the error message. It will tell you exactly what the compiler is thinking.

Are you following any book?
If you like our products and services, please help us by posting your review here.

itvase
Posts: 1
Joined: Tue Jan 09, 2024 10:52 pm
Contact:

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

Post by itvase »

Chanandler wrote:
Thu Aug 25, 2016 1:36 pm
Did something go wrong here?

Look for correct/green answer and the explanation:
and true for newStr.equals(myStr)
I have the same concern as you.
It's strange that admin hasn't responded to this for more than 7 years.

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

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

Post by admin »

itvase wrote:
Thu Jan 11, 2024 11:50 pm
Chanandler wrote:
Thu Aug 25, 2016 1:36 pm
Did something go wrong here?

Look for correct/green answer and the explanation:
and true for newStr.equals(myStr)
I have the same concern as you.
It's strange that admin hasn't responded to this for more than 7 years.
The reply to this query is there at the top of this page.
I am not sure what is the issue.
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 51 guests