Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Sybex CSG 11, potential incorrectness in Question 20 of Chapter 3

 
Greenhorn
Posts: 8
Angular Framework Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a bit confused about the Question 20 and it's answer in Chapter 3 of the Complete Study Guide:

Question

20. Which of the following statements about unary operators are true? (Choose all that apply.)

A. Unary operators are always executed before any surrounding binary or ternary operators.
...



Answer:

A, D, E. Unary operators have the highest order of precedence, making option A correct. ...



In my opinion (and understanding) Unary operators do not always be performed first, as it does not get executed if it appear in behind short-circuit operator where the decision is already clear or within the non-called part of a ternary expression.
And even if it does, get executed, the always is wrong in my opinion: basically java will first check the first statement in front of a short-circuit expression before it evaluates the part behind, same counts for the ternary expression: it will perform first the evaluation, and later call the evaluated expression.
So a true statment would look like: "Unary operators are executed before any surrounding binary operator, except it stands behind a short-circuit expression." and  "Unary operators within a ternary evaluation, are always executed before the surrounding ternary expression".

Example:returns:

May i ask if someone can check that? And explain me where my thoughts make an error, if the answer within the book is correct?

*multiple edits are made to make text better understandable, and to provide more examples
 
Marshal
Posts: 71752
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think I agree with you. The answer was right and the question was, well, at least not clear. I think it would read better as something like this:-

If a unary operator and a binary or ternary operator are both executed on the same operand, the unary operator is executed first.

 
Ranch Foreman
Posts: 175
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This reminds me of one of the examples that Scott and Jeanne decided to remove from any future versions of the book because it confused people more than it enlightened them.

Precedence only breaks ties between two operators of different precedence competing for the same operand.
Within equal precedence of operators competing for the same operand, associativity (left or right) applies instead.

A very large number of confused people misinterpret this to mean that globally within an entire expression "all higher precedence operators are executed first, then later the lower precedence operators get applied".

Computer scientists and programmers smarter than almost everyone else (certainly including me) seem to say "Congratulations on memorizing the entire precedence table of operators in your chosen language.  If you write code that will be misinterpreted by any significant number of otherwise competent developers that have not memorized the whole table, you have written code that is difficult and dangerous to read and maintain.  You may think your code is 'more efficient' at runtime but it most likely is not.  The extra parenthesis will rarely or never slow anything down at runtime, and you've made the job of your maintenance programmers harder.  This will not win you friends on your team, nor make better software."

That statement was aimed at people like me, who were very proud to have memorized the whole table.  I made it a point to never write code like that, and to limit the use of the skill to explaining to confused third parties why code written by someone else than either of us wasn't behaving the way they were expecting.

In defense of the exams, I feel that I have spent decades seeing bad, tricky code behaving strangely in production, and that the exams want you to learn not to be tricked by that, so you won't add new problems when working on that code due to your misunderstanding.  I have seen that the danger they are trying to protect you from does exist.

If that is as far as it goes, great.  Many people seem to reasonably worry that they have an unintended side effect of encouraging test takers to continue to write code like that outside of the exams.  I don't know to what extent certification exams are to blame, but it sure does happen a lot.  Both the smart-alecks and those confused by their code that treats parentheses like rare expensive commodities seem to contain many otherwise excellent and highly productive developers.

I am facing the 819, and find myself trying to memorize the table well enough to get the right answer on any of the questions of this type.  I like to think that it won't cause me to avoid parentheses like they were some kind of rare and expensive resource.  Many of the questions of this type seem like they would evaporate into nothingness just by adding them, nobody would ever get them wrong.

x = x++x++x++x++;   // great code only as an obfuscated Java contest entry, terrible anywhere else.  I don't want to see it on a cert exam, but want to make sure I don't get the question wrong if I do...
 
Campbell Ritchie
Marshal
Posts: 71752
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:. . . one of the examples that Scott and Jeanne decided to remove from any future versions of the book because it confused people . . .

I presume you mean the lion and tiger question. I thought I had persuaded them to keep it in. The fact that everybody who doesn't know the order of execution gets it wrong makes it a good question. Remember that cert exaams test your knowledge of the rules, not your ability to write good code. And if we start with x as 5, what would the value of x be after your obfuscated code?
 
Jesse Silverman
Ranch Foreman
Posts: 175
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is 26...I was already perceived to be an OCD precedence-memorizer, and I sometimes get these wrong....if I ever got THAT one wrong I don't expect it to happen again...

