In recent years, there have been many programming schools that advertise "become an engineer even if you are inexperienced" or "become an engineer even if you are from a liberal arts background", and I was taken care of by one of them during the training of the company. However, I felt that the focus was not so much on the "basics", probably because the purpose was to create an application in a short period of time, so I became a ** newcomer who will learn programming from now on **. I wanted to write something that would serve as a gateway to the "basics" of Java, and for ** new engineers like me who feel that the "basics" aren't enough.
So far, the word "basic" has all been bracketed.
Meaning of Ki-so [Basic] The basic part that makes something happen. Originally. "Because the foundation is solid, you can improve quickly" "Consolidate the foundation" "Basic knowledge"
In this entry, as a first step towards learning the "basics" of Java
I would like to focus on these two.
First, let's look at the language itself, Java.
Java Before that, let's first briefly check the programming language itself.
Programmers use programming languages to send instructions to computers. However, a computer is just an electrically powered calculator.
(As an aside, the computer is called Rechner in German, but it comes from the verb rechnen, which is "calculate." It's really an aside.)
Computers can only receive two electrical signals, on or off, so old programmers used to write programs using binary numbers 0 and 1. By the way, the one composed of 0 and 1 is called ** machine language **.
It seems that binary numbers are often written in hexadecimal numbers because they have many digits, but they are still not human-friendly. Therefore, "** Assembly Language **" was created by adding a nickname (called "mnemonic") to the instructions represented by the machine language.
However, programs are still not easy for humans to understand, and as the times progressed, the number and complexity of programs required increased. Therefore, a method of writing programs that is easier for humans to understand was created. (By the way, programming languages that are easy for humans to understand are called "high-level languages", while languages that are easy for computers to understand are called "low-level languages".)
What is easy for humans to understand also means that it is difficult for computers to understand. So you have to "convert" the code written by the programmer into machine language so that the computer can understand it. That is the work called "** compilation ", and the one that compiles it is called " compiler **". The compiler translates programs written in high-level languages into something that a computer can understand, but it compiles for that computer. That is, the computer in another environment cannot understand the translated machine language. So you will need another compiler for it again.
Java is finally here.
Java was announced by Sun Microsystems in 1995. Java, which is a high-level language, needs to be compiled into machine language as I wrote earlier, but the difference from other languages is that it uses a virtual machine called ** JVM ** (Java Virtual Maschine). There is.
The JVM allocates memory on your computer and boots. And since you can run the program on that JVM, you don't have to worry about the differences between computers.
To be more specific, Java is written in a human-friendly way (filename is hoge.java) and is translated into what is called an intermediate language through compilation (filename is hoge.class. The name intermediate language is official. It seems that it is called so for convenience, and it is correctly called ** Java byte code ). Then, the JVM loads the intermediate language, converts it line by line into machine language, and executes it. A program that converts and executes line by line in this way is called " interpreter **" [^ 1].
[^ 1]: While the compile and compiler are separated according to the English grammar, the interpreter should be divided into interpret and interpreter according to the English grammar, but I wonder if the notation is not divided. ??
In other words, there are two conversion steps before Java is executed. (Conversion by compiler and interpreter)
JVM There were JDKs and JREs when building the Java environment. The JDK is the Java Development Kit, a software development kit required when developing using Java. And what is included in that JDK is the JRE (Java Runtime Environment), which is called the Java execution environment. This is a set of software that allows you to run Java applications on your computer, including the JVM here.
The JVM consists of threads and memory. A process is defined in the thread and executes that process. Memory is divided into heap and native. The heap area stores application objects, and garbage collection (GC) regularly organizes objects that are no longer needed. The native area is the area used by the JVM itself and stores the information being processed by the thread (thread stack). I'd like to explain in a little more detail, but I'm going to run out, so I'll move on.
Let's take a quick look at the process leading up to the final display of Hello World !.
HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Let's compile first. The ** javac ** command compiles a java file and creates a class file.
javac HelloWorld.java
This will create a file called HelloWorld.class. Let's display Hello World! First. Start the JVM with the ** java ** command, parse the HelloWorld.class file written in Java bytecode, and have it executed.
$ java HelloWorld
Hello World!
It's done. Now let's check the Java bytecode. You can use the ** javap ** command to convert a class file into a human-readable format.
$ javap -c HelloWorld
Compiled from "HelloWorld.java"
public class HelloWorld {
public HelloWorld();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3 // String Hello World!
5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
}
public class Starting with HelloWorld, this class has two functions, the first is the constructor and the second is the bytecode of the main function. The original code didn't have a constructor, but it seems to be generated automatically. Let's look inside the function. Code consists of three parts: "index: instruction #constant pool number". The constant pool is the area where constants and symbols are stored in the Java virtual machine, but let's take a look. You can check by specifying the -v option in the javap command.
Constant pool:
#1 = Methodref #6.#15 // java/lang/Object."<init>":()V
#2 = Fieldref #16.#17 // java/lang/System.out:Ljava/io/PrintStream;
#3 = String #18 // Hello World!
#4 = Methodref #19.#20 // java/io/PrintStream.println:(Ljava/lang/String;)V
#5 = Class #21 // HelloWorld
#6 = Class #22 // java/lang/Object
#7 = Utf8 <init>
#8 = Utf8 ()V
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 main
#12 = Utf8 ([Ljava/lang/String;)V
#13 = Utf8 SourceFile
#14 = Utf8 HelloWorld.java
#15 = NameAndType #7:#8 // "<init>":()V
#16 = Class #23 // java/lang/System
#17 = NameAndType #24:#25 // out:Ljava/io/PrintStream;
#18 = Utf8 Hello World!
#19 = Class #26 // java/io/PrintStream
#20 = NameAndType #27:#28 // println:(Ljava/lang/String;)V
#21 = Utf8 HelloWorld
#22 = Utf8 java/lang/Object
#23 = Utf8 java/lang/System
#24 = Utf8 out
#25 = Utf8 Ljava/io/PrintStream;
#26 = Utf8 java/io/PrintStream
#27 = Utf8 println
#28 = Utf8 (Ljava/lang/String;)V
Let's check the main function and see the flow of Hello World! Display.
public static void main(java.lang.String[]);
Code:
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3 // String Hello World!
5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
First of all, the getstatic instruction refers to # 2. this is
#2 = Fieldref #16.#17 // java/lang/System.out:Ljava/io/PrintStream;
You can see that we are getting the field to use, java / lang / System.out.
The next ldc instruction, which loads a constant value from the constant pool at run time.
#3 = String #18 // Hello World!
#18 = Utf8 Hello World!
The string Hello World! Was loaded here.
And the invoke virtual instruction,
#4 = Methodref #19.#20 // java/io/PrintStream.println:(Ljava/lang/String;)V
Execute the println function with the previous string as an argument.
Finally, return void to finish. Thank you for your hard work, Hello World! Is now displayed.
I tried to make it something I wanted to know when I was studying Java for the first time, but it is difficult to write it in a concise manner and with the main points firmly in mind. Do I really want to read this content a little less than a year ago? However, I hope this article has helped someone. If you want to learn the basics of Java with this as an opportunity, it is the best & highest. And I decided to study the basics of Java so that I could send it out in this way.
If you make a mistake, please point it out.
Recommended Posts