Guest User

Untitled

a guest
Jun 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. # Java basic knowledge
  2.  
  3. #### Primitive type and Autoboxing/Unboxing
  4. Eg: int vs Integer
  5. int is primitive type, while Integer is a class (eg: Integer.parseInt("1") is a call of Integer's class method)
  6. All primitive type in Java has an equivalent wrapper class:
  7. * byte -> Byte
  8. * short -> Short
  9. * int -> Integer
  10. * long -> Long
  11. * boolean -> Boolean
  12. * char -> Character
  13. * float -> Float
  14. * double -> Double
  15.  
  16. Wrapper class inherit from Object class and primitive don't.
  17. ##### Java 5 have autoboxing.
  18. Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object
  19. wrapper classes.
  20.  
  21. Auto boxing example
  22. ```java
  23. List<Integer> li = new ArrayList<>();
  24. int i = 2;
  25. li.add(i);
  26. ```
  27. This is actually
  28. ```java
  29. li.add(Integer.valueOf(i));
  30. ```
  31. Unboxing example
  32. ```java
  33. int m = li.get(0);
  34. ```
  35. This is actually
  36. ```java
  37. int m = li.get(0).intValue();
  38. ```
Add Comment
Please, Sign In to add comment