Java

Updated:

Primitive variable

byte a;  // Integer type 1byte
short b; // 2byte
int c;   // 4byte
long d;  // 8byte

float e;  // Real number type 4byte
double f; // 8byte

char g; // Character type 2byte

boolean h; // Logic type

Reference Variable

//ex)
String b; // b has an address, not a value
ClassType className = new ClassType();          // There are countless reference variables.

Instance variable and Class variable

public class Variable {
    int a;        // Variables declared within a class
    static int b; // Variables declared within a class with static keyword. A variable that shares a value. Can be called with class name.variable.
}

Local variable and Parameter

public class Variable {
    void add(int c){
        int d = c; // d is local variable, c is parameter.
    }
}

Constructor

public class Constructor{
    int a;
    int b;
    
    Constructor(){} // Default Constructor. Compiler automatically generates when uncoded.
    
    COnstructor(int a, int b){ // Constructor override
        this.a = a
        this.b = b
    }
}

Creating object

public class Constructor{
    int a;
    int b;
    
    public static void main(Stirng[] args){
        Constructor c = new Constructor(); // Creating object. c is object.
        
        c.a = 1;
        c.b = 0;
    }
}

Conditional statement

int a = 1;
int b = 2;
if(a < b){
    // b is bigger than a
}else if(a > b){
    // a is bigger than b
}else{
    // a = b
}

switch(value){ // If value is 1, case 1 operate
    case 1 :
          // Method
          braek;
    case 2 :
          // Method
          braek;
    case 2 :
          // Method
          braek;
    default :
          // Method
}

Loop

for(int i = 0; i < n; i++){
    // It repeats n times.

}

int i = 0;
while(i < n){
    // It repeats n times.
    i++;
}

Pattern

DTO(Data Transfer Object), AO(Value Object)

public class DTO{ // But, VO read only
    private int number;
    
    DTO(){}
    
    DTO(int number){
        this.number = number;
    }
    
    public int getNumber(){
        return number;
    }
    
    public void setNumber(int number){
        this.number = number;
    }
}

DAO(Data Access Object)

public class DAO {

    public void add(DTO dto) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/test", "test", "test");

        PreparedStatement preparedStatement = connection.prepareStatement("insert into users password=?");

        preparedStatement.setString(1, dto.getNumber());
        preparedStatement.executeUpdate();
        preparedStatement.close();
        
        connection.close();

    }
}

MVC pattern

MVC_Diagram_3

public class DTO {
	private int number;
    
    DTO(){}
    
    DTO(int number){
        this.number = number;
    }
    
    public int getNumber(){
        return number;
    }
    
    public void setNumber(int number){
        this.number = number;
    }
	
}
public class DAO {

    public static boolean add(DTO dto) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/test", "test", "test");

        PreparedStatement preparedStatement = connection.prepareStatement("insert into users password=?");

        preparedStatement.setInt(1, dto.getNumber());
        preparedStatement.executeUpdate();
        if(preparedStatement.executeUpdate() == 1){
            return true;
        }
        return false;
        
        preparedStatement.close();
        connection.close();
        
    }
}
public class Service {
    public static boolean add(DTO dto) throws ClassNotFoundException, SQLException {
        return DAO.add(dto);
    }
}
public class Controller {
    public static void test(DTO dto){
        try{
            if(service.add(dto) == true){
                EndView.printMessage("Success");
            }
        } catch(Exception e){
            //
            Endview.failMessage("Fail");
        }
        //
    }
}
public class EndView {
    public static printMessage(String m){
        //
    }
    
        public static failtMessage(String m){
        //
    }

}
public class StartView {
    public static void main(Stirng[] args){
        Controller.test(dto);
    }
}

Inheritance

It is only possible ‘is a’ relationship
ex) people is customer (x)
customer is people (o)

public class Student extends Teacher
public class Teacher extends School
public class School

super(): Super() brings a parent’s constructor.

Polymorphism

The higher type accepts all lower types.
This maximizes the usability of the code.

class Test{
    public void a(int test){
        System.out.println(test);
    }
    public void a(String test){
        System.out.println(test);
    }
}
public class PolymorphismOverloadingDemo {
    public static void main(String[] args) {
        Test test = new Test();
        test.a(1);
        test.a("one");
    }
}

Properties

It has a function to utilize the contents of the file.

#db.properties
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:xe
jdbc.id=SCOTT
jdbc.pw=TIGER

Exception

exception
Checked Exception: No rollback. must be processed.
Unchecked Exception: Rollback.

try{
    //
}catch(Exception e){
    //
    log.error("Fail update :", e);
}finally{
    //
}

Datastructure

자바자료구조

List<Test> list = new Arraylist<Test>(length); // Can be resized


List<E> list = new Vector<E>(); // Because it consists of synchronized methods, 
                                // multi-threads cannot execute these methods at the same time, 
                                // and only if one thread completes the execution can another execute.
                                
				
 List<E> list = new LinkedList<E>(); // When you remove or insert objects from a particular index, 
                                     // only the front and back links change and the rest of the links do not change.
                                     
				     
Set<String> setExample = new...;        // Do not allow duplicate storage of the same data.
Iterator<String> iterator = setExample.iterator(); // It does not maintain the order in which data is stored.
while(iterator.hasNext()){
    String getin = iterator.next();
}


Set<E> set = new HashSet<E>(); // If a string is stored in the HashSet, a String object with the same string is considered an equivalent object, 
int index = key.hashCode() % capacity // and a String object with different strings is considered a different object.


Map<K,V> map = new HashMap<K,V>(); // The key and value allow null values.

Categories:

Updated: