Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Java basic knowledge
- #### Primitive type and Autoboxing/Unboxing
- Eg: int vs Integer
- int is primitive type, while Integer is a class (eg: Integer.parseInt("1") is a call of Integer's class method)
- All primitive type in Java has an equivalent wrapper class:
- * byte -> Byte
- * short -> Short
- * int -> Integer
- * long -> Long
- * boolean -> Boolean
- * char -> Character
- * float -> Float
- * double -> Double
- Wrapper class inherit from Object class and primitive don't.
- ##### Java 5 have autoboxing.
- Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object
- wrapper classes.
- Auto boxing example
- ```java
- List<Integer> li = new ArrayList<>();
- int i = 2;
- li.add(i);
- ```
- This is actually
- ```java
- li.add(Integer.valueOf(i));
- ```
- Unboxing example
- ```java
- int m = li.get(0);
- ```
- This is actually
- ```java
- int m = li.get(0).intValue();
- ```
Add Comment
Please, Sign In to add comment