This week I’m mixing up the core java interview questions: all the questions are going to be about the number types in Java!
1. What is the difference between a byte, short, int, a long, a float and a double?
byte, short, int, long are all integer types (they don’t have decimal places; whole numbers only here). The only difference between them is the number of bytes they take up.
byte = 1 byte (8 bit)
short = 2 bytes (16 bit)
int = 4 bytes (32 bit)
long = 8 bytes (64 bit)
Integer values are stored as twos compliment. The maximum value of a byte is 128, whereas for a short it is 32,767, int is 2^31-1 and long 2^63-1.
float and double are real numbers (e.g. have decimal places)
2. What syntax can be used when assigning integer types?
Integer types can be assigned to
- Regular numbers:. int i = 101
- Octal: int i = 013
- Hex: int i = 0xABCABCAB
- Postfixed with l or L for long:long l = 12ll long t = 0x12321232L
3. What syntax can be used when assigning real types?
- double types using d or D: double d = 15.0d
- float types using f or F: float f = 17.0f
- e can be used for scientific notation: float f = 212e-23f (212 to the power of negative 23)
4. What restrictions are there on using the different types with each other?
It is perfectly fine to cast to a data type which has more space.
byte b = 121;
int i = b;
Is completely fine. However when attempting to assign in the opposite direction a cast is required.
5. What is Autoboxing?
int, long etc. are examples of primitive types in Java. Each has an object representation such as Integer and Long. Autoboxing was introduced in Java 8 to automatically convert between these types. For example:
Integer j = 4;
Is an autoboxing operation. The JVM is automatically converting “4” into an Integer object with value 4. This works in both directions too.
int i = new Integer(4);
6. Why do we care about autoboxing?
It can cause a performance hit. Depending on your application it probably doesn’t matter, from the spec:
“It is not appropriate to use autoboxing and unboxing for scientific computing, or other performance-sensitive numerical code. An
Integer is not a substitute for an
int; autoboxing and unboxing blur the distinction between primitive types and reference types, but they do not eliminate it.”
If you have a piece of code that is being repeatedly hit that involves autoboxing, it may be worth looking to remove it.
7. What is the result of the following java code?
System.out.println(((Integer)0) == ((Integer)0)); System.out.println(((Integer)200) == ((Integer)200));
It depends on your JVM. On mine it shows
But it may be different on your machine; this is a JVM optomisation to create a cached instance of Integer values under a certain amount: for integers under a certain value it does not create a new object but instead reuses an existing, cached one.
8. How do you convert from a String to an integer type and vice versa?
Use Integer.parseInt(“123″). This throws a NumberFormatException if the String cannot be converted, which is a RuntimeException.
To convert from Integer to String, user Integer.toString(i).
9. long l = 1/0 : Will this expression compile? What is the result if so?
This will compile but result in a java.lang.ArithmeticExpresion, specifically, “java.lang.ArithmeticException: / by zero”.
10. When should I use a float or a double?
Generally it is better to prefer double unless there is a very specific reason not to. A float is half the size of a double and can be faster on some processors, however double can be faster on some modern processors. There is a wonderful piece of advice on StackOverflow:
“Don’t use float. There is almost never a good reason to use it and hasn’t been for more than a decade. Just use double.”
A float is single (32 bit) precision whereas a double is (unsurprisingly) double (64 bit) precision.