[JAVA] Read "Hello world"

Overview

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

Characteristic part of "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).

There is no 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;

What is the special degree?

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) **.

Class declaration does not have ʻextends` (speciality: ☆☆☆)

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, theHelloWorld 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 {
  // ...
}

What is the special degree?

ʻ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.

What is the special degree?

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) **.

Specific processing of "Hello world"

It's been long, so I posted it separately. → [Understand System.out.println ("hello, world")] link-sysout

HelloWorld.java (redundant description)

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);
  }

}

at the end

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".

reference

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

Read "Hello world"
Java beginners read Hello World
Java, Hello, world!
Java Hello World
"Hello World" in Java
Java Learning (1)-Hello World
Hello world in node.js
Hello World in Java
Studying Java-Part 1-Hello World
Hello World on WebAssembly
Hello World with Micronaut
Hello World with Spring Boot
Hello World with Spring Boot!
Hello World with VS Code!
java hello world, compile, run
Hello World with Spring Boot
Hello World with SpringBoot / Gradle
Hello, World! With Asakusa Framework!
Spring Boot Hello World in Eclipse
Hello World for ImageJ Java Plugin
Until "Hello World" with Spring Boot
Hello World on AWS Lambda + Java
Hello world with Kotlin and JavaFX
Hello World with Docker and C
Hello World in java in eclipse now
(Intellij) Hello World with Spring Boot
Hello World with GlassFish 5.1 + Servlet + JSP
Create PDF with itext7 ~ Hello World ~
hello, world in Vanilla Java-EHW2018 "MVP"
"Hello world" for ImageJ with Eclipse
Hello World with GWT 2.8.2 and Maven
Android OS (7.1.2) build and Hello World
Hello world in Java and Gradle
Creating an Elasticsearch Plugin Series (1) Hello World
Hello World with Eclipse + Spring Boot + Maven
Hello, World! In the bootstrap loader area
Hello world with Java template engine Thymeleaf
[Practice! ] Display Hello World in Spring Boot
Java development with Codenvy: Hello World! Run
"Hello, World!" With Kotlin + CLI in 5 minutes
Hello World on Mac VS Code Java
Introduction to Ratpack (3) --hello world detailed explanation
Hello world with Kotlin and Tornado FX
Hello RSpec
How Spring Security works with Hello World
(IntelliJ + gradle) Hello World with Spring Boot
Minimal Java environment construction and Hello World
Spring Integration Study memorandum ~ Understanding Spring Integration Sample 1. Hello World ~
Output Hello World in kotlin's simple main function
Hello World with JavaFX 11 (OpenJFX) in Liberica JDK 11
Next.js + Rails (API) + Mysql on Docker's Hello World!
Hello world! With Spring Boot (Marven + text editor)
Hello World at explosive speed with Spring Initializr! !! !!
Build Java environment and output hello world [Beginner]
Run JSP Hello World with Tomcat on Docker
Beginners try using android studio Part 1 (Hello World)
Until you install Gradle and output "Hello World"
Returning to the beginning, Java-Kisama mocked Hello World-
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
Display "Hello World" in the browser using Java
Display "Hello World" in the browser using Java