• 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

The unary plus operator (+)

 
Ranch Hand
Posts: 67
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.

With reference to section "Applying Unary Operators" in Chapter 2 (of the book OCP Oracle Certified Professional Java SE 17 Developer Study Guide: Exam 1Z0-829).

The unary plus operator (+) does not indicate whether a number is positive.  It promotes a variable to an int (https://docs.oracle.com/javase/specs/jls/se17/html/jls-15.html#jls-15.15.3).

Consider the following example.



The above example will print



You can try it with all other types (byte and short).


The-unary-plus-operator.png
[Thumbnail for The-unary-plus-operator.png]
 
Sheriff
Posts: 5554
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have a question?
 
Saloon Keeper
Posts: 15442
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this is meant as an erratum for the study guide.
 
Albert Attard
Ranch Hand
Posts: 67
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this the right forum where we can raise potential errata about the OCP Oracle Certified Professional Java SE 17 Developer Study Guide: Exam 1Z0-829 book?
 
Stephan van Hulst
Saloon Keeper
Posts: 15442
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.
 
Tim Cooke
Sheriff
Posts: 5554
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh OK. It's probably worth stating clearly that you're submitting erratum rather than looking for help.
 
Albert Attard
Ranch Hand
Posts: 67
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry if I was not clear.  Is there a template that I can follow when reposting such things, please?
 
Marshal
Posts: 28134
94
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
No, not at all. Just mention that you think there is an error in the book. You almost did that anyway, so there's no harm done.
 
Master Rancher
Posts: 4760
71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While we're at it, a further erratum: the negative unary operator also does not imply a number is necessarily negative.  It just causes the sign to change - either positive to negative, or negative to positive.  And it's also promoted to int, unless it's already long, float, or double.
 
author & internet detective
Posts: 41855
904
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scott and I chatted. We are ok with our simplified book explanation.
 
Stephan van Hulst
Saloon Keeper
Posts: 15442
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with the OP though. Your explanation implies that the following expression yields a positive value:

It doesn't seem simplified, it seems an explanation of a completely different operator.
 
Mike Simmons
Master Rancher
Posts: 4760
71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, it seems to be valid only for explaining how unary operators affect numeric literals, but not how they affect variables and expressions.
 
Albert Attard
Ranch Hand
Posts: 67
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your answers/replies.

Jeanne Boyarsky wrote:Scott and I chatted. We are ok with our simplified book explanation.



Why do you think that the description in the book is a simplified version?  I am asking as I may be missing something.  The JLS (15.15.3), states

15.15.3. Unary Plus Operator +
The type of the operand expression of the unary + operator must be a type that is convertible (§5.1.8) to a primitive numeric type, or a compile-time error occurs.
Unary numeric promotion (§5.6) is performed on the operand. The type of the unary plus expression is the promoted type of the operand. The result of the unary plus expression is not a variable, but a value, even if the result of the operand expression is a variable.
At run time, the value of the unary plus expression is the promoted value of the operand.



I would mention this fact in the book for completeness, even if it is not in scope for the exam.
 
Jeanne Boyarsky
author & internet detective
Posts: 41855
904
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Albert Attard wrote:

Why do you think that the description in the book is a simplified version?  I am asking as I may be missing something.  ... I  would mention this fact in the book for completeness, even if it is not in scope for the exam.


I think the average reader would find it simpler. (Not every reader, you didn't.) We definitely aren't including things for completeness. This book is to help pass the exam. It's not the Java Language Specification.
 
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic