Concepts / Java Data Types

Java Data Types

Arithmetic expressions depend on both the operator and the types of the operands.

  • Programming

Same Symbol, Different Result

Two expressions can use the same operator and still produce different answers. Whether a result looks like ordinary mathematics depends on more than the symbol between the numbers. It also depends on the types of the values being combined. This article follows one expression through Java's evaluation steps and shows where the type of each operand changes the outcome.

What do you think happens?

In Java, what does the expression 7 / 2 produce when both operands are integers?

  • 3.5

  • 3

  • 4

  • 3.0

Reveal answer
Answer:

3

Two integer operands produce an integer result, so the fractional part is discarded and the answer is 3. Writing 7.0 / 2 instead gives 3.5, because one operand is then a real number.

Two Questions Every Arithmetic Expression Raises

When Java evaluates an arithmetic expression, it has to answer two questions. The first is which operation to perform, which the operator determines. The second is what types the operands should have while the operation runs. The second question is easy to overlook. The operators look familiar from mathematics, but the operand types can change the result, especially with division.

Most arithmetic operators combine two values. Java's basic binary operators are addition with +, subtraction with -, multiplication with *, division with /, and the remainder operator %. Each one works on a pair of operands. The unary operators are the exception, because they act on a single operand. The sections that follow take these operators in the order where their type behavior matters most.

Integer Division Versus Real Division

Division follows the type of its operands. If both operands are integers, Java produces an integer result, even when ordinary mathematical division would include a fractional part. The fractional part is discarded. If at least one operand is a real number, Java converts the other operand to a real number and produces a real-number result.

The left column of the diagram shows the integer case. Both operands are integers, so the division stays in integer arithmetic and the fractional part is dropped. The right column shows the real case. A single real operand is enough to change the operation: the integer 2 is converted to 2.0, and the division is carried out as real-number division. The only difference between the two expressions is the type of one operand, yet the results differ.

java
Output
3
3.5
3.5
Notes on the code
  • a / b uses two int operands, so the result is the integer 3
  • 7.0 / 2 has a real operand, so the division is real-number division
  • 7 / 2.0 also has a real operand, so the order of the operands does not matter here
Worked example

Predicting the result of 10 / 4

Predict the value of 10 / 4 when written as 10 / 4 and when written as 10 / 4.0.

  1. 1
    Check the operand types of 10 / 4

    Both 10 and 4 are integers, so Java performs integer division.

  2. 2
    Discard the fractional part

    The exact quotient is 2.5, but integer division discards the fractional part, leaving 2.

  3. 3
    Check the operand types of 10 / 4.0

    The operand 4.0 is a real number, so Java converts 10 to 10.0 and performs real-number division.

  4. 4
    Compute the real result

    10.0 / 4.0 gives 2.5.

Result

10 / 4 gives 2, while 10 / 4.0 gives 2.5.

How Java Converts Mismatched Operands

Suppose an expression combines values whose types differ. Before carrying out the operation, Java converts one value so that the two operands match. The source example is 37.4 + 10. Here the integer 10 is converted to the real number 10.0. Java then evaluates the operation using 37.4 and 10.0. This automatic adjustment is called type conversion.

The pipeline above reads from top to bottom. Java first notices that the operands have different types. It then converts one operand, the integer 10, so that both operands are real numbers. Only after this conversion does it perform the addition. The final value is real because the operands used in the operation were real.

java
Output
12.5
Notes on the code
  • n is an int, while x is a double
  • Java converts n to 10.0 before adding, giving 12.5 as a real number

The Remainder Operator

The percent sign, %, does not mean percentage in this context. It gives what remains after one number is divided by another. This is the remainder operator. For integer operands, 7 % 2 is 1, because 7 divided by 2 leaves a remainder of 1. The expression 34577 % 100 is 77, and 50 % 8 is 2.

