Pass two parameters to a function that accepts only one parameter

Asked

Viewed 280 times

6

I’ve been making a code to generate a pdf from the framework Itext in Java. I came across the following situation, I needed to define a phrase as negrito and sublinhado. After searching Google, I found the suggestion of a user to use the logical operator ou "|" short-lived circuito. Because this function in the parameter that accepts integer values only had one entry.

So the code went like this.

FontFactory.getFont(FontFactory.defaultEncoding, 10, com.lowagie.text.Font.BOLD | com.lowagie.text.Font.UNDERLINE)

Since this function getFont in this would only accept three parameters, and in the third parameter I passed 2 using the "or" short circuit, then I would like to understand what the purpose and why the language accepted.

com.lowagie.text.Font.BOLD and com.lowagie.text.Font.UNDERLINE are an Enum and respectively have the values 1 and 4.

2 answers

9

What you found is a bitfield and is a common way of representing boolean parameter sets in a compact way.

The way it works is that each bit (i.e., power of two) of the value represents a different formatting category. For example:

NEGRITO    = 1 (decimal) = 0001 (binário)
ITALICO    = 2 (decimal) = 0010 (binário)
SUBLINHADO = 4 (decimal) = 0100 (binário)

Each number then represents a different set, depending on which bits are 1:

6 (decimal) = 0110 (binário) = SUBLINHADO e ITALICO
3 (decimal) = 0011 (binário) = ITALICO e NEGRITO
0 (decimal) = 0000 (binário) = nenhuma formatação

The operator | is the "or binary" operator. For each bit, the response bit will be 1 if the bit is 1 in at least one of the operands and 0 if it is 0 in both. In your case where the numbers represent sets of formatting, the | acts as the joint operator:

0100 | 001 = 0101  -- SUBLINHADO | NEGRITO = (SUBLINHADO e NEGRITO)
0110 | 010 = 0110  -- (SUBLINHADO e ITALICO) | ITALICO = (SUBLINHADO e ITALICO)

5

You are not passing two parameters. There is no way to pass more parameters than the accepted method. You are making a mathematical expression with two operands and passing the result as parameter. See this way:

FontFactory.getFont(FontFactory.defaultEncoding, 10, bold + underline)

This is not the same thing, but it shows with arithmetic exactly the same way you used in your example. Let’s consider that the variable bold Valha, 1 and the underline worth 2, then you will be passing 3 to the third parameters of this method, only this.

You probably just met the logical operator | called "OR LOGICAL" described in boolean algebra.

This is a simple way to manipulate bits. You can connect all the bits required in an operation. The expected parameters is a byte or sequence of bytes which represent several logical states. Each state is a single bit which obviously can be turned on or off. If you search this enumeration com.lowagie.text.Font will see that its values are exponential (0, 1, 2, 4, 8, 16). And the logical OR is only saying to link these values. At the bottom is a sum. If, for example, the BOLD vale 2 e o UNDERLINE` is worth 4, the logical OR will ensure that these bits are connected, resulting in 6.

I will not extend myself in these details of the calculation, this the hugomg answer already explains and the AP posted a link in comment (Using Bit Flags and Enumsets in Java). The important thing for your question is that the operator | does not accept more than one parameter (actually the correct name is argument since you are passing, parameter is what you receive), it is only doing a calculation before sending. Another way of seeing:

int config = com.lowagie.text.Font.BOLD | com.lowagie.text.Font.UNDERLINE;
FontFactory.getFont(FontFactory.defaultEncoding, 10, config);

It is clear that there are only three parameters.

You can read more about the operator in that reply.

  • A doubt that remained, would be the following, this is done by JMV, because I’m imagining it at the level of debug understands, as this appears on the console for example from Eclipse, would be shown both enuns, or show the enum of this sum.

  • 1

    If you’re talking about the end result, there’s no way to show both enums more. Remember the first example I gave. If it was a sum, would it show "Bold + underline"? No, it would show the result of this account. With the enum will happen the same. These two enums are constants with a certain value. They are not magic constructions of language. After you run some math on them, only the result remains. The other side, ie the method that is receiving this result, should know how to manipulate this information.

  • 1

    To complement I am posting this blog http://eddmann.com/posts/using-bit-flags-and-enumsets-in-java/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.