Java beginners read Hello World

Introduction

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

Output result

Hello World

Start reading

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.

Summary

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.

References / Sites

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

Java beginners read Hello World
Read "Hello world"
Java, Hello, world!
Java Hello World
"Hello World" in Java
Java Learning (1)-Hello World
Read System.out.println ("hello, world")
Hello World in Java
java hello world, compile, run
Hello World for ImageJ Java Plugin
Hello world in Java and Gradle
Hello world with Java template engine Thymeleaf
Java development with Codenvy: Hello World! Run
Hello Java Lambda
Minimal Java environment construction and Hello World
Build Java environment and output hello world [Beginner]
Beginners try using android studio Part 1 (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
Wastefully creative "Hello World" output method list (Java)
Read JSON in Java
Read Java Property file
Let's write Hello World
Read Java HashMap source
Hello World on WebAssembly
Hello World with Micronaut
Hello World with Spring Boot
Read binary files in Java 1
[Java] Beginner's understanding of Servlet-②
[Java] One type of alphabet prohibited With binding Hello, world! [Binding]
Java history in this world
Java debug execution [for Java beginners]
[Java] Basic statement for beginners
Hello World with Spring Boot!
Hello World with VS Code!
Read standard input in Java
Object-oriented summary by beginners (Java)
Read binary files in Java 2
Hello World with Spring Boot
Java for beginners, data hiding
[Java] [Android] Read ini file
Try Hello World using plain Java on a Docker container
Hello World with SpringBoot / Gradle
Java application for beginners: stream
Until Android Studio beginners display "Hello World" on HUAWEI nova 2
Hello, World! With Asakusa Framework!
Try Hello World with the minimum configuration of Heroku Java spring-boot
Java 10 (JDK 10) was released on March 20, 2018, so let's try Hello World.
Spring Boot Hello World in Eclipse
Easily read text files in Java (Java 11 & Java 7)
[For beginners] Summary of java constructor
Java, abstract classes starting from beginners
Rock-paper-scissors game for beginners in Java
Read Java properties file in C #
Java for beginners, expressions and operators 1
[For beginners] Run Selenium in Java
Beginner's word No4 (Java command, JVM)
Read CSV in Java (Super CSV Annotation)
Until "Hello World" with Spring Boot
Hello world with Kotlin and JavaFX