• Post Reply Bookmark Topic Watch Topic
  • New Topic

how the "+" sign works inside the method call parameters ?  RSS feed

 
Sami Kassoum
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just encountered this line of code and I really don't understand how the + sign works here ?

seems like the Font constructor can combine two parameters together, but then how is the constructor itself is written?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing special with the + sign when it's used in a method argument list vs. when it is used anywhere else.

It adds two numbers. Font.BOLD and Font.ITALIC are just constants defined in class Font, and they are added together here. It is exactly the same as this:

 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look in here:

https://docs.oracle.com/javase/7/docs/api/java/awt/Font.html

You will see there are three constructors for Font, one which has three arguments. The second is "style" which you is defined as:

The style of this Font, as passed to the constructor. This style can be PLAIN, BOLD, ITALIC, or BOLD+ITALIC.


In your code sample you have chosen the last option. I wouldn't worry about the "+", that's just a valid option as defined by the class.

 
Sami Kassoum
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jesper, Paul
Now I understand that! at first my imagination went too far
but now I know it is simply an addition of two constants and the result is passed to the second parameter in Font(...,...,...) constructor
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the actual values of these constants and convert them to their binary equivalents, you might see what's happening and why the + works in combining BOLD and ITALIC in one style value. Each constant value has only one bit position set in binary form and each position that is set is different from the others. They are essentially bit flags. A common way to combine bit flags like this is to do a bitwise OR operation but addition will produce the same result and is more intuitive to read.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you dig into the documentation, you'll also see that PLAIN is 0. This means that it has all its bit flags set to 0. Because of this, adding PLAIN to any other constant will do nothing. You have to set the value to just PLAIN. You could also do a bitwise AND or multiply by PLAIN (interpreted as TURN ALL BITS OFF) but that is not as intuitive to read as the + operation to combine flags.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use the bitwise inclusive or operator |
The really clever thing is that you can toggle a feature on and off with the bitwise exclusive or operator. Let us imagine we have several attributes, each set by a different bit.
foo.setAttributes(foo.getAttributes() ^ Foo.SOUR);
toggles between sour, not sour and back to sour.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!