Tutorial by Examples

As you use generic types with utility classes, you may often find that number types aren't very helpful when specified as the object types, as they aren't equal to their primitive counterparts. List<Integer> ints = new ArrayList<Integer>(); Java SE 7 List<Integer> ints = new A...
Due to auto unboxing, one can use a Boolean in an if statement: Boolean a = Boolean.TRUE; if (a) { // a gets converted to boolean System.out.println("It works!"); } That works for while, do while and the condition in the for statements as well. Note that, if the Boolean is null...
This code compiles: Integer arg = null; int x = arg; But it will crash at runtime with a java.lang.NullPointerException on the second line. The problem is that a primitive int cannot have a null value. This is a minimalistic example, but in practice it often manifests in more sophisticated f...
Autoboxing can come at a substantial memory overhead. For example: Map<Integer, Integer> square = new HashMap<Integer, Integer>(); for(int i = 256; i < 1024; i++) { square.put(i, i * i); // Autoboxing of large integers } will typically consume substantial amount of memory (...
Case 1: While using in the place of method arguments. If a method requires an object of wrapper class as argument.Then interchangeably the argument can be passed a variable of the respective primitive type and vice versa. Example: int i; Integer j; void ex_method(Integer i)//Is a valid statem...

Page 1 of 1