Is there any difference between a = a + b and a += b expressions?
Yes. When adding byte or short variables Java promotes them to int first. a = a + b stores an int into a smaller type requiring explicit cast. a += b includes an implicit narrowing cast so it compiles without error
Why does a += b work for byte variables but a = a + b does not?
When adding byte or short variables Java promotes them to int first. a = a + b stores an int into a byte which needs an explicit cast. a += b has implicit casting built in so it compiles without error
MEMORY BOOSTER: a = a+b vs a += b
a = a + b = int promotion + may need explicit cast. a += b = same arithmetic + implicit cast back to variable type. For byte or short use += to avoid compile error. The += operator is a compound assignment that includes implicit narrowing cast
What happens when you put a return statement in a try or catch block - will finally execute?
Yes - finally executes even if there is a return in try or catch. The finally block always runs before the method actually returns
What happens when you call System.exit() from a try or catch block - will finally execute?
No - System.exit() terminates the JVM immediately so the finally block will NOT execute
MEMORY BOOSTER: finally block execution
finally ALWAYS runs EXCEPT when System.exit() is called. return in try/catch = finally still runs. System.exit() = JVM dies = finally skipped. Rule: finally runs unless the JVM itself stops
What does the expression 1.0 / 0.0 return in Java? Will there be a compilation error?
No compilation error and no ArithmeticException. It returns Double.INFINITY. Floating-point division by zero follows IEEE 754 rules and returns infinity
Why does integer division by zero throw an exception but floating point division does not?
Integers have no representation for infinity so int/0 throws ArithmeticException. Floating point follows IEEE 754 which defines special values: INFINITY - NaN and negative zero to handle these cases
MEMORY BOOSTER: Floating point division by zero
int / 0 = ArithmeticException. double / 0.0 = Double.INFINITY (no exception). 0.0 / 0.0 = Double.NaN. -1.0 / 0.0 = Double.NEGATIVE_INFINITY. IEEE 754 handles these gracefully for floating point
Can we use multiple main methods in multiple classes?
Yes - each class can have its own main method. JVM only looks for main in the class whose name is passed to the java command so there is no conflict
Can we have multiple methods named main in the same class?
Yes - but only one can have the signature public static void main(String[] args). JVM only recognizes that exact signature as the entry point. Other methods named main are just regular overloaded methods
MEMORY BOOSTER: Multiple main methods
Multiple classes can each have main() - no conflict. Same class can have multiple main() methods but JVM only calls: public static void main(String[] args). All others are regular overloaded methods ignored by JVM
Does Java allow you to override a private or static method?
No - private methods are not visible to subclasses so they cannot be overridden. Static methods can only be hidden not overridden. Creating a same-signature static method in a subclass is called method hiding not overriding
What is method hiding in Java?
Method hiding occurs when a subclass defines a static method with the same signature as a static method in the parent class. The method called depends on the reference type at compile time not the object type at runtime
MEMORY BOOSTER: Override private or static
private = not visible in subclass = cannot override. static = belongs to class = cannot override = can only hide (method hiding). Neither is true runtime polymorphism. Only instance non-private non-final methods can be overridden
What happens when you put a key object in a HashMap that is already present?
The old value is replaced by the new value. HashMap does not allow duplicate keys. The put() method returns the old value when replacing and stores the new value in the same bucket
MEMORY BOOSTER: Duplicate key in HashMap
put() with existing key = old value replaced + old value returned. HashMap never has duplicate keys. Same key = same hashCode = same bucket = equals() confirms match = value overwritten. put() return value is the OLD value (or null if new key)
How can you make sure that N threads can access N resources without deadlock?
Use resource ordering. Always acquire resources in the same fixed order and release them in reverse order across all threads. This eliminates circular wait which is one of the four conditions required for deadlock
MEMORY BOOSTER: Prevent deadlock with N resources
Resource ordering = all threads request resources in same fixed order. Thread A and B must both request Resource 1 then Resource 2 never in different orders. No circular wait = no deadlock. Simple but very effective technique
How can you determine if the JVM is 32-bit or 64-bit from a Java program?
Use System.getProperty(“sun.arch.data.model”) which returns 32 or 64 depending on the JVM bit size
MEMORY BOOSTER: Detect JVM bit size
System.getProperty(“sun.arch.data.model”) returns 32 or 64. Useful for code that behaves differently on 32-bit vs 64-bit JVM. Can also check at command line with java -d32 or java -d64
What is the right data type to represent Money in Java?
BigDecimal is the best choice. It supports decimal points and correct rounding. double can be used with predefined precision but floating point arithmetic can produce erroneous results for financial calculations
Why should you avoid using double for monetary calculations?
Floating point arithmetic with double is imprecise. For example 5 * 0.1 does not equal exactly 0.5 in Java due to binary representation of decimals. This imprecision is unacceptable for financial calculations
MEMORY BOOSTER: Money representation
Use BigDecimal for money - never double or float. BigDecimal = exact decimal arithmetic + controlled rounding. double = imprecise = dangerous for finance. 5*0.1 == 0.5 is FALSE in Java. BigDecimal uses more memory but correctness is worth it