This post is an article of Schoo's special event "Schoo advent calendar 2016" at the end of 2016. I am an assistant at Schoo, focusing on the master plan business.
This time, I will explain Java's Hello World with reference to the language specifications. It's a story like why "public static void main (String [] args) {...}".
class Test {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
The method main must be declared as public, static, void. You also have to take one argument, which is an array of strings. Source: Java Language Specification 3rd Edition 12.1.4 Starting Test.main P.277 </ cite>
There is no choice but to obey because it says something like that. No programming language can start without a starting point called an entry point. Although it varies depending on the language, the entry point specifications are fixed, and in the case of Java, this is the case.
It's okay to end with this, but considering the reason for this, you can see various Java worlds, so please keep in touch.
First, let's talk about the class declaration part.
class Test {
…
}
It is declared that there is a "class" keyword and it is a class called "Test".
Since Java is based on object orientation, you can imagine that the basic building block is a class. This does not treat the main () method as an exception, so whenever you execute the main () method, it belongs to some class.
The class name can be anything as long as it does not violate the rules. After all, there is no restriction on the class name in the execution of the main () method because it will be specified when the java command is executed. Rather, the class name can be anything, so it is permissible to write the main () method in any class. Some people sometimes misunderstand that they have to have one in the project, but you can write in any class. For example, when writing test code temporarily. (JUnit is better)
Also, some people may think that there is no "public", but it is not a mast.
Let's dig a little deeper into the class declaration here.
A class declaration is a declaration of a new reference type. There are two types of class declarations: normal class declarations and enum declarations. Source: Java Language Specification 3rd Edition 8.1 Class Declaration P.194 </ cite>
The part that "declares a new reference type" is the most important, and if you do not understand that a class is a reference type, you probably cannot understand polymorphism. Also, since it is a "class" or "enum", the enum is actually a special class. Even if enums seem to be a different concept from classes, it must be kept in mind that they have various characteristics of classes.
Next is the method declaration part.
public static void main(String[] args) {
…
}
The specification of the method itself looks like this.
A method declares executable code that can be invoked by passing a certain number of arguments as values. Source: Java Language Specification 3rd Edition 8.4 Method Declaration P.186 </ cite>
The access modifier must be "public".
I can imagine why it has to be "public". Since the JVM executes the main () method of the class specified by the argument, "public" fits nicely considering that it is obviously started from the outside. It seems that the "public" designation is necessary.
There are "public", "(unspecified)", "protected", and "private" access modifiers for methods, and it is desirable to control the information hiding of behavior by selecting these.
static
Must be "static". Given this is like who instantiates the main () method, it turns out that the main () method must be a static method.
Methods declared as static are called class methods. Class methods are always invoked without a reference to a particular object. Source: Java Language Specification 3rd Edition 8.4.3.2 static method P.193 </ cite>
As a method of creating an instance in Java, "new" or "create by reflection" can be considered. However, it is assumed that these are done by the originally instantiated object. Then what happens in the initial state where no instance exists yet?
So static exists in Java. It is static that it can be "always launched without a reference to a specific object" as described in the specification. Therefore, it is necessary to have a static method that can be started as it is in order for some kind of startup to be performed when there is no object that anyone can new.
If you can understand this, you should be able to understand Java's memory model. Java classes are basically blueprints for objects that are instantiated and expanded in memory at the new timing (although there is reflection). However, apart from this, static also exists.
This concept of static is somehow difficult to explain to program beginners. When I try to convey the concept of "new", I say "something is static" ... Also, a person with experience in functional languages runs abusive to make everything static.
The return type must be "void". The main () method in Java does not return anything.
In the return type of the method, declare the value if the method returns a value, and describe void otherwise. Source: Java Language Specification 3rd Edition 8.4.5 Method Return P.196 </ cite>
It doesn't seem to return anything, so it's void. If you really want to return a return code, there is a method like System.exit ().
The method name must be "main". It's a rule, so I have no choice but to obey it.
If you make a mistake like "mein" once in a while, you will not get a compile error and you will not be able to start. People who are used to it are good, but beginners are apt to make mistakes and get hooked, especially if they are done only with notepad.
The normal method name can be anything as long as it does not violate the convention.
It says, "In addition, you must receive one argument that is an array of strings." "String [] args" is required. It must be present, and no other arguments can be accepted. By the way, it is possible to write "String ... args".
This is necessary to get any information at startup. Existence that is nice to assume when specifying the mode of the application.
Based on this, I will explain to beginners. I want the Japanese version of the Java SE 8 version of the language specification to be published.
Recommended Posts