Java basic syntax

A java program can be considered as a collection of objects that work together by calling each other's methods. The following briefly introduces the concepts of classes, objects, methods, and instance variables.

First Java program

Let's look at a simple java program that will print the string Hello world

How to save, compile and run this program will be described step by step below:

You will see Hello world in the window

Basic grammar

When writing Java programs, you should pay attention to the following points:

Java identifier

All components of Java need names. Class names, variable names, and method names are all called identifiers.

There are several points to note about Java identifiers:

Java modifier

Like other languages, Java can use modifiers to modify methods and properties in classes. There are two main types of modifiers:

We'll delve into Java modifiers in later chapters.

Java variables

There are mainly the following types of variables in Java

Java array

An array is an object stored on the heap and can hold multiple variables of the same type. In later chapters, we will learn how to declare, construct, and initialize an array.

Java enumeration

Java 5.0 introduces enumeration, which limits that variables can only be preset values. Using enumeration can reduce bugs in your code.

For example, we design a program for juice stores, which will limit juice to small, medium and large cups. This means that it does not allow customers to order juice other than these three sizes.

example

Note: enumerations can be declared separately or in classes. Methods, variables, and constructors can also be defined in enumerations.

Java keyword

Java keywords are listed below. These reserved words cannot be used for the names of constants, variables, and any identifiers.

Java annotation

Similar to C / C + +, Java also supports single line and multi line comments. Characters in comments are ignored by the java compiler.

Java blank line

Blank lines, or lines with comments, are ignored by the java compiler.

inherit

In Java, a class can be derived from other classes. If you want to create a class and there is already a class with the properties or methods you need, you can inherit the newly created class from the class.

Using inherited methods, you can reuse the methods and properties of existing classes without rewriting these codes. The inherited class is called super class, and the derived class is called subclass.

Interface

In Java, an interface can be understood as a protocol for mutual communication between objects. Interfaces play an important role in inheritance.

Interface only defines the methods to be used for derivation, but the specific implementation of the method depends entirely on the derived class.

The difference between Java source program and compiled running program

As shown in the figure below:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>