Java Programming
113 flashcards by MentisLibrary
Java as working developers use it: primitives and objects, classes interfaces and records, generics and type erasure, the collections framework, streams and lambdas, threads and executors, exceptions, and the JVM and build tooling.
- What is the signature of a Java entry point method?
- public static void main(String[] args). It must be public and static so the JVM can call it without an instance, return void, and take an array of strings.
- Name Java's eight primitive types.
- byte, short, int, long, float, double, char and boolean. They hold values directly and are not objects.
- What is the difference between a primitive variable and a reference variable?
- A primitive variable holds the value itself. A reference variable holds a reference to an object on the heap, so assignment copies the reference and both names see the same object.
- What is fixed about a Java array once created?
- Its length. arr.length is a final field, not a method, and there is no way to grow the array; you allocate a new one and copy.
- When can you use the enhanced for loop?
- Over any array or any type implementing Iterable. It gives you each element in order with no index.
- Why does a switch statement need break?
- Because cases fall through by design: once a label matches, execution continues into the following cases until a break or the end of the block.
- What does the arrow form of switch change?
- Each label runs only its own branch with no fall through, and the switch can be used as an EXPRESSION that produces a value.
- What does / do with two integers?
- Integer division: the fractional part is discarded, truncating toward zero. % gives the remainder, which keeps the sign of the dividend.
- What does var do in a local declaration?
- It infers the variable's type from the initializer at compile time. The variable is still statically typed and its type never changes.
- What does final mean on a variable, a method and a class?
- On a variable it can be assigned only once. On a method it cannot be overridden. On a class it cannot be extended.
- What does static mean on a member?
- It belongs to the class rather than to any instance: one copy shared by all objects, callable as ClassName.member with no instance.
- What does the pattern form of instanceof add?
- It tests the type and binds a typed variable in the same expression, so the explicit cast disappears.
- How do you declare a method taking any number of arguments?
- With varargs: Type... name as the LAST parameter. Inside the method it is an ordinary array.
- How do && and & differ on booleans?
- && short-circuits: the right operand is not evaluated when the left already decides the result. & always evaluates both.
- What are the wrapper classes, and what is autoboxing?
- Integer, Long, Double, Boolean and the rest wrap a primitive as an object. Autoboxing is the compiler inserting the conversion for you in both directions.
- Why can == on two Integer objects be true for small numbers and false for large ones?
- Integer.valueOf caches boxed values from -128 to 127, so small values are the same object. Larger values allocate a new object each time.
- Why is String immutable in Java?
- Its character data never changes after construction, so strings can be shared safely between threads, cached in the string pool, and used as reliable hash map keys.
- Why does == sometimes work on strings and sometimes not?
- == compares references. Compile-time literals are interned into the string pool, so identical literals are the same object; strings built at run time are not.
- When should you use StringBuilder?
- When building a string in a loop or from an unknown number of parts. It appends into a growable buffer and produces the string once.
- What does String.format give you over concatenation?
- A template with typed conversions, so numbers, padding and locale-aware formatting are expressed once instead of assembled by hand.
- What happens when an int overflows in Java?
- It wraps silently using two's complement arithmetic. There is no exception unless you use the Math.addExact family, which throws ArithmeticException.
- Why must money never be stored in a double?
- double is binary floating point, so decimal fractions such as 0.1 have no exact representation and errors accumulate over repeated arithmetic.
- What does a char actually hold?
- A single 16-bit UTF-16 code unit, not a full character. Characters outside the basic multilingual plane occupy two char values.
- What does an enum give you over integer constants?
- A fixed set of typed instances, so only declared values are possible. Enums can hold fields, define methods, and are usable in switch and EnumMap.
- What does declaring a record generate?
- A final class with private final fields, a canonical constructor, accessors named after the components, and value-based equals, hashCode and toString.
- What is Optional for, and what is it not for?
- Expressing a possibly-absent RETURN value so the caller must handle the empty case. It is not intended for fields or parameters.
- When does Java convert numeric types automatically?
- Widening conversions are implicit and lossless: int to long to float to double. Narrowing conversions need an explicit cast and can silently lose data.
- What does a constructor do that a method does not?
- It runs when the object is created, has the class's name, declares no return type, and cannot be called again on an existing object.
- How does one constructor call another in the same class?
- With this(...) as the very first statement. super(...) calls a superclass constructor and must also be first, so a constructor uses one or the other.
- What are Java's four access levels?
- public everywhere, protected in the package and in subclasses, package-private with no keyword within the package only, and private within the class.