1) Can you predict the output of the following code.
class Problem1
{
public static void main(String[] args)
{
System.out.println(Boolean.getBoolean("true"));
}
}
Predicted output:
true
Real output:
false
Analysis:
Boolean.getBoolean searches for that system property (WHAT!!!) and if that is equalIgnoreCase to "true" then only this will return true. In all cases this will return false. So, in this case the output is false.
How to overcome this?
As we use parseInt to convert String to int, parseFloat to convert String to float, we need to use parseBoolean.
System.out.println(Boolean.parseBoolean("true")) will return true.
2) What does the following program print?
class Problem2
{
public static void main(String[] args)
{
long num = 10_000 * 10_000 * 10_000 * 10_000;
System.out.println(num);
}
}
Predicted output:
1 followed by 16 zeros.
Real output:
a negative value
Analysis:
Negative number output implies a overflow but howcome this can happen as I have used long to store this value?
Whenever we use a number, by default, it is assumed to be int. All the operations which we do on this number will be treated as int. So in this example by the time we do the 3rd multiplication the result will not fit in the int and returns a negative number.
We need to use L after the number to treat the number as long. One more glitch here is that the overflow will still happen if we put L as part of the last number. That is,
num = 10_000 * 10_000 * 10_000 * 10_000L will also overflow because by the time the compiler sees L, the result would have already overflowed.
How to overcome this?
Put L after the first number or introduce a 1L and multiply the rest of the numbers.
long num = 1L * 10_000 * 10_000 * 10_000 * 10_000;
will store num having 1 followed by 16 zeros.
3) What does the following program print?
class Problem3
{
public static void main(String[] args)
{
java.util.List<Integer> ints = new java.util.ArrayList<>();
for (int i = 1; i <= 5; i++)
{
ints.add(i);
}
ints.remove(3);
System.out.println(ints);
}
}
Predicted output:
[1, 2, 4, 5]
Real output:
[1, 2, 3, 5]
Analysis:
In this problem I add integers from 1 to 5 to the list and I remove 3 from the list. I expect it to print [1, 2, 4, 5]. Is this true? Think about it.
The problem is due to the introduction of autoboxing and unboxing features in JDK 5. In List I can add only Integer objects and not primitives. To make our life simpler, Java Compiler takes care of auto boxing the int value to Integer object. So, after the end of the loop we have a list with contents [1, 2, 3, 4, 5]. True. But remove is confusing. If we look at the documentation, we have 2 remove methods. One removes based on the index and other removes Objects. In our case which one will the compiler pick? As we have a method which takes an int, there is an exact match to call that method. The value 3 is treated as primitive itself and it is not autoboxed to Integer object 3. The effect is that we remove the value present at the index 3 which is 4.
How to overcome this?
ints.remove((Object) 3) will make the compiler call the remove(Object obj) method and this will return [1, 2, 4, 5].
No comments:
Post a Comment