It's spring, so I wrote "Hello world" instead of my original intention. As you all know, "Hello world" is a simple introductory code. However, on the other hand, there are some characteristic points that are difficult for beginners who are new to the language to understand.
In this post, I will explain the characteristic parts of "Hello world". Only "Hello world", but "Hello world".
HelloWorld.java
First, take a look at the main code in this post. It's a very ordinary "Hello world".
HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello, world");
}
}
HelloWorld.execution result of class
hello, world
Now let's take a look at the characteristic description of "Hello world" (compared to a typical Java program). Each item is ** at my own discretion ** with a "special degree". The more ★ there are, the more special (a description not found in general programs).
package
declaration (speciality: ★★★)A typical Java program starts with a package
declaration.
However, HelloWorld.java
does not have a package
declaration.
Package declaration example
package com.example.xyz;
Without the package
declaration, the class would belong to a special package called the" default package ".
The "default package" is sometimes referred to as the "anonymous package".
Classes that belong to the "default package" cannot be ʻimport from other classes. This is because it is necessary to specify "class name including package name [^ fqcn]" in the ʻimport
declaration.
Example of import declaration
import com.example.xyz.HelloWorld;
Classes that cannot be ʻimport` from other classes are inconvenient, so use the "default package" only with disposable code like the sample code.
Since it is unlikely that the package
declaration does not exist in a general non-disposable program, the special degree is set to ** ★★★ (3) **.
In Java, all classes are subclasses of the ʻObject class. For a direct subclass of the ʻObject
class, the description of ʻextends Objectcan be omitted. That is, the
HelloWorld class is a direct subclass of the ʻObject
class.
An example of declaring a direct subclass of the Object class
//Exactly the same as the class declaration below
public class HelloWorld {
// ...
}
//Explicitly be a direct subclass of the Object class
public class HelloWorld extends Object {
// ...
}
ʻExends Object` is usually not specified [^ extends]. So, the special degree is ** ☆☆☆ (0) **.
main
method is defined (speciality: ★ ☆☆)The main
method is a special method.
The java
command is designed to call the main
method of the specified class at runtime.
The following is quoted from [Official documentation of the java
command] link-tools_java.
The java command launches a Java application. This is done by launching the Java Runtime Environment, loading the specified class, and calling the main method of that class.
This method must be declared as public and static. Also, do not return a value. In addition, you must be able to specify a Stringarray as a parameter. The format of the method declaration is:
public static void main(String[] args)
As mentioned above, the following are the conditions for the method called from the java
command.
--The method name is main
--The access modifier is public
--static
(static method)
--The return value is void
--The argument is String []
(Stringarray)
If there is no callable main
method in the specified class, the java
command will result in the following error.
Error example when main method does not exist
> java HelloWorld
error:The main method cannot be found in class HelloWorld. Define the main method as follows:
public static void main(String[] args)
It's kind.
It's not a special description, but for large programs most classes don't have a main
method.
Perhaps some Java programmers have never used the main
method [^ main], so the special degree is set to ** ★ ☆☆ (1) **.
It's been long, so I posted it separately. → [Understand System.out.println ("hello, world")] link-sysout
I tried to reflect the explanation so far in the code. I've added a lot of comments, so please read it.
HelloWorld.java(Redundant description)
//By omitting the pacakege declaration, this class belongs to the default package
import java.lang.*;
//* Hereafter, java.Classes in the lang package can be used without qualifying the package name, but are written in FQCN for illustration.
//This class is a direct subclass of the Object class
public class HelloWorld extends java.lang.Object {
//Define a method with a condition called from a java command
public //-The access modifier is public
static //・ Static(Static method)Is
void //-The return value is void
main //-The method name is main
(java.lang.String[] args) //-The argument is a String[](Stringarray)Is
{
//Get the "standard" output stream from the out field of the System class
java.io.PrintStream ps = java.lang.System.out;
//Create a String instance without using a string literal
char data[] = { 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' };
java.lang.String str = new java.lang.String(data);
//Print str to stream with println method
ps.println(str);
}
}
Finally, let's take a look at the ordinary "Hello world" again.
HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello, world");
}
}
In this way, I realized that there are many things I can learn from "Hello world". Only "Hello world", but "Hello world".
The link destination is Java7 because I felt that many people would use it. The content of this post is basically the same for all versions.
[^ fqcn]: Fully qualified class name. It is called "FQCN" for short of "Fully Qualified Class Name".
[^ extends]: I've only seen it in "explanatory code" like this one.
[^ main]: There are many programs that work without writing the main
method, such as when using a server-side program or framework.
Recommended Posts