[Java] Baekjun number 2557 - Hello World
We have summarized the basic structure of a Java program and the solution for printing Hello World through the main method.
한국어 원문은 여기에서 볼 수 있습니다.
[Java] Baekjun number 2557 - Hello World
Solution
1
2
3
4
5
public class Main {
public static void main(String args[]) {
System.out.print("Hello World!");
}
}
For Java in Baekjun
1
2
3
4
public class Main {
public static void main(String args[]) {
}
}
You should write the main method as follows:
- The main method operates first when running a Java application (
main), can be accessed from any object (public), is defined the moment Java is compiled (static), and is a function that does not return a value (void).
As I usually solve problems using programmers, I was confused because often I just had to put the answer in the answer.
main method in java
- Because program startup is the main method, when a Java application is executed, the main method is executed first.
public: Access modifier, means the object can be referenced from anywhere. - A type of restriction that can be accessed from the outside
- The types are in the order of strongest constraints:
private -> protected -> publicdefault: Can be accessed from the same package as inside the classstatic: This means that this function is a static function. If a function or class is declared withstatic, the corresponding object is defined the moment Java is compiled -> Afterwards, objects other thanstaticare defined.- Therefore, it is impossible to call an object other than
staticfrom astaticobject. - Because
staticis defined first, objects that have not yet been defined cannot be called.void: Only executes, no return value -> Just moves to the called part after the function ends.String args[]: Used to receive values from outside when executing a program.1
java Main hello 123
1 2 3 4
public static void main(String[] args) { System.out.println(args[0]); // hello System.out.println(args[1]); // 123 }
- The received hello and 123 values are entered into the args array.
- In KOTE, input is mostly received through Scanner and BufferedReader, so it is rarely used.
- Therefore, it is impossible to call an object other than
Output from java
print: simply prints the content in parentheses printf: Same as printf in C, used to write %d, %s, etc. println: After printing the content in parentheses, a newline character (\n) is included at the end, leaving a single line space after printing.