8 Java data types interview Q&As

Q1. How would you go about choosing the right data types for your application?
A1. Java is what is known as a strongly typed language. This means Java only accepts specific values within specific variables or parameters. Some languages, such as JavaScript, PHP, and Perl are weakly typed languages.

1. Know the data limits to prevent any data overflow

Java primitive data types

Java primitive data types

2. Prefer immutable wrapper objects to primitives.

Each primitive data type has a corresponding wrapper class like Integer, Long, Character, Float, Double, etc. There are 8 primitive variables and as many wrapper objects. In Java 5, additional wrapper classes like AtomicInteger, AtomicLong, AtomicBoolean and AtomicReference were introduced to provide atomic operations for addition, increment, and assignment. These additional classes are mutable and cannot be used as a replacement for regular immutable wrapper classes.

Q2. What are wrapper classes, and why do you need them?
A2. The wrapper classes are a good example of the decorator design pattern. They decorate the primitive values, and enhance their behavior by providing immutability, atomicity, null checking, etc.

1) Wrapper objects can be initialized to null. This can’t be done with primitives. Many programmers initialize numbers to 0 or -1 to signify default values, but depending on the scenario, this may be incorrect or misleading.

2) Wrapper objects are very useful for optional data. Databases almost always have a significant fraction of their fields as optional (that is, as possibly NULL). In addition, the forms submitted in Web applications can contain optional parameters. Both NULL database fields and missing request parameters naturally map to null object references. With primitives, there is no such natural mapping.

3) Wrapper objects will also set the scene for a NullPointerException when something is being used incorrectly, which is much more programmer-friendly as it fails fast than some arbitrary exception buried down the line. Preferably, check for null early on in the method and report it immediately where applicable to adhere to the fail fast principle.

4) The wrapper objects are immutable, hence inherently thread-safe. Other threads can only read the values set by the thread that initialized this object.

5) When you create wrapper objects, use the valueOf( ) static factory method for efficiency.

The second approach is in fact an implementation of the flyweight design pattern.

Q. When to prefer primitives?
A. Primitives are faster to create and use than wrapper objects. Wrapper objects need to be auto-unboxed before use. Thus there is an extra step for the JVM to perform. For example, in order to perform arithmetic on an Integer, it must first be converted to an int before the arithmetic can be performed. In many business applications this rarely matters unless you were writing something very number-crunching or profiling indicates that the auto-boxing is a performance or memory issue in a particular part of your code as it is executed very frequently.

Anti-pattern: Watch out for premature-optimization anti-pattern where you are tempted to code for a perceived performance gain and sacrificing good design and maintainability.

Q3. When working with floating-point data types, what are some of the key considerations?

1. Never compare float or double with “==” or != operator

2. Use long, int, or BigDecimal for storing money, and performing monetary calculations.

Floating point data types like float, double, Float, or Double can result in inaccurate results. use either the BigDecimal or int/long representing the value in its lowest units like cents.

Q4. What is your understanding of widening versus narrowing conversions of primitive data types?
A4. Left to right (e.g. byte to short) is a widening conversion and considered safe because there is no chance for data loss. For example, byte has a range between -128 and 127 and short has a wider range between -32768 and 32767. So when you go from left to right, the data types are implicitly cast by the compiler since it is safe to do so.

Java primitive data types

Java primitive data types

Right to left (e.g. short to byte) is a narrowing conversion and considered unsafe because there is a chance for data loss. So when you go from right to left, the compiler expects you to explicitly cast the data to clearly state that it is safe to do so. If you do not cast explicitly, you will get a compile-time error. For example,

Note: byte and short are signed data types and they cannot be implicitly cast to unsigned char data type even though it is a widening conversion.

Q5. What are the dangers of explicit casting?
A5. Not knowing the MIN and MAX values can result in unexpected results due to loss of data during narrowing.

Trap #1 Be careful when casting explicitly.

Trap #2: Simple binary operations apply ‘binary numeric promotions’

The binary numeric promotion rule automatically casts each operand to the size of the larger operand type. If neither operand is larger, then both are cast to the same type. In byte b = b + 10, the value 10 is of type int and is the larger operand type compared to b, which is of type byte, hence will be evaluated as follows:

Above code throws a compile time error because b+10 evaluates to an int. You need an explicit cast to convert an int to byte as it is a narrowing conversion.

Trap #3: Compound Operators, such as +=, -=, etc contain an explicit cast