It reminds me tho, what did you think of the Java Puzzler book?
I saw in reviews people either loved it or hated it.
The negative reviews said "You could read this whole book and not learn how to do one thing in Java you didn't already know how to do."
The positive reviews didn't think that was the point of the book at all.
 
Campbell Ritchie
Marshal
Posts: 71752
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't got the book, so I don't know the numbers.

My Missus gave me a Java Puzzlers (Joshua Bloch and Neal Gafter) for my birthday once. I really like it, even though I still wouldn't get all the examples.

Jesse Silverman wrote:. . . The negative reviews said "You could read this whole book and not learn how to do one thing in Java you didn't already know how to do." . . .

There are a large population of book reviewers who think every book is a beginner's tutorial and mark them like that. I think that means they didn't read it properly.
 
Samuel Silver Moos
Greenhorn
Posts: 8
Angular Framework Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

I think I agree with you. The answer was right and the question was, well, at least not clear. I think it would read better as something like this:-

If a unary operator and a binary or ternary operator are both executed on the same operand, the unary operator is executed first.



Thanks a lot for the quick and clear answer

Also a big thank to you and Jesse Siverman to share your experience about such coding "problems" and the reasons why such question types provided by the book (and OCP) are quite helpful for us software-developers and engineers in everyday business.

Even if im codeding java for a couple of years , and moste of the project i worked run without problems, i'm learning quite a lot from the book, even if moste seems to be more like compiler-knowleage which each IDE delivers, there are a lot of additional informations which are helpful in everyday situations, especially when working with "legacy code". (if seen two kind of strange bugs, one was mixing 2^10 with Math.pow(2,10) and an other was 1|2 in a switch case statment, which i'm sure i would have found them en eternity earlyer with this knowledge...)

Regards Samuel

P.S. Sorry for my spelling mistakes, i'm an early bird: my writing skills get even worse in evenings.
 
Campbell Ritchie
Marshal
Posts: 71752
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somebody should have read the list of operators, then they would have known that ^ isn't a power operator. What was the intention behind 1 | 2? Was somebody trying to catch both possibilities? Of course the compiler won't notice. 1 | 2 is a compile‑time constant and the compiler will simply convert that to 5. And all's tickety‑boo. Remember no compiler can detect incorrect logic.



Except for my deliberate error, which I am trusting you to find.
JS: what did you get from

x = x++x++x++x++;

?
 
Samuel Silver Moos
Greenhorn
Posts: 8
Angular Framework Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Somebody should have read the list of operators, then they would have known that ^ isn't a power operator. What was the intention behind 1 | 2? Was somebody trying to catch both possibilities? Of course the compiler won't notice. 1 | 2 is a compile‑time constant and the compiler will simply convert that to 5. And all's tickety‑boo. Remember no compiler can detect incorrect logic.

Except for my deliberate error, which I am trusting you to find.
JS: what did you get from


x = x++x++x++x++;


?



Your expectations are correct, that's was exactly what was tryed... Luckily it was just in an error handling of a rest API.

I expect compilation sees: x= x + (+x) + (+x) + (+(x++));
But I'm definitely not sure if compilation would work.
But if, it would result in 4*x, but then override it with x+1 as the post increment assignment strikes?

Will test it for shure on Monday ;-)

 
Jesse Silverman
Ranch Foreman
Posts: 175
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The front part of my first reply after that covered that:

It is 26...I was already perceived to be an OCD precedence-memorizer, and I sometimes get these wrong....if I ever got THAT one wrong I don't expect it to happen again...



My natural inclination is to be able to do all these things, but there are so so so so so many places where you can memorize a dozen rules to take care of things or just say something like "don't mix varArgs with fancy overloading" and never bump into them....

Seriously, in almost no cases do parentheses cost anything, right?
 
Samuel Silver Moos
Greenhorn
Posts: 8
Angular Framework Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did you get to 26?!

I just tested it with Java11:

it does not compile: it recognizes the ++ as single unary operand in all cases:
returns:

And with parenthesis (which does not make the code anything better):
results in
(letting out parenthesis but keep spaces returns in same result)

Summarized:
Original version does not compile: as compiler sees ++ only as post/pre-increment.
Adding spaces so that code can compile "ignores" the post-increment, as it's result get override by the "complex" assignment to x with lower precedence gets called later.

