Java basics – part 1

Tram Ho

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.

Compile the Test.java file into bytecode Test.class .

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.

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.

If not imported as above, then when using the program, specify package.Class , for example as follows.

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.

Constant

Constants are similar to declaring variables. Previously used the keyword const , but was replaced with final .

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).

The above commands take string parameters, for printf() , the following parameters are the values ​​passed. Therefore, the following is incorrect.

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.

To read data, use the next_() methods that match the type of data to be read, for example.

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.

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.
Share the news now

Source : Viblo