In byte b+=10 compound binary operation, you may be thinking that b+=10 will be expanded as b = b + 10. But it is really not correct. This is because the compound operations will have an explicit cast in the converted byte code.

performing a compound assignment operation (e.g. +=, -=, *=, etc). You may be thinking that b+=10 will be expanded as b = b + 10. But it is really not correct. This is because the compound operations will have an explicit cast even though it is not shown in the code.

Q6. Do you think the following code will throw a compile-time exception? If yes, how will you fix it?

A6. Yes. It is cast first and then divided as casting operator has precedence over division operator as per the precedence table. So the above code is equivalent to

To fix it, you need to get the division operator to evaluate prior to casting. You can achieve this by introducing a parenthesis around the division as parenthesis has higher precedence (in fact highest) than casting as per the precedence table.

Q7. What is the output of the following code snippet?

A7. x=6, y=7, z=6

line 1: x=5, y=0, z=0
line 2: x=6, y=6, z=0;
line 3: x=6, y=7, z=6

You need to understand the pre and post increment operators to get this right. ++x is a pre-increment and x++ is a post increment. ++x means x is incremented before being used and x++ means x is incremented after being used. So line2 increments x by one and then assign it to y. Whereas in line 3, z is assigned the old y value (i.e. prior to incrementing) of 6 and then y value is incremented to 7.

As you may rightly ask that as per the precedence table, both pre-increment (i.e. ++expr) and post-increment (i.e. expr++) operators do have precedence over the assignment operator (i.e “=”). The line 3 int z = y++; is roughly evaluated as follows:

Q8. Can you list some practical applications where the bitwise operations can be applied?

Example 1: To pack and unpack values.

For example, to represent

  • age of a person in the range of 0 to 127. Use 7 bits.
  • gender of a person 0 or 1 (0 – female and 1 – male). Use 1 bit.
  • height of a person in the range of 0 to 255. Use 8 bits.

To pack this info: (((age << 1) | gender ) << 8 ) | height. For example, age = 25, gender = 1, and height = 255cm. Shift the age by 1 bit, and combine it with gender, and then shift the age and gender by 8 bits and combine it with the height.

Bitwise operations

Bitwise operations


packed binary=11001111111111

Screen shot 2014-10-01 at 6.15.38 PM

Example 2: To compactly represent a number of attributes like being bold, italics, etc of a character in a text editor.

This is a more practical example.

Screen shot 2014-10-01 at 6.16.59 PM

Example 3: If you can think of anything as slots or switches that need to be flagged on or off,

you can think of bitwise operators. For example, if you want to mark some events on a calendar.

Screen shot 2014-10-01 at 6.19.37 PM

Example 4: To multiply or divide by 2n

