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
Unboxing
Turn Wrapper type into primitive type
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
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, ...
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:
Example2:
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?