Basic Java syntax

A Java program can be defined as a collection of objects, communicating through calling each other's procedures. Now we will look at the class, object, procedure and instance variable / instance variable to consider their meaning.

A Java program can be defined as a collection of objects, communicating through calling each other's procedures. Now we will look at the class, object, procedure and instance variable to consider their meaning.

- Subjects: Subjects with status and behavior. Example: A flower has states such as: name, color, scent; behavior: blooming, dying. An object is an instance of the class.

- Class: A class can be defined as a detailed template / design, describing the behavior / state that its object supports.

- Procedure (or method): A procedure is basically a behavior. It is where logic is written, data is manipulated and all actions are performed.

- Entity variables: Each object has a unique set of entity variables. The state of the object is created by the values ​​assigned to these variables.

You can meet these concepts in familiar object-oriented programming languages ​​like C ++, Python, PHP. And in the context of Java, I will only talk about them in the Java language. Below, is the difference between the class and the object in Java.

No.
Object
Class
1. The object is an instance of a class. The class is a template or design to create objects. 2. Objects are a real-world entity such as a laptop pen, phone, desk, chair, . Class is a group of similar objects. 3. Object is a physical entity. The class is a logical entity. 4. Objects are created primarily from new keywords. For example: Student s1 = new Student (); Class is declared using the class keyword. Example: Student class {} 5. The object can be created multiple times. The class is declared only once.6 The object is granted memory when it is created. The class must not be allocated memory when it is create.7. There are many ways to create objects in java such as new keyword, newInstance () method, clone () method, factory method and deserialization. There is only one way to define the class in java to use keyword class.

Basic Java syntax

Before starting to dive into Java programming, you must keep in mind the following points:

  1. Distinguish between upper and lower case letters: Java is a case-sensitive language, meaning that you write Quantum, quantrimang or QuanTangMang, QUANTRIMANG will have different meanings.
  2. Class name: For all class names, the first letter should be in capital letters. If you use multiple words to form a class name, the first word in the word should be in capital letters.

Example: class JavaFirst, class QuanTriMang

  1. Procedure name: All procedure names should start with lowercase letters. If multiple words combine multiple words into the procedure's name, after the first word, the first letters of the word should be in capital letters.

Example: public void myQuanTriMang ()

  1. Program file name : The file name must match the class name exactly. When saving the file, you name the file as the class name and the extension ".java", remember that Java is case sensitive, if the file name and the class name do not match, it cannot be compiled.

For example: Assuming JavaFirst is the class name, the file must be JavaFirst.java

  1. public static void main (String args []): The Java program starts with the main () procedure, which is a required part of every Java program.

Identifier in Java

All Java components must be named. The name used for class, variable, and procedure is called the -identifier identifier.

There are several points to note about identifiers:

  1. All identifiers should begin with a letter (A to Z or a to z), a monetary character ($) or an underscore (_).
  2. After the first character, you can use any character to write an identifier.
  3. A keyword cannot be used as an identifier.
  4. Identifiers are also case sensitive.
  5. Examples of correct identifiers: tuoi, $ luong, _giatri_, __1_giatri.
  6. Examples of false identifiers: 123ghi, -tien.

Modifier in Java

Like other programming languages, you can modify classes, methods, . using Modifiers. In Java, there are two types of Modifiers:

  1. Access Modifier: Include default, public, protected, private.
  2. Non-access Modifier: Including final, abstract, strictfp.

We will learn about these Modifiers in the next tutorials.

Variable in Java

Java has the following types of variables:

  1. Local variable
    Variable of class (static variable)
    Object variable (not static variable)

Array in Java

Arrays are objects that store multiple variables with the same data type. However, an array itself is also an object in memory. We will also learn how to declare, build, and initialize this object in future articles.

Java Enum

Enum was introduced in Java 5.0. Enum restricts a variable so that it can only receive one value among several predefined values. Easier to understand, enum is used to define a set of fixed numbers, such as day of week, season of year, .

Using enum can help reduce the number of errors in the code.

For example, we assume an application for fruit juice shops, which can limit the size of small, medium and large cup sizes. This can help ensure that others cannot add sizes other than small, medium and large.

 class FreshJuice { enum FreshJuiceSize { SMALL , MEDIUM , LARGE } FreshJuiceSize size ; } public class FreshJuiceTest { public static void main ( String args []){ FreshJuice juice = new FreshJuice (); juice . size = FreshJuice . FreshJuiceSize . MEDIUM ; System . out . println ( "Size: " + juice . size ); } } 

Output result:

 Size: MEDIUM 

Note: Enum can be declared individually or declared in a class. Procedures, variables, constructors can also be defined in Enum.

Keywords in Java

The list below is the keyword (key) in Java. They cannot be used as constants, variables or any other identifier. So when naming variables, constants, procedures, classes, . you have to avoid these keywords.

abstract assert boolean break byte case class const continue catch default by double else enum extends final finally float for goto if implements import instanceof int giao diện mới không có gói không được bảo vệ public return short static strictfp super chuyển đổi đồng thời không đúng.

Comment in Java

Java supports commenting on one line or multiple lines like C, C ++, Python. All characters in the comment will be ignored by the Java compiler.

For example:

 public class MyFirstJavaCode { /* Đây là chương trình Java đầu tiên. * Nó sẽ in ra màn hình 'Hello World'. * Đây là ví dụ comment trên nhiều dòng.
* By TipsMake.com */ public static void main ( String [] args ) { // Đây là ví dụ comment trên 1 dòng. /* Đây vẫn là ví dụ comment trên một dòng by TipsMake.com. */ System . out . println ( "Hello World" ); } }

Output:

 Hello World 

Use blank lines

A line only contains spaces, possibly comments, called blank lines, and Java completely ignores it.

Inheritance in Java

In Java, classes can be deduced (or derived) from other classes. If you need to create a new class that already has a class containing the code you need, you can deduce the new class from that existing class.

Inheritance allows you to reuse existing class procedures and procedures without rewriting in the new class. In this case, the existing class is called a superclass, the new class is deduced called a subclass.

Interface in Java

In the Java programming language, an interface can be defined as a contract between objects on how to communicate with each other. Interfaces play an essential role when it comes to the concept of inheritance.

An interface defines the procedures, subclasses should use. But the implementation of methods is entirely dependent on subclasses.

Next lesson: Write and run Java code on the computer for the first time

Previous article: Download and install Java on the computer

  1. 200 common Java interview questions, with reference answers
  2. Basic Java exercises, with sample decoding (some)
  3. What is Java? Why choose Java?
Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile