javaCore
javaCore
OOP
OOP stands for Object-Oriented Programming.
OOP is a programming paradigm that uses "objects" to design applications and computer programs.
Properties:
Encapsulation is the mechanism that binds together code and the data it manipulates, and
keeps both safe from outside interference and misuse.
In Java this achieved by using access specifiers such as public, private, protected and
default.
class Animal {
void eat() {
System.out.println("eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("barking...");
}
void work() {
super.eat();
bark();
}
}
-> Ouput: eating...
barking...
Polymorphism(tính ?a hình):
Polymorphism is the ability of a programming language to present the same interface for
several different underlying data types.
Overloading is the ability to define more than one method with the same
name in a class.
The methods must have different method parameters/ tham s?.
EX:
class A{
void sum(int a,int b){
System.out.println(a+b);
}
void sum(int a,int b,int c){
System.out.println(a+b+c);
}
Overriding means having two methods with the same method name and
parameters.
One of the methods is in the parent class and the other is in the child class.
Note: connot override static,final, private methods.
Ex: Same method "eat" of Animal, cow eats grass but lion eat meat.
Abstract class:
-> use abstract class when group of objects inheritate from the same class.
Interface:
-> use interface when group of objects have same behavior. How is a constructor
invoked in Java?
II. Java Core
1. Primitive data type
2.
Note :
The default type for floating-point literals in Java is double, which provides higher precision
compared to the float type
The BigDecimal data type in Java is used to represent floating-point numbers with higher
precision, making it suitable for financial and decimal-based calculations.
3.
Conditional statements
4.
Note: Ternanry Operator
6.
Reference data type
7.
provide a mechanism to work with objects by holding their memory addresses.
8.
When assigning one reference variable to another in Java, only the reference (memory address) is
copied, not the entire referenced object. Both variables will refer to the same object in memory.
9.
String
10.
String Literal vs. String Object
String Literal:
A string literal is a sequence of characters enclosed in double quotes (ch? ??nh s?n)
String literals are stored in the string pool.
String literals are immutable( can only get but not set())
String Object:
A string object is an instance of the String class.
String objects are stored in the heap memory.
String objects are mutable.
11.
Compare StringBuilder vs StringBuffer
StringBuilder:
StringBuilder is non-synchronized, which means it is not thread-safe.
StringBuilder is faster than StringBuffer because it is not thread-safe.
StringBuilder is used in a single-threaded environment.
StringBuffer:
StringBuffer is synchronized, which means it is thread-safe.
StringBuffer is slower than StringBuilder because it is thread-safe.
StringBuffer is used in a multi-threaded environment.
thread-safe: (N?u m?t object ???c t?o ra và ch? ???c s? d?ng bên trong method t?o ra nó,
thì nó ???c coi là thread safe)
12.
Java Date and Time
13.
Java 8 introduced the java.time package, which contains classes to represent date and time.
14.
Java 9 and later versions introduced the java.time.chrono package, which contains classes to represent
the calendar system.
15.
Wrapper class
16.
Note: When you create a Wrapper object using valueOf(), the JVM will check if there is already a
Wrapper object with the same value in memory. If there is, the valueOf() method will return that
object. If there is no Wrapper object with the same value in memory, the valueOf() method will create
a new object and return that object.
17.
Array
18.
Note : A variable arguments list, also known as varargs, is a special type of parameter that can be used
to pass a variable number of arguments to a function. The variable arguments list is always the last
parameter in the list of arguments.
19.
variable argument method:
a. List
b. Set - HashSet: - HashSet does not maintain the insertion order of elements. - HashSet does not
allow duplicate elements. - HashSet allows only one null element. - LinkedHashSet: - LinkedHashSet
maintains the insertion order of elements. - LinkedHashSet does not allow duplicate elements. -
LinkedHashSet allows only one null element. - TreeSet: - TreeSet stores elements in ascending
order. - TreeSet does not allow null elements. - Note : For two objects to be considered equal and
treated as duplicates by a Set implementation, the equals() method needs to return true,
indicating that the objects have the same state and are considered equal.
c. Map
-HashMap: - HashMap does not maintain the insertion order of elements. - HashMap allows only one
null key but multiple null values.
1. Generics
2. Generics in Java allow you to create classes, interfaces, and methods that operate on objects of
specified types.
3. Example : generic in ArrayList
4. Note:
The diamond operator (<>) is used to specify the type of the generic class.
Generic classes can have multiple type parameters.
The type parameter can be a class or an interface.
Overview:
No state
No side effects (pure functions)
Immutable variables
recursion instead of loops
Higher-order functions (functions as parameters and return functions). Ex:
Note:
Running program:
1. Multiple Thread
2. Note:
The range of thread priorities in Java is from 1 to 10.
The default priority assigned to a thread in Java is 5. The Thread class defines
NORM_PRIORITY as the default priority value.