Java FIFO queues: add, remove, and isEmpty
Add items to the tail of a FIFO queue, take them from the head, and check that the queue is not empty before calling `remove()`.
CKTIS / Knowledge base
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
Add items to the tail of a FIFO queue, take them from the head, and check that the queue is not empty before calling `remove()`.
Check whether an element belongs to a set and tell whether an addition or removal changed it. Use `LinkedHashSet` to track completed lessons without duplicates and in insertion order.
Use `String.valueOf` to turn a value into text. Learn why a missing exception message becomes the string `"null"`, while passing the bare `null` literal throws an exception.
`Throwable.getCause()` returns the immediate cause. Call it on each successive object to walk from the outer exception to the last available cause.
Read the message from a specific exception, handle a missing message, and distinguish the primary exception from its causes and suppressed exceptions.
Call `getSuppressed()` on the primary exception to inspect attached exceptions, such as a resource-closing failure that occurred after the `try` body failed.
In Java, + adds numbers or concatenates text when at least one operand has type String. Operand types, numeric promotion, and parentheses determine the result. Unary + preserves the value after type promotion.
The += operator adds a value to a variable or appends text to a String and stores the result, including an implicit conversion to the variable's type.
The expression array[index] selects an array element for reading or writing. Indices start at zero, and Java checks their bounds at run time.
The `=` operator stores a value in a variable; it does not test equality. It requires compatible types, and the assignment expression itself also produces a value, allowing assignments to be chained.
The & operator preserves bits that are 1 in both integral operands. Use it to mask values and test flags. With boolean values, it means AND without short-circuit evaluation.
The &= operator stores an AND result in its left operand: bitwise for integral values, logical for boolean values.
The unary ~ operator flips every bit of an integral value after numeric promotion. Its result has type int or long. It is not the logical complement operator !: ~ does not accept boolean values.
The | operator combines corresponding integer bits: a result bit is 1 when at least one input bit is 1. With boolean operands, it performs logical OR without short-circuiting.
The |= operator combines a variable’s value with the right operand using OR and stores the result. It supports integral and boolean values.
The ^ operator applies XOR to corresponding bits of integral values. For boolean values, it tests whether they differ.
The ^= operator stores an XOR result in its left operand. It works with integral types and boolean; it does not mean exponentiation.
The (type) operator explicitly converts a value to a specified type. Numeric casts can lose data, and reference casts can throw ClassCastException; they do not change an object's class.
The ?: operator selects one of two values based on a condition. It is an expression with three operands; only the selected branch is evaluated at run time.
The -- operator subtracts 1 and stores the result in a variable. The x-- form yields the previous value, whereas --x yields the new value. Both forms update the variable while evaluating the expression.
The / operator computes a quotient. Operand types determine whether division is integer or floating-point, affecting fractional results and the behavior of division by zero.
The /= operator divides a variable’s saved value by the right operand and stores the result back, with an implicit conversion to the variable’s type.
The == operator returns boolean: it compares primitive values or reference identity. For String objects it does not compare contents; use equals for that.
The >= operator compares numeric values and returns boolean. It includes equality, making it useful for checking thresholds that include the boundary value.