300+ Core Java Interview Q&As
Module 1 Java Overview+
Unit 1 17 Java Overview Interview Q&As  - Preview
Unit 2 4 Java Compile-time Vs Runtime Interview Q&As  - Preview
Module 2 Java data types-
Unit 1 8 Java data types interview Q&As  - Preview
Unit 2 7 Java primitives & objects memory consumption Q&As  - Preview
Unit 3 4 Java autoboxing & unboxing interview Q&As  - Preview
Unit 4 12 Java String class Interview Q&As  - Preview
Module 3 Java modifiers, annotations, initializers & constructors+
Unit 1 6 Java Modifiers every interviewer seems to like  - Preview
Unit 2 10 Java initializers, constructors, regular methods and static factory methods Q&As
Unit 3 8 Java Annotations interview Q&As
Unit 4 4 Java annotation types & processing interview Q&As
Module 4 Java classes, interfaces & class loaders+
Unit 1 12 Java classes and interfaces interview Q&As
Unit 2 3 Abstract classes Vs interfaces interview Q&As
Unit 3 3 Java class loading interview Q&As to ascertain your depth of Java knowledge
Module 5 Java Objects Interview Q&As+
Unit 1 5 Java Object class methods interview Q&As  - Preview
Unit 2 3 Object wait( ) & notify( ) interview Q&As
Unit 3 7 Object equals Vs == and pass by reference Vs value interview Q&As
Unit 4 10 Java immutable objects interview Q&As
Unit 5 10 Java serialization, cloning, and casting interview Q&As
Module 6 OOP & FP+
Unit 1 10 Java OOP Interview Q&As
Unit 2 5 Inheritance Vs Composition OOP Interview Q&As  - Preview
Unit 3 2 Polymorphism OOP Interview Q&As
Unit 4 8 OOP Design principles interview Q&As
Unit 5 5 OOP Vs FP, FP concepts & where to use FP Q&As  - Preview
Unit 6 19 Java Functional Programming (i.e. FP) interview Q&As
Unit 7 Java FP Lambda expressions by examples
Module 7 Generics & Java Collection Framework(JCF)+
Unit 1 12 Java Generics Interview Q&As  - Preview
Unit 2 5 Java Generics Wildcards Interview Q&As
Unit 3 17 Java Collection Framework (JCF) interview Q&As
Unit 4 9 Java data structures interview Q&As
Unit 5 4 Sorting objects in a Collection interview Q&As
Unit 6 8 Java Map interface & Caching interview Q&As
Unit 7 Lambda expressions to work with Java 8 Collections
Module 8 Java multithreading+
Unit 1 17 Java multi-threading interview Q&As  - Preview
Unit 2 7 Java locks & synchronized keyword interview Q&As
Unit 3 9 blocking, scheduling & daemon threads interview Q&As
Unit 4 7 Java Executor framework Interview Q&As
Unit 5 ExecutorService Vs Fork/Join & Future Vs CompletableFuture Interview Q&As
Unit 6 7 Java ThreadLocal interview Q&As
Unit 7 Java Thread stop, resume, suspend & interrupt interview Q&As
Unit 8 12 Java multi-threading differences between X and Y Q&As
Unit 9 Java multi-threading 15 scenarios interview Q&As
Module 9 What is wrong with this multithread code?+
Unit 1 5 Ways to debug Java thread-safety issues  - Preview
Unit 2 Heap Vs Stack, Thread safety & Synchronization  - Preview
Unit 3 Deep Vs Shallow comparison & multithreading
Unit 4 An excellent written test multithreading question - wait/notify
Unit 5 Multithreading & Locks
Unit 6 Java Collection & ConcurrentModificationException
Unit 7 HashMap & Race condition
Unit 8 ConcurrentHashMap & Atomic operations
Unit 9 JConsole for debugging deadlocks in Java
Unit 10 jvisualvm to debug deadlocks in Java
Module 10 Exceptions Handling+
Unit 1 5 Java exception handling interview Q&As
Module 11 Java I/O & NIO+
Unit 1 15 Java old I/O and NIO (i.e. New I/O) interview Q&As
Unit 2 Java 8 way to reading files
Module 12 JVM+
Unit 1 11 JVM memory model and Atomicity, Visibility, and Ordering interview Q&As
Unit 2 8 Java Garbage Collection interview Q&As to ascertain your depth of Java knowledge
Unit 3 jvisualvm to sample Java heap memory
Unit 4 5 JMX and MBean interview Q&As
Module 13 What is new?+
Unit 1 Java 7 features list
Unit 2 Java 8 features list
Module 14 Java code quality+
Unit 1 How will you go about improving on the following Java code?
Unit 2 How will you go about ensuring code quality in Java apps?
Unit 3 5 Java unit testing interview Q&As
Unit 4 Mocks, stubs, domain, and anemic objects interview Q&As
Unit 5 30+ Java Code Review Checklist Items
Module 15 Debugging Java applications+
Unit 1 7 debugging Java interview questions & answers
Unit 2 Debugging "NoSuchMethodError" in Java
Unit 3 Debugging JAR hell issues in Java
Unit 4 Debugging Java version issues
Unit 5 Remote debugging in Java with Java Debug Wire Protocol (JDWP)
Unit 6 Debugging like a pro with eclipse IDE tutorial for Java developers
Module 16 Performance considerations & memory management+
Unit 1 4 Java application performance considerations interview Q&As
Unit 2 8 Java memory management interview Q&As
Unit 3 jvisualvm to detect memory leak
Module 17 Design Patterns Interview Q&As+
Unit 1 12 Java design patterns interview Q&As
Unit 2 5 Java singleton design pattern interview Q&As
Unit 3 Java Proxy, Decorator, Adapter, Bridge, and Facade design patterns Interview Q&As
Unit 4 3 Flyweight design pattern Interview Q&As to improve memory usage & performance
Learn by categories on the go...
Learn by categories such as FAQs – Core Java, Key Area – Low Latency, Core Java – Java 8, JEE – Microservices, Big Data – NoSQL, Architecture – Distributed, Big Data – Spark, etc. Some posts belong to multiple categories.