[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");
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) **.
is a
staticfield of the
System` 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.
" 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.
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 ʻintand
boolean) 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.
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) **.
[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