Additional note to myself: Java-compiler accept quite strange inputs:
is seen as x=x++ +x; or maybe x+ ++x;<-EDIT: as compiler works "greedy" it's first of this two is seen as x=x++ +x++;
but following don't get compiled
Taken apart, that all this examples makes no sense at all, as this would only confuse other programmers, while better solutions would be available, they still would be accepted by compiler if added spaces.
Also quite tricky (but logical if following the rules): x=++x+x; does output 2*(x+1), while x=x+ ++x or x+++x returns 2*x+1.


hopefully i will never see such code in real live environment (and also not while taking the exam)

 
Campbell Ritchie
Marshal
Posts: 71752
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The most likely explanation is that the lexer associated with javac goes left‑to‑right and works on a greedy basis. After the second x, it tries to match the following plus signs. Since they would match both + and ++ the greedy lexer goes for the larger token. After that it cannot match anything else and gets confused. Note the order the compiler errors come in: left‑to‑right.
If you are trying to decode +++ you can match the first two +s, leaving one to be a binary plus.
And remind us, what does line 2 in the following do?
 
Jesse Silverman
Ranch Foreman
Posts: 175
8
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad here guys!
Normally, I am considered extremely typo-averse on both production and consumption.
Less detail-oriented people consider me OCD and do not appreciate my corrections unless they were about to send the typo-containing material out to a group of people whom they would be embarrassed in front of, then they love it.

I meant to say:
jshell> x = 5;
x ==> 5

jshell> x = x+++x+++x+++x++;
x ==> 26

I think this was my first serious typo on the forum, and I meant it to represent something I had seen once on a mock exam that I would literally never, ever, do in real life and would hope not to encounter in code I was working with.  I don't know, maybe some sort of auto-generated code, possibly.

p.s. The lexer-level stuff that I accidentally caused you to explore by my otherwise-regrettable typo reminds me of something from my old C++ days.
In Java code that I write today, including something I am thinking of posting on the regular Java programming forum, I almost always write things like:
Set< Set<String> > rather than Set<Set<String>> because until C++ 2011 many (all?) C++ lexers would see the >> as an improperly placed right shift operator token, rather than the close of nested type parameters.  In smaller fonts my now older eyes find the former more readable anyway, but my fingers hit the space bars so that C++ compilers nobody has used for a decade wouldn't choke on them.

Campbell: I now know TWO people who use the phrase tickety-boo, my wife and YOU.  She more often uses it as a form of negated predicate, "Wouldn't say tickety-boo" but uses it as situation all clear in the affirmative, tho less commonly.  She watches a lot of British tv/movies.  It is unbelievably uncommon amongst speakers of American English, I think.


 
Campbell Ritchie
Marshal
Posts: 71752
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's rather old fashioned but definitely commoner in Britain.
 
Marshal
Posts: 26308
80
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:It's rather old fashioned but definitely commoner in Britain.



Google Books agrees with you, at least about the "commoner in Britain" part. Have a look:

https://books.google.com/ngrams/graph?content=tickety-boo&year_start=1900&year_end=2019&corpus=28&smoothing=3

https://books.google.com/ngrams/graph?content=tickety-boo&year_start=1900&year_end=2019&corpus=29&smoothing=3
 
Jesse Silverman
Ranch Foreman
Posts: 175
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul, now we are getting way off topic, but am I to read that as the phrase is actually getting far more popular these days???
I definitely truly strongly felt that it was quite an old phrase and that the issue was that fewer younger people knew it anymore.
I am reading the graphs totally wrongly if that is the case, tho.

It may be a Pachabel's Canon in D phenomenon, where hardly anyone but Classical Music Scholars was even aware of the piece until it was featured in a very popular 1980 movie, or girls being named Madison (which up to that point wasn't a more common name for girls than Lincoln, Washington or Eisenhower) after Splash became popular....like, it might have been a catch-phrase for a character on a BritCom?
 
Campbell Ritchie
Marshal
Posts: 71752
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it is in books, then that reflects written English rather than spoken. I don't watch enough telly to comment about it becoming a catchphrase, unless of Sir David Jason as Delboy, which would have been before the rise on that graph.
 
author & internet detective
Posts: 40393
821
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Going back to the original point. Samuel: we were thinking about numeric binary operators with option A. I added it to the errata since technically it was incorrect. Good find on the edge case!
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic