• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Equality

 
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, on Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 9834-9835). Enthuware. Kindle Edition.
I am not sure why this returns true:

considering that trim() here doesn't do anything? Are the extra spaces ignored?
thanks
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

That line of code should return false.

Henry
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have thought so, but apparently not according to Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 9021-9027). Enthuware. Kindle Edition. ):

Which of these expressions will return true? Select 4 options
A.
B.
C.
D.
E.


Correct answers:A,B,C,E.
And here is a screenshot of the question too:

equals.jpg
[Thumbnail for equals.jpg]
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Attin wrote:I would have thought so, but apparently ...



It takes only a few seconds to write a test program to confirm. In fact, you probably could have done it faster than posting this topic...


... and BTW, with the answer, since the "hello" and "world" are on different lines, how do you know that there is only one space?

Henry
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Attin wrote:. . . C. . . .

Now, that is interesting. You will have to go to the Java® Language Specification (=JLS). Now, the JLS can be difficult to read at the best of times, but I am going to make you read the grammar. That will be worse. You want the line saying

ExpressionName . [TypeArguments] Identifier ( [ArgumentList] )

Ignore the type arguments and you have
ExpressionName = "hello"
. = . (surprise, surprise ‍)
Identifier = concat
( = (
ArgumentList = "    world"
) = )

Now, it is evident that you have a kind of expression on the left of that line of grammar; you are also saying that it is a kind of expression. So we can fiddle that into

KindOfExpression ::= KindOfExpression . identifier ( argumentList )

So  that grammar shows that the . before identifier associates to the left. This is very important. If the . operator associated to the right, you would execute it like this:-But . associates to the left, so it is executed like this:-You can show that for yourself very simply:How on earth can you get line C to return true?
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried it myself yesterday, because like Henry I thought that it should be false. And so it was. Maybe it was a typo, with some parentheses misplaced? This would return true:

But that seems unlikely.
 
Enthuware Software Support
Posts: 4818
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This thread turned up while I was searching for something.
This is neither an error nor a typo. It seems some versions of Kindle are stretching a single space a little too much. It shows up correctly on Kindle Fire HDX 8.9 though (tried with Kindle Previewer). The explanation given with this question makes it quite clear that there is a single space between "hello" and "world".

HTH,
Paul.
 
reply
    Bookmark Topic Watch Topic
  • New Topic