java
Output
1
77
2
Notes on the code
  • 7 % 2 leaves 1 after dividing 7 by 2
  • 34577 % 100 keeps the last two digits, which is the remainder after dividing by 100
  • 50 % 8 leaves 2 after 50 is divided by 8

Unary Minus and Unary Plus

A unary operator acts on one operand rather than combining two values. Unary minus changes a number to its negative. The expression -X has the same value as (-1) * X. Java also provides unary plus, but +X leaves the value of X unchanged. Unary plus is available, though it has no effect on the value of its operand.

java
Output
-5
5
5
Notes on the code
  • -x negates the value of x for this expression
  • +x produces the same value as x
  • x itself still holds 5 after both expressions

The Plus Operator: Addition or Text

The plus operator has a second role in Java. When one side of the operation is a String, + concatenates the value onto that text instead of performing numeric addition. The other value is automatically converted to String for this combination. Whether + adds or joins depends on whether a String operand is present.

The flowchart starts with the plus operator and checks whether either operand is a String. If so, the other operand is converted to String and the values are joined into text. If neither operand is a String, the plus operator performs ordinary numeric addition. The same symbol therefore produces either a number or a piece of text, depending on the types present.

java
Output
7
Total: 34
7 total
Notes on the code
  • 3 + 4 has no String operand, so it adds to 7
  • "Total: " + 3 joins text first, and the result + 4 keeps joining, giving Total: 34
  • 3 + 4 is evaluated first as addition, and then the String joins the result, giving 7 total

Mistakes Beginners Make With Arithmetic Types

  • Common mistake

    Expecting 7 / 2 to produce 3.5

    What it looks like
    int a = 7; int b = 2; a / b
    Why it fails

    Two integer operands produce an integer result, so the fractional part is discarded.

    Do this instead

    Use a real operand, such as 7.0 / 2 or 7 / 2.0, when a real-number result is needed.

  • Common mistake

    Treating % as a percentage operator

    What it looks like
    50 % 8
    Why it fails

    The percent sign in Java gives the remainder after division, so 50 % 8 is 2, not a percentage.

    Do this instead

    Read % as what remains after division.

  • Common mistake

    Assuming + always adds numbers

    What it looks like
    "Total: " + 3 + 4
    Why it fails

    Once a String operand is present, + concatenates the value onto the text.

    Do this instead

    Check whether a String operand is present before predicting the result of +.

  • Common mistake

    Expecting unary plus to change a value

    What it looks like
    +x
    Why it fails

    Unary plus leaves the value of X unchanged.

    Do this instead

    Use -x to negate a value, and rely on x itself when no change is wanted.

Check Your Prediction

MEDIUM

Without running the code, predict the printed output of these three lines: System.out.println(9 / 4); System.out.println(9 / 4.0); System.out.println("Score: " + 9 % 4). Then state which operand types determine each result.

Hints
  • Check whether each division has two integer operands or a real operand.
  • The remainder operator gives what is left after division.
  • Once a String operand appears, the plus operator concatenates.

Summary of Operand Types and Operator Behavior

  1. 1
    Arithmetic expressions depend on both the operator and the types of the operands.
  2. 2
    When operand types differ, Java converts one value so that both operands match before the operation, as in 37.4 + 10 becoming 37.4 + 10.0.
  3. 3
    Two integer operands give integer division, which discards the fractional part, while a real operand gives a real-number result.
  4. 4
    The % operator gives the remainder after division, and unary minus negates a value while unary plus leaves it unchanged.
  5. 5
    The plus operator adds numbers unless a String operand is present, in which case it concatenates text.

At a glance

  • Arithmetic expressions depend on both the operator and the types of the operands.
  • Java converts mismatched operands to compatible types before evaluating the operation, as in 37.4 + 10 becoming 37.4 + 10.0.
  • Integer division discards the fractional part, while a real operand causes real-number division.
  • The % operator reports what remains after division, and unary minus negates one operand.
  • The plus operator concatenates text whenever one operand is a String, and otherwise performs numeric addition.