Data Type

Intoduction

Size

  • Whole Number: long (involve larger range of number) > int

  • Decimal Number: double (involve more decimal places) > float

Wrapper Type

  • Number: Include all of the wrapper type

  • It is an object that include primitive type

Autoboxing

  • Turn Primitive type into Wrapper type

int a = 1;
//Autoboxing
Integer b = a;

Unboxing

  • Turn Wrapper type into primitive type

Integer a = 1;
//Unboxing
int b = a;

Wrapper vs Primitive

  • Wrapper type such as Integer is an Object but including primitive type , default is null

  • Primitive type such as integer default is 0

  • When returning a value as a json object, wrapper type is preferred

  • When involving large of calculation, primitive type is preferred, as autoboxing is not needed

Reference Type vs Value Type

Reference Type

List<String> stringList1 = new ArrayList<>();
List<String> stringList2 = stringList1;
stringList1.add("test");
stringList2.forEach(s -> System.out.println(s));
// test
  • When creating new object or new list , a variable (string1) is pointed to the address in memory of the object

  • When stringlist2 is equal to stringlist1, which means that stringlist variable is also pointed to the same memory of the object

  • So that, the result will be shown below

Value Type

  • Mostly on primitive type, e.g int, double, ...

int test1 = 1;
int test2 = test1;
test1++;
System.out.println(test2);
// 1

Generic

  • Generic is a parameterized type, to allow type to be parameter, so as to let the class work well with different classes

  • It is not suitable for primitive type

  • Example 1:

// typical example is List, which can work well with different types
List<String> StringList = new ArrayList<>();
...
  • Example2:

public class Dog<T,D> {
   private T name;
   private D age;

    public Dog(T name, D age) {
        this.name = name;
        this.age = age;
    }

    public T getName() {
        return name;
    }

    public void setName(T name) {
        this.name = name;
    }

    public D getAge() {
        return age;
    }

    public void setAge(D age) {
        this.age = age;
    }
}
List<String> stringList1 = new ArrayList<>();
List<String> stringList2 = stringList1;
stringList1.add("test");
stringList2.forEach(s -> System.out.println(s));

Dog<String,Integer> dog = new Dog<>("happy",18);
System.out.println(dog.getName());
System.out.println(dog.getAge());

Interface and Abstract Class

  • Abstract class cannot be declared as an instance, need to be extended by concrete class

  • Both only include the type of functions only but not the content of function, like the plot

  • Abstract Class can include non-abstract class , but interface cannot

  • Interface can be implemented by many functions , but abstract class can only be inherited by one function

  • An abstract class can have abstract and/or non-abstract methods, while an interface can only have abstract methods.

  • An abstract class can have final, static or non-static or non-final variables, while interfaces can have only final and static variables.

Static Type and Final Type

Static

  • The value cannot be changed by setter and belong to the class but not object , and this type of function can be called without declaring new Object. e.g: Math.round(1000); round is a static type of function

  • When we do not need to depend on the variable of the class on method and also want to apply singleton, we can use static method or variable

Final

  • The value is finalized and cannot be changed / override.

String vs String Buffer vs String Builder

String

  • String is immutable in Java. So it’s suitable to use in a multi-threaded environment. We can share it across functions because there is no concern of data inconsistency. String is a final class.

  • If the new operator is used to create a string, it gets created in the heap memory.

  • The + operator is overloaded for String. We can use it to concatenate two strings. Although internally it uses StringBuffer to perform this action.

  • Since String is immutable in Java, whenever we do String manipulation like concatenation, substring, etc. it generates a new String and discards the older String for garbage collection.These are heavy operations and generate a lot of garbage in heap. So Java has provided StringBuffer and StringBuilder classes that should be used for String manipulation.

String Buffer & String Builder

  • StringBuffer is thread-safe and synchronized whereas StringBuilder is not. That’s why StringBuilder is faster than StringBuffer

  • For String manipulations in a non-multi threaded environment, we should use StringBuilder else use StringBuffer class

Last updated

Was this helpful?