Skip to content

About

Refreshing Core Java Knowledge and learning about modern Java changes and features

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

171 Commits

Folders and files

Repository files navigation

Everything about Boolean and boolean in Java

Click to reveal.

Is boolean a primitive type or an object wrapper?

Click to reveal answer.
  A primitive data type.

Is Boolean a primitive type or an object wrapper?

Click to reveal answer.
  An object wrapper class.

Where is a local boolean variable typically stored in memory?

Click to reveal answer.
 On the **stack**. If its a local variable inside a method. In the heap if its a class field
image

Where is a Boolean object stored in memory?

Click to reveal answer.
 On the heap.

What is the default value of a boolean class field?

Click to reveal answer.
false

What is the default value of a Boolean class field?

Click to reveal answer.
null

Can a primitive boolean store a null value?

Click to reveal answer.
No.

Why is Boolean used instead of boolean in Java Collections?

Click to reveal answer.
Java Generics do not support primitive data types.

What is the term for automatically converting a boolean to a Boolean?

Click to reveal answer.
Autoboxing.

What is the term for automatically converting a Boolean to a boolean?

Click to reveal answer.
Unboxing.

Which method does the compiler inject during autoboxing of a boolean?

Click to reveal answer.
Boolean.valueOf()

Which method does the compiler inject during unboxing of a Boolean?

Click to reveal answer.
booleanValue()

What happens at runtime when Java attempts to unbox a null Boolean?

Click to reveal answer.
A NullPointerException is thrown.

Why is boolean preferred over Boolean for high-performance loops?

Click to reveal answer.
To avoid the memory overhead and GC pressure caused by creating wrapper objects.

What is the null-safe way to check if a Boolean variable is true?

Click to reveal answer.
Boolean.TRUE.equals(variable)

How many bits of information does a boolean logically represent?

Click to reveal answer.
1 bit.

Why does Boolean have more memory overhead than boolean?

Click to reveal answer.
It includes an object header in addition to the primitive value.

What is the short-circuit behavior of the && operator?

Click to reveal answer.
It skips the right-hand side if the left-hand side is false.

What is the short-circuit behavior of the || operator?

Click to reveal answer.
It skips the right-hand side if the left-hand side is true.

How does the & operator differ from && when used with booleans?

Click to reveal answer.
& evaluates both sides regardless of the first value (no short-circuiting).

How does the | operator differ from || when used with booleans?

Click to reveal answer.
| evaluates both sides regardless of the first value (no short-circuiting).

What is the result of true ^ true in Java?

Click to reveal answer.
false

Which operator returns true only if the two boolean inputs are different?

Click to reveal answer.
The XOR operator (^).

What is the result of the Logical NOT (!) operator applied to true?

Click to reveal answer.
false

Why should .equals() be used instead of == to compare two Boolean objects?

Click to reveal answer.
== compares memory references, while .equals() compares logical values.

What is the fundamental difference between boolean and Boolean in Java?

Click to reveal answer.
boolean is a primitive data type, while Boolean is an object wrapper class.

What is the default value of a boolean variable when declared as a class field?

Click to reveal answer.
false

What is the default value of a Boolean object reference when declared as a class field?

Click to reveal answer.
null

Why does a Boolean object have higher memory overhead than a boolean primitive?

Click to reveal answer.
It is stored on the heap and includes an object header plus the internal primitive value.

Why can't you store boolean primitives directly in a List or Map?

Click to reveal answer.
Java Generics do not support primitives; they require **object types** like Boolean.

What is the result of attempting to call a method like .toString() on a boolean primitive?

Click to reveal answer.
A **compilation error** (primitives do not have methods).

How does the Java compiler perform autoboxing for a boolean?

Click to reveal answer.
It automatically converts the primitive boolean to a Boolean object using Boolean.valueOf().

How does the Java compiler perform unboxing for a Boolean?

Click to reveal answer.
It extracts the primitive value by automatically calling the booleanValue() method.

What is the "Hidden Danger" of unboxing a Boolean object that is null?

Click to reveal answer.
It throws a NullPointerException (NPE) because the compiler tries to call .booleanValue() on a null reference.

Why is using Boolean in a massive loop for mathematical/logical operations inefficient?

Click to reveal answer.
It causes performance degradation due to constant object creation and increased Garbage Collection pressure.

Why is it dangerous to rely on == when comparing wrapper objects like Integer or Boolean?

Click to reveal answer.
The == operator compares memory references (addresses) rather than logical values.

How can you safely check if a Boolean object is true without risking a NullPointerException?

Click to reveal answer.
Use Boolean.TRUE.equals(yourObject).

How does the short-circuit AND operator (&&) behave if the left operand is false?

Click to reveal answer.
It skips evaluation of the right operand because the overall result is guaranteed to be false.

How does the short-circuit OR operator (||) behave if the left operand is true?

Click to reveal answer.
It skips evaluation of the right operand because the overall result is guaranteed to be true.

In what specific scenario is a short-circuit AND (&&) commonly used to prevent crashes?

Click to reveal answer.
To perform a null check before accessing a member: if (user != null && user.isActive()).

What is the difference between the logical AND (&&) and the bitwise AND (&) when used on booleans?

Click to reveal answer.
&& is short-circuiting, while & forces evaluation of both sides regardless of the first result.

What is the logical result of the Exclusive OR (^) operator?

Click to reveal answer.
It returns true only if the two inputs are different (one true, one false).

What is the result of the expression true ^ true in Java?

Click to reveal answer.
false

What does the logical NOT (!) operator do to a boolean value?

Click to reveal answer.
It flips the value (e.g., !true becomes false).

Java Access Modifiers and Non-Access Modifiers

Click to reveal.

What is an object header in Java?

Click to reveal answer.
An object header in Java is a small block of metadata stored at the beginning of every object in heap memory. It serves as the runtime identity card for the object, allowing the Java Virtual Machine (JVM) to perform memory management, garbage collection, and synchronization.

In the context of Java memory, where is a static field stored?

Click to reveal answer.
In the Metaspace (part of the class metadata), rather than within individual heap objects.

How many copies of a static field exist if a class has 1,000 instantiated objects?

Click to reveal answer.
Exactly one copy, shared by all instances.

What is the primary limitation of a static method regarding instance members?

Click to reveal answer.
It cannot access instance fields or instance methods because it lacks a this parameter.

Why can a static field be accessed even if no objects of that class have been created?

Click to reveal answer.
Because static members belong to the class itself and are initialized when the class is loaded.

What is the effect of the final keyword on a variable?

Click to reveal answer.
It ensures the variable can be assigned only once; its value becomes a constant.
image

When must a final instance field be initialized?

Click to reveal answer.
By the end of every constructor execution.
image

How does marking a method as final affect the inheritance hierarchy?

Click to reveal answer.
It prevents any subclass from overriding that specific method.

Java Concurrency in Practice

One of the challenges of developing concurrent programs in Java is the mismatch between the concurrency features offered by the platform and how developers need to think about concurrency in their programs.

Threads are the easiest way to tap the computing power of multiprocessor systems. As processor counts increase exploiting concurrency effectively will only become more important.

About

Refreshing Core Java Knowledge and learning about modern Java changes and features

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages