About Question enthuware.ocajp.i.v7.2.1392 :
Moderator: admin
-
- Site Admin
- Posts: 10075
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1392 :
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.
-
- Posts: 1
- Joined: Mon Jul 02, 2018 10:36 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1392 :
all the chars will be concatenated to null, so newStr = nullgood in the end
-
- Posts: 22
- Joined: Sat Jun 30, 2018 6:11 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1392 :
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?
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?
-
- Site Admin
- Posts: 10075
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1392 :
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.
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.
-
- Posts: 22
- Joined: Sat Jun 30, 2018 6:11 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1392 :
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? )
"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? )
-
- Site Admin
- Posts: 10075
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1392 :
>This point can be on the exam?
Yes, it is possible but most probably not.
Yes, it is possible but most probably not.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Sat Jan 19, 2019 4:38 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1392 :
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.
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.
-
- Site Admin
- Posts: 10075
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1392 :
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?
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.
-
- Posts: 1
- Joined: Tue Jan 09, 2024 10:52 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1392 :
I have the same concern as you.Chanandler wrote: ↑Thu Aug 25, 2016 1:36 pmDid something go wrong here?
Look for correct/green answer and the explanation:and true for newStr.equals(myStr)
It's strange that admin hasn't responded to this for more than 7 years.
-
- Site Admin
- Posts: 10075
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1392 :
The reply to this query is there at the top of this page.itvase wrote: ↑Thu Jan 11, 2024 11:50 pmI have the same concern as you.Chanandler wrote: ↑Thu Aug 25, 2016 1:36 pmDid something go wrong here?
Look for correct/green answer and the explanation:and true for newStr.equals(myStr)
It's strange that admin hasn't responded to this for more than 7 years.
I am not sure what is the issue.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 29 guests