Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on page 117 about Equality of String object (Java OCA 8 Programmer I Study Guide, Sybex)

 
Guilong Li
Greenhorn
Posts: 4
Java jQuery Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I am reading this book (OCA java se 8 program I study guide) for preparing OCA java certificate exam. When I read the page 117 which talking about the equality of String object, it prints out "true" when we compare the equality of Strings x and y as shown below:

It is also mentioned in this page the code below is supposed to print out "false":

However, when I run this code in either Eclipse or command prompt, it always generate "true".

I am confusing right now. Could anybody tell me which is the correct answer because there might be similar questions in the exam?  Thanks in advance!!!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guilong Li,

First of all, a warm welcome to CodeRanch!

Guilong Li wrote:It is also mentioned in this page the code below is supposed to print out "false":

I think (actually I am 100% sure) the code snippet you are using is different from the one in the study guide. That's why you have a different output than the one listed in the study guide. So you should revise your code snippet very carefully with the one listed/mentioned in the book, make the necessary changes (actually it's just one very small but really important change) and try to compile and run the code snippet again.

Hope it helps!
Kind regards,
Roel

PS. Always use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers. I already added the code tags for you. See how much easier the code is to read?
 
Guilong Li
Greenhorn
Posts: 4
Java jQuery Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel, thanks for your quick response. Yes, there is a space missing at the beginning the code. Thanks again.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guilong Li wrote:Yes, there is a space missing at the beginning the code. Thanks again.

Indeed, well spotted! And do you understand why you get false with the additional space and true without it?
 
Guilong Li
Greenhorn
Posts: 4
Java jQuery Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:And do you understand why you get false with the additional space and true without it?

Yes, the book has already addressed this question very clear. At the compile-time, " Hello World" is different with "Hello World".trim(), while the later one is the same string with "Hello World". That's why for String z="Hello world".trim(), you got "true", while for String z="Hello world".trim(), you got "false". Am I right? Thanks.
 
Guilong Li
Greenhorn
Posts: 4
Java jQuery Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guilong Li wrote:Yes, the book has already addressed this question very clear. At the compile-time, " Hello World" is different with "Hello World".trim(), while the later one is the same string with "Hello World". That's why for String z="Hello world".trim(), you got "true", while for String z="Hello world".trim(), you got "false". Am I right? Thanks.

Sorry the space missed again.
At the compile-time, " Hello World" is different with "Hello World".trim(), while the later one is the same string with "Hello World". That's why for String z="Hello world".trim(), you got "true", while for String z=" Hello world".trim(), you got "false".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic