티스토리 뷰

JAVA

main method in Java

seoca 2018. 12. 31. 11:24


Main method


the main method is the first method when we learn Java to print "Hello World". It's the entry point of Java program. The syntax is fixed and can't be changed. Only the name of the String. for instance, you can change args below.



public static void main(String[] args)


public 

Java runtime 이 이 메소드를 실행 할 수 있도록 public 으로 설정


static

인스턴스화 하지 않아도 static메서드는 호출가능


void

main method 는 return type이 없기 때문에 void


main 

main method 의 이름


String[] args

Java main method는 String array 타입의 argument 하나만 허용(=java command line arguments)

args is a changeable name.






import java.util.Scanner;
 
public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();
        double d = scan.nextDouble();
        scan.nextLine();

        String s = scan.nextLine();
        
        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}
 



Reference

Hackerrank (https://www.hackerrank.com/)

w3schools (www.w3schools.com)


'JAVA' 카테고리의 다른 글

Nested if-else example  (0) 2019.01.02
instance in Java  (0) 2018.12.31
Java Data type (primitive & non-primitive)  (0) 2018.12.18
Java loop example  (0) 2018.12.18
Scanner class in Java(User input)  (0) 2018.12.18