What do Java’s int, double, and boolean types store?
int stores whole-number values, double stores decimal or floating-point values, and boolean stores either true or false.
Study Java Primitive Types and Expressions with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What do Java’s int, double, and boolean types store?
int stores whole-number values, double stores decimal or floating-point values, and boolean stores either true or false.
What is a Java literal?
A literal is a fixed value written directly in source code, such as 12, 4.99, or false.
What is Java’s arithmetic operator precedence?
Parentheses are evaluated first, then *, /, and %, followed by + and -. Operators with equal precedence are evaluated left to right.
When does + perform String concatenation?
If either operand is a String, + performs concatenation. Evaluation remains left to right, so "Total: " + 2 + 3 produces "Total: 23".
What is the result of 7 / 3 in Java?
With two int operands, division uses integer arithmetic and discards the fractional part toward zero. Thus, 7 / 3 evaluates to 2.
How can you preserve the fraction in numeric division?
At least one operand must be a double before division occurs. For example, 7.0 / 3 produces approximately 2.3333333333333335.
What does the % operator return?
The % operator returns the remainder after division. For example, 17 % 5 evaluates to 2.
What happens when int division uses a zero divisor?
An int division or remainder operation with a zero divisor throws an ArithmeticException at run time.
What happens when an int operation exceeds its range?
An int overflow wraps around within the fixed int range instead of expanding. For example, 2,147,483,647 + 1 becomes -2,147,483,648.
How does casting double to int handle the fractional part?
Casting a double to int discards its decimal portion toward zero; (int) -7.9 evaluates to -7, not -8.
Why does cast placement matter in division?
Casting before division changes the operation: (double) x / y performs floating-point division, while (double) (x / y) casts an already-truncated integer result.
How does Math.round differ from an int cast?
Math.round(6.7) returns 7 as a long, whereas (int) 6.7 returns 6 by truncating toward zero.