# 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

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

### Unboxing

* Turn Wrapper type into primitive type

```java
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&#x20;

```java
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&#x20;

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

```java
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:<br>

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

* Example2:

```java
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;
    }
}
```

```java
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&#x20;
* 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&#x20;

* 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**&#x20;

* 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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://petercheng7788.gitbook.io/developer-note/programming-language/java-concept/data-type.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
