Knowledge base
This is already the N
programming language in my self-study journey that I am making knowledge-sharing articles here. Therefore, the scattered articles here will avoid re-explaining the basic concepts that I have learned when learning JavaScript
and C
If you’ve been with us since the first Web Programming Self-Taught Series, you’ll need to skim through the Sub-Series C
of the Some Common Programming Models Series in order to start reading this Series. And in case you came to this article from another source, there is a high chance that you understand the basic programming concepts and Java
more than a lost newbie
who is relearning by herself.
Language & Environment
The Java
language, designed by software engineer James Gosling
and first released in 1995, is currently being developed by Oracle
and is used on two main platforms:
JVM - Java Virtual Machine
–Java
virtual machine is designed to be an intermediary operating environment between programmers and computer operating systems such asWindows
,Linux
,BSD
, etc..ART - Android RunTime
– anAndroid
virtual machine designed to act as an intermediary between a programmer and a specialGoogle
tuned version ofLinux
forAndroid
devices.
These two operating environments provide a somewhat different set of API
programming interfaces, but in terms of supporting features at the Java
language syntax level, ART
fully supports the equivalent of the session JVM
. LTS - Long Term Support
.
The common part between the two sets of programming interfaces on ART
and JVM
is mainly about built-in data structures and module
that support network
communication. The difference is that ART
has additional libraries to support drawing user interfaces and control hardware module
of Android
devices, while JVM
has additional libraries to support rendering of user interfaces for the environment. computer market if not including the version that supports the development of enterprise applications J2EE
:
ART:
Android SDK API
JVM:
JDK 17 LTS API
The articles in this Series will cover the range of features of the Java
language at the Android API
syntax and programming interface level. However, for a start, printing the results of code operations to the computer’s command line window will be much simpler than creating and build
an project Android
, so we will not need to care about ART
.
Tải về JDK:
After downloading and installing JDK
, in the command line window, we can type the command to check the version of the javac
compiler.
1 2 3 | javac --version javac 17.0.6 |
This compiler will help convert the Java
code we write into operating code that can be understood by the JVM
virtual machine. Then, the JVM
virtual machine will have the task of transferring the work logic back to the computer operating system that we are using.
Program Hello, World!
The Java
code that we write will be placed in plain text files of the .java
format. Each of these files will contain only one block of code that defines at the top level of the file overview, which can be code that defines a class
, or an interface
, or an enum
.
1 2 3 4 5 6 7 8 | <span class="token keyword">class</span> <span class="token class-name">HelloWorld</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> main <span class="token punctuation">(</span> <span class="token class-name">String</span> <span class="token punctuation">[</span> <span class="token punctuation">]</span> $args <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span> <span class="token punctuation">.</span> out <span class="token punctuation">.</span> println <span class="token punctuation">(</span> <span class="token string">"Hello, World!"</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
The JVM
virtual machine will be able to start program execution from a static main
method of any class
as shown in the example code above. To compile the code we type the command:
1 2 3 | cd learn-java-language && cd hello-world javac HelloWorld.java |
Then in the project
folder will appear an executable file with the same name as the HelloWorld
source code file but with the format .class
. Now to start running the program using JVM
, we type the command:
1 2 3 | java HelloWorld Hello, World! |
In general, we have the familiar class
definition syntax as in JavaScript
. Here Java
is implemented as a static-typing
language like C
, so should be preceded by each identifier such as field name, method name, parameter name, local variable name, we will have more style keywords.
We have main
being defined as void
, so there’s no need for a terminating return
statement; The args
parameter is the array of character strings passed in after the java HelloWorld.class
command. The public
keyword alone is called the access modifier
visibility adjustment label, which will be covered in another article.