I first came into contact with Java in the training for new graduates, so I read Hello World as an output. Click here for the source code to read.
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Hello World
1.public class Main {} It seems difficult to read all at once, so let's break down the source code. First of all, from this part on the outermost side.
public class Main {
}
1-1.class Main
"Class Main" means that the whole block (the range enclosed by {}) is ** class ** and the name of the class is "Main". A "class" in Java is the program itself written in Java.
** "Programming in Java language" = "Creating a class" **
You can say that. In the process of programming, some "function" is written in the class. The resulting program uses its functions (s) to do some processing. Most Java programs that actually run on the Internet are a combination of multiple "classes (functions)".
1-2.public
What is "public" at the beginning of the source code?
By adding "public" to a class, that class becomes "accessible from other classes". In other words, assuming that the class has some function,
** "When a program consists of a combination of class A and class B If class A has public, class B can use the functions of class A. **
It means that.
While writing this "If only the Main class exists in this Hello World program, isn't it meaningless to have" public "at this point? I thought, but when I think about when I want to add additional classes (functions) to this program in the future, it makes sense to add public.
2.public static void main(String[] args) {} Next, go inside one
public static void main(String[] args) {
}
I will read this. This is called a ** method ** and represents the content of the functionality of the class.
2-1.main(String[] args)
Here, "main" is the name of the method, and this entire block can be called the "main method".
** Methods are always inside a block of class. ** **
The reason is that the function = method that the class has. Basically, a class has multiple or one method.
Now, let's talk about "(String [] args)" that follows main. here,
** The part that specifies the type and name of the argument received by the main method **
is. ** String [] ** is the argument type and ** args ** is the name. Now that we have two concepts, "argument" and "type", let's take a quick look.
Arguments: Raw materials (data) required to use the functions of the main method. If this is specified, it can be said that the main method has a function to perform some processing on the passed raw material. To put it the other way around, the main method will not work unless you pass the ingredients.
Type: There are several types of arguments passed to the method, and each type is expressed as "~ type". Here, it is specified to pass data of type String [].
Based on the above, if the meaning of "main (String [] args)" is translated into Japanese,
** "The main method is a method of type String [] that works by receiving an argument named args." **
It becomes the sentence.
2-2.public static void What are the "public", "static", and "void" at the beginning?
These are attached to the head of the main method to convey the nature of the method. I will explain the meaning of each one.
public: Methods with this can be used by classes other than the one to which you belong. Depending on the method, there are cases where it is intentionally disabled from other classes.
static: Methods with this are kept uniquely within the class and shared within the class for use. It's a method for a class.
void: The method that did some processing specifies the form of the value (data) that will be returned as a result. "Void" means "returns nothing". For example, if this part is String [] instead of void, the method returns data of type String [] to the method caller as a result of processing.
3.System.out.println("Hello World"); Finally, the contents of the main method
System.out.println("Hello World");
Let's talk about this one line. If you read this one line, ** The part "System.out.println ()" is a description for outputting something, and "something" here is "" Hello World "". ** ** I think you can understand that sensuously.
** System.out.println () ** can be remembered as a regular syntax for outputting character strings etc. in Java, but this time I am writing it for my own output So I will explain as much as possible. If you look at "System.out.println ()", there are some parts separated by ".". In Java, "." Often means "-no" in Japanese. With this in mind, let's look at the meanings of the words separated by "." One by one.
System: A class provided as standard in Java.
out: From among the many functions in the System, the functions related to output are called.
println(): The method that out has. Use by putting data such as character strings in ().
Ends the current line by writing a line delimiter string. (From Java ™ Platform Standard Ed. 8)
It's a really abstract explanation, but if you translate it into Japanese
** In the System class provided as standard in Java, the output function named out calls the method called println () that outputs a character string, and the character string "Hello World" is called. Is output. ** **
It can be said. By the way, the semicolon (;) at the end of the line is defined by Java rules to be added at the end of the instruction.
I tried to explain the Hello World of Java. The straightforward impression is, "Is it so difficult for beginners to explain the first few lines of a program very roughly?" I would be grateful if you could point out any mistakes or strange points. Thank you for reading this far.
-["Introduction to Java for a refreshing second edition (refreshing series)"](https://www.amazon.co.jp/%E3%82%B9%E3%83%83%E3%82%AD%E3% 83% AA% E3% 82% 8F% E3% 81% 8B% E3% 82% 8BJava% E5% 85% A5% E9% 96% 80-% E7% AC% AC2% E7% 89% 88-% E3% 82% B9% E3% 83% 83% E3% 82% AD% E3% 83% AA% E3% 82% B7% E3% 83% AA% E3% 83% BC% E3% 82% BA-% E4% B8 % AD% E5% B1% B1-% E6% B8% 85% E5% 96% AC / dp / 484433638X) / Kiyotaka Nakayama, Daigo Kunimoto
-Understanding System.out.println ("hello, world")-Qiita
--What is a static method / JavaA2Z --KAB-studio
Recommended Posts