[JAVA] Read System.out.println ("hello, world")

Overview

[Understanding "Hello world"] link-hello has become long, so I decided to write an explanation of the specific processing part here.

[Previous post] I will repost the code of link-hello. In this post, I will explain the lines in this "Hello world" that describe the specific processing.

HelloWorld.java


public class HelloWorld {

  public static void main(String[] args) {
    System.out.println("hello, world");
  }

}

A line that describes specific processing


System.out.println("hello, world");

In addition, ** at my own discretion ** I added "every time I'm in trouble if I don't know" for each item. The more ★ you have, the better you should know.

System points to the java.lang.System class (every time you don't know: ★★ ☆)

In Java, it is implicitly (without writing anything) ʻimport java.lang. *. That is, classes belonging to the java.lang package can be referenced without writing a ʻimport declaration.

So the java.lang.System class can simply be written as System.

The following two lines have exactly the same meaning


System.out.println("Hello world");
java.lang.System.out.println("Hello world");

How often do you have trouble if you don't know?

It is usually not possible to omit java.lang.. However, if you don't know that it belongs to the java.lang package, you will have trouble checking [JavaDoc] link-java_lang, so be aware that it is an abbreviated notation.

If you don't know it, set it as ** ★★ ☆ (2) **.

ʻOut is a staticfield of theSystem` class (every time you don't know: ★ ☆☆)

Let's take a look at [JavaDoc for the System class] link-system. It is explained that the following three static fields are defined.

Field name Description
err A "standard" error output stream.
in A "standard" input stream.
out A "standard" output stream.

As mentioned above, System.out points to the static field of the System class. A "standard" output stream is a "stream" that is connected to a "standard output". (The explanation of "standard output" and "stream" is omitted here.)

In general, it's better to go through a getter (so-called getter) than to make a field public. Also, the above fields have quite different names by Java convention. Perhaps it has such a special design for convenience.

Now, if you look at the description of the ʻout field, you can see that this field is of the [PrintStream`] link-printstream class.

System.Declaration of out


public static final PrintStream out

Let's try assigning System.out to a variable of type PrintStream.

Try assigning to a PrintStream type variable


java.io.PrintStream ps = System.out; // System.Substitute out for ps
ps.println("Hello world"); //Calling the println method of ps

The result is, of course, the same as writing System.out.println ("hello, world "); on one line.

How often do you have trouble if you don't know?

" System.out.println "is an idiom for printing values from Java programs to standard output. So you don't have to worry if you don't know exactly what ʻout` is. However, since it is a basic part, I will leave it as ** ★ ☆☆ (1) ** if you do not know it, with the feeling that you want to know it.

"Hello world" is syntactic sugar for creating String instances (every time you don't know: ★★ ☆)

Let's take a look at [JavaDoc for the String class] link-string. It is explained as follows.

String The class represents a string. All literal strings such as "abc" in Java programs run as instances of this class.

The String class can be said to be the most favored class among many Java classes. In the Java language, [^ literal_o], the only class that can be written literally is the String class. Only primitive types (such as ʻintandboolean) and null can be literally represented except for strings (String`).

By the way, I think that there are many people who asked "What is a literal?" Simply put, a literal is a "value written directly in the source code."

Even if the term literal isn't quite right, the code below [^ string_new] may give you a glimpse of its importance.

Example of creating a String instance that does not use a string literal


char data[] = {'a', 'b', 'c'};
String str = new String(data);

//The above is the same as the code below
String str = "abc";

If you don't have a string literal, you'll need to "write a constructor call with a char array as an argument" to "get a String instance of any value ". Strings are often used, so it would be too much trouble to write them one by one.

"Syntactic sugar" is a grammatical mechanism for simplifying the description of such "troublesome code". In other words, "Create a String instance "has syntactic sugar called a string literal.

How often do you have trouble if you don't know?

If you need a String instance of any value, generate it as a string literal. Rather, you shouldn't instantiate a constructor call when you don't need it. (For example, when generating a string from a byte array, the String constructor may be used.)

But even if you write it in a string literal, you need to know that it's actually new String (...) (that is, it's creating a String instance). Let's do it.

Not limited to String, it is important to know where and how the instance is created, so if you do not know it, set it as ** ★★ ☆ (2) **.

at the end

[Understanding "Hello world"] At the end of link-hello, I wrote a code that reflects the contents of this post in HelloWorld.java, so please have a look there as well.

[^ literal_o]: In languages other than Java, it's not uncommon to be able to write non-string classes literally (regular expression literals, date literals, etc.). [^ string_new]: This code is taken from [JavaDoc for the String class] link-string.

Recommended Posts

Read System.out.println ("hello, world")
Read "Hello world"
Java beginners read Hello World
Java, Hello, world!
Java Hello World
"Hello World" in Java
Java Learning (1)-Hello World
Let's write 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
How Spring Security works with Hello World
(IntelliJ + gradle) Hello World with Spring Boot
Minimal Java environment construction and Hello World