Skip to content

Instantly share code, notes, and snippets.

@WaltXin
Last active March 31, 2019 22:48
Show Gist options
  • Save WaltXin/9ba66c8a09e45f0ea1ba9bc5a8b7ae0b to your computer and use it in GitHub Desktop.
Save WaltXin/9ba66c8a09e45f0ea1ba9bc5a8b7ae0b to your computer and use it in GitHub Desktop.
Java Knowledge

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

List<Integer> li = new ArrayList<>();  
int i = 2;
li.add(i);

This is actually

li.add(Integer.valueOf(i));

Unboxing example

int m = li.get(0);

This is actually

int m = li.get(0).intValue();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment