A. Introduction
1. Java
Java is a high-level programming language and a platform for running programs written in Java.
Java can create many different types of applications:
- Standalone application: a standalone application
- Web application: run on server, create dynamic web
- Enterprise application: business application, bank, … security, high load
- Mobile application
There are about 3 billion devices running Java platforms, proving that Java is very popular today.
2. History
James Gosling and his colleagues created the Java language, originally called Greentalk, then changed to Oak (oak tree). At first Java was only used for electronic and embedded devices. In 1995, the name was changed to Java.
Java undergoes various versions, the latest being SE 14.
Java is currently owned by Sun Microsystem (under Orable).
3. Editions
There are 4 main versions, for different purposes:
- Java Card: for smartcard like ATM, SIM, …
- Java Micro Edition (ME): compact version for mobile, embedded
- Java Standard Edition (SE): the core, standard version of Java
- Java Enterprise Edition (EE): enterprise version, large system
4. Features
Some of the most important and remarkable features and features of Java are as follows.
Platform independent
Platform independence is the most important feature of Java. Java compiles source code into platform independent bytecode, which can run any hardware, operating system that supports Java platform.
Write once, run anywhere – Java criteria
Simple
Java is a simple language with easy-to-learn syntax similar to C ++, but it removes some unnecessary and cumbersome features (pointer, operator overloading, …)
In addition, Java also has GC that helps clean up and manage memory without manual management.
Object oriented
Java is pure object oriented language (OOP).
Multi threaded
Java programs can have many threads, so the speed and performance will be better.
Secure
Java is quite safe, minimizing potential security problems. Java ditches pointer usage, strict casting, and strict code verification.
In addition, because the Java program runs in the JVM, the error is easy to control, does not affect the system.
Robust
Java is powerful in its automatic memory management, and has strict exception handling and error checking mechanisms.
Portable
Bytecode of java can be carried on many different platforms and still works properly, so Java is portable.
B. Java concepts
1. Components
Java platform
A platform is a type of hardware or software that can function as a program.
Thus, java platform is the platform that allows running java programs on it. The Java platform includes the compiler, libraries and many other tools to aid in running the program.
Java Virtual Machine
JWM is an executable environment preparation program for Java applications. It can be seen as a (abstract) virtual machine for the program to run inside it.
JWM has 4 functions:
- Upload bytecode from
.class
file to memory (class loader) - Verify valid bytecode (verifier)
- Translates the bytecode into an interpreter and executes it
- Provide an execution environment
Java Runtime Environment
JRE includes JVM, run libraries and programs that support running java applications. The JRE can be compared with the .NET framework.
JRE is for users who want to run apps written in java, if not, they can’t run.
Java Development Kit
JDK includes JRE and other tools to support java programming (compiler, interpreter, docs, …). Unlike JRE for users, JDK is for programmers, to write java programs.
Garbage Collection
GC is a component of the JVM, which controls and collects unused memory (no longer referenced) to return to the OS, saving memory,
2. IDE
There are many IDEs that support java programming, but I encourage using Eclipse or IntelliJ IDEA to code. NetBean is quite good but I don’t like its interface, at least Eclipse is nicer.
3. Build commands
Although every IDE supports building and running java programs, you should also know how to compile it manually using the command line.
1 2 | javac Test.java |
Compile the Test.java
file into bytecode Test.class
.
1 2 | java Test |
Execute the bytecode in the Test.class
file and run the program. Note there is no need to add the .class
extension to the filename.
The javac
program is in the JDK (for programmers) and java
is in the JRE and also in the JDK (because the JDK contains the JRE).
4. Build tools
In large java project, it is possible to use many libraries, many other frameworks to support. There are java libraries and also external libraries. Therefore, it is necessary to have tools to help build and manage those libraries (called dependencies), so build tools are also called dependencies manager.
Two popular tools, Maven and Gradle, can learn more.
5. Naming convention
In order for java code to be clear and consistent, it is advisable to follow some of java naming convention. This is optional, but recommended for cleaner code.
Different objects in code have their own names:
- Package name: Lower case or snake case
java.util
- Class name: Pascal case and the noun
MyClass
- Interface name: Pascal case and the adjective
Flyable
- Object name: Camel case
obj
,otherObj
- Variable name: Camel case
myName
- Constant name: Upper case
PI_NUMBER
- The method name, Camel case, the first word is the verb
getName
,toString
Java is case sensitive.
C. Java basic
1. Simple program
The general structure of a java program. When going deeper, each class contained in a separate file also has this structure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <span class="token comment">// Chương trình thuộc package nào (chỉ thuộc 1 package)</span> <span class="token keyword">package</span> package_name <span class="token punctuation">;</span> <span class="token comment">// Import class vào để sử dụng (có thể import nhiều class)</span> <span class="token keyword">import</span> <span class="token namespace">java <span class="token punctuation">.</span> util</span> <span class="token punctuation">.</span> <span class="token class-name">Scanner</span> <span class="token punctuation">;</span> <span class="token comment">// Import một class</span> <span class="token keyword">import</span> <span class="token namespace">java <span class="token punctuation">.</span> util</span> <span class="token punctuation">.</span> * <span class="token punctuation">;</span> <span class="token comment">// Import toàn bộ package</span> <span class="token comment">// Định nghĩa thêm các class khác</span> <span class="token keyword">class</span> <span class="token class-name">Util</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token punctuation">}</span> <span class="token comment">// Class chính, là public class duy nhất trong file</span> <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">App</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token comment">// Class chính phải có method main() như sau</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span> <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 punctuation">.</span> <span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
Source code is stored in the App.java
file, each file must have a unique public class
, the other classes are secondary and only declared class
.
The public class
name needs to match the file name, as in the example above the file is App.java
, the main class name must be App
. In the main class, there must be a main()
method which is public static void
. This is where the entire program starts.
2. Comment
Comments are ignored when compiling. Java has 2 types of comments:
- Single line comment: From the
//
sign to the end of the line - Multi line comment (block comment): in pair
/* */
Comment //
used for commenting, commenting for code, while /* */
often used to create documents.
And there is another kind of comment quite similar to /* */
is documentation comment, used to write documents with javadoc tool. Documentation comment is of the form /** */
.
3. Import
Use the import
keyword to import one or more classes into the program. Classes can belong to many different packages.
1 2 3 | <span class="token keyword">import</span> <span class="token namespace">java <span class="token punctuation">.</span> util</span> <span class="token punctuation">.</span> * <span class="token punctuation">;</span> <span class="token comment">// Import toàn bộ package</span> <span class="token keyword">import</span> <span class="token namespace">java <span class="token punctuation">.</span> util</span> <span class="token punctuation">.</span> <span class="token class-name">Scanner</span> <span class="token punctuation">;</span> <span class="token comment">// Import một class Scanner vào</span> |
If not imported as above, then when using the program, specify package.Class
, for example as follows.
1 2 3 4 5 6 7 | <span class="token comment">// Không dùng import (dài dòng)</span> java <span class="token punctuation">.</span> util <span class="token punctuation">.</span> <span class="token class-name">Scanner</span> sc <span class="token operator">=</span> <span class="token keyword">new</span> java <span class="token punctuation">.</span> util <span class="token punctuation">.</span> <span class="token class-name">Scanner</span> <span class="token punctuation">(</span> <span class="token class-name">System</span> <span class="token punctuation">.</span> in <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment">// Dùng import (ngắn hơn)</span> <span class="token keyword">import</span> <span class="token namespace">java <span class="token punctuation">.</span> util</span> <span class="token punctuation">.</span> <span class="token class-name">Scanner</span> <span class="token punctuation">;</span> <span class="token class-name">Scanner</span> sc <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Scanner</span> <span class="token punctuation">(</span> <span class="token class-name">System</span> <span class="token punctuation">.</span> in <span class="token punctuation">)</span> <span class="token punctuation">;</span> |
The import
statement is usually placed outside and at the top and bottom of the package
declaration.
4. Variable & constant
Variable
Variables in Java have 3 types, each with their own location:
- Instance variable: in class, not of any method. This variable belongs to the entities (instance – object) created from the class.
- Static variables: Similar to entity variables, but with the
static
keyword to mark variables that belong to classes, not objects. - Local variable: is declared in the method.
The declaration syntax is similar to C ++. The init value can be omitted, and multiple variables of the same type can be declared on one line.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <span class="token comment">// Biến không khai báo ở đây</span> <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">App</span> <span class="token punctuation">{</span> <span class="token comment">// Biến thực thể</span> <span class="token keyword">int</span> instanceVar <span class="token operator">=</span> <span class="token number">10</span> <span class="token punctuation">;</span> <span class="token comment">// Biến tĩnh</span> <span class="token keyword">static</span> <span class="token keyword">double</span> staticVar <span class="token punctuation">;</span> <span class="token comment">// Không cần init value</span> <span class="token comment">// Đây là method</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// Biến cục bộ</span> <span class="token keyword">int</span> localVar <span class="token operator">=</span> <span class="token string">"Hello"</span> <span class="token punctuation">,</span> name <span class="token operator">=</span> <span class="token string">"John"</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
Constant
Constants are similar to declaring variables. Previously used the keyword const
, but was replaced with final
.
1 2 | <span class="token keyword">final</span> <span class="token keyword">float</span> PI <span class="token operator">=</span> <span class="token number">3.14</span> <span class="token punctuation">;</span> |
A constant cannot assign a new value, and an init value is required when declaring. For an object constant, the object’s fields are still mutable.
5. Input & output
Output
Use the print commands of System.out
to output the output to the console screen (of cmd or Eclipse console).
1 2 3 4 5 | <span class="token class-name">System</span> <span class="token punctuation">.</span> out <span class="token punctuation">.</span> <span class="token function">print</span> <span class="token punctuation">(</span> <span class="token string">"String "</span> <span class="token operator">+</span> a <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment">// In không xuống dòng</span> <span class="token class-name">System</span> <span class="token punctuation">.</span> out <span class="token punctuation">.</span> <span class="token function">println</span> <span class="token punctuation">(</span> <span class="token string">"String "</span> <span class="token operator">+</span> a <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment">// In và xuống dòng</span> <span class="token class-name">System</span> <span class="token punctuation">.</span> out <span class="token punctuation">.</span> <span class="token function">printf</span> <span class="token punctuation">(</span> <span class="token string">"String %d %d"</span> <span class="token punctuation">,</span> a <span class="token punctuation">,</span> b <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment">// In với chuỗi format và pass value vào</span> |
The above commands take string parameters, for printf()
, the following parameters are the values passed. Therefore, the following is incorrect.
1 2 3 | <span class="token class-name">System</span> <span class="token punctuation">.</span> out <span class="token punctuation">.</span> <span class="token function">println</span> <span class="token punctuation">(</span> a <span class="token punctuation">,</span> b <span class="token punctuation">,</span> c <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment">// Sai</span> <span class="token class-name">System</span> <span class="token punctuation">.</span> out <span class="token punctuation">.</span> <span class="token function">println</span> <span class="token punctuation">(</span> a <span class="token operator">+</span> <span class="token string">" "</span> <span class="token operator">+</span> b <span class="token operator">+</span> <span class="token string">" "</span> <span class="token operator">+</span> c <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment">// Đúng</span> |
Must use string concatenation +
, java will automatically convert the variables into strings and the concatenation is valid.
In addition to the output stream, System.out
has the same System.err
, but only uses the error output to the screen. Console in eclipse will display the text output System.err
in red.
Input
To import data from the console, use the java.util.Scanner
class as follows.
1 2 3 4 | <span class="token keyword">import</span> <span class="token namespace">java <span class="token punctuation">.</span> util</span> <span class="token punctuation">.</span> <span class="token class-name">Scanner</span> <span class="token punctuation">;</span> <span class="token class-name">Scanner</span> sc <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Scanner</span> <span class="token punctuation">(</span> <span class="token class-name">System</span> <span class="token punctuation">.</span> in <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment">// Tạo object scanner, và đọc dữ liệu từ System.in stream</span> |
To read data, use the next_()
methods that match the type of data to be read, for example.
1 2 3 4 | <span class="token keyword">int</span> a <span class="token operator">=</span> sc <span class="token punctuation">.</span> <span class="token function">nextInt</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment">// Đọc số nguyên</span> <span class="token keyword">double</span> b <span class="token operator">=</span> sc <span class="token punctuation">.</span> <span class="token function">nextDouble</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment">// Đọc số thực</span> <span class="token class-name">String</span> s <span class="token operator">=</span> sc <span class="token punctuation">.</span> <span class="token function">nextLine</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment">// Đọc chuỗi thì đọc cả dòng</span> |
After using the nextInt()
, nextFloat()
, … commands in general to read numbers, use the next nextLine()
command to delete the remaining n
characters left in the buffer after reading the numbers.
The number read methods only take the number, leaving the character n
, causing the command to read the string behind to be drifted (the result of the string is empty, it runs without stopping to enter). Therefore, use nextLine()
after reading the number to remove the remaining n
character, to avoid the error of drifting as above.
1 2 3 4 5 6 7 8 | <span class="token keyword">int</span> a <span class="token operator">=</span> sc <span class="token punctuation">.</span> <span class="token function">nextInt</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> sc <span class="token punctuation">.</span> <span class="token function">nextLine</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">float</span> b <span class="token operator">=</span> sc <span class="token punctuation">.</span> <span class="token function">nextFloat</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> sc <span class="token punctuation">.</span> <span class="token function">nextLine</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment">// Lệnh này không bị trôi đi</span> <span class="token class-name">String</span> s <span class="token operator">=</span> sc <span class="token punctuation">.</span> <span class="token function">nextLine</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> |
Finally, two notes when using the Scanner
class to avoid errors:
- A single scanner should be used for the entire program. Multiple scanners with access to the
System.in
stream will cause conflicts (even if one scanner is closed). - When finished using the scanner, call the
close()
method to close the input stream.