• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

The unary plus operator (+)

 
Greenhorn
Posts: 5
2
  • 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]
 
Marshal
Posts: 5327
324
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: 13886
314
  • 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
Greenhorn
Posts: 5
2
  • 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: 13886
314
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.
 
Tim Cooke
Marshal
Posts: 5327
324
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
Greenhorn
Posts: 5
2
  • 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: 27211
87
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: 4195
57
  • 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: 41086
848
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: 13886
314
  • 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: 4195
57
  • 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.
 
get schwifty. tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic