The best Java basic grammar Tutorial In 2024, In this tutorial you can learn The first Java program,Examples,The basic syntax,Java identifier,Java modifiers,Java variables,Java arrays,Java enum,Examples,Java keywords,Java annotations,Java blank line,inherit,interface,
A Java program can be considered as a collection of objects, and these objects through method calls to each other to work together. Introduced under the concept of classes, objects, methods and instance variables below.
Let's look at a simple Java program, it will print the string Hello World
The following will be gradually describes how to save, compile and run this program:
You will see the Hello World in the window
C : > javac HelloWorld.java C : > java HelloWorld Hello World
Gif presentation:
When writing a Java program, you should note the following:
All components of Java are required names. Class names, variable names and method names are called identifiers.
About Java identifiers, the following points should be noted:
Like other languages, Java can be modified using the modifier class methods and properties. There are two types of modifiers:
In the following sections we will discuss in-depth Java modifiers.
Arrays are stored on the heap object, you can save multiple variables of the same type. In later chapters, we will learn how to declare and initialize an array structure.
Java 5.0 introduced the enumeration, enumeration constraint variables can only be pre-set value. Using enumerations reduces code bug.
For example, we designed a program for the fruit juice shop, it will limit the juice of a small cup, medium cup, large cup. This means that it does not allow the customer in addition to the three point Dimensions juice.
class FreshJuice { enum FreshJuiceSize{ SMALL, MEDUIM, LARGE } FreshJuiceSize size; } public class FreshJuiceTest { public static void main(String []args){ FreshJuice juice = new FreshJuice(); juice.size = FreshJuice. FreshJuiceSize.MEDUIM ; } }
Note: enum declaration may be used alone or inside the class declaration. Methods, variables, constructors can also be defined in the enumeration.
The following lists the Java reserved words. These reserved words can not be used to name constants, variables, and any identifiers.
Keyword | description |
---|---|
abstract | Abstract methods, abstract classes modifiers |
assert | Assertion conditions are met |
boolean | Boolean data type |
break | Out of the loop or label snippet |
byte | 8-bit signed data types |
case | A conditional switch statement |
catch | With the exception of information capture and try |
char | 16-bit Unicode character data type |
class | Class definitions |
const | Unused |
continue | The remaining portion of the loop is not executed |
default | switch statement default branch |
do | Loop, the loop body is executed at least once |
double | 64-bit double-precision floating-point number |
else | When the branch condition is not satisfied if executed |
enum | Enumerated type |
extends | It represents a class is a subclass of another class |
final | It represents a value after initialization can not change the representation can not be rewritten, or a class can not have subclasses |
finally | , Mainly to the completion of code execution and design for robustness and integrity of the program, whether there are code execution exception occurs. |
float | 32-bit single-precision floating-point number |
for | for loop |
goto | Unused |
if | Conditional statements |
implements | It represents a class implements an interface |
import | Importing classes |
instanceof | Test whether an object is an instance of a class |
int | 32-bit integer |
interface | Define an interface, a type of abstract methods and constants only |
long | 64-bit integer |
native | Representation of non-java code |
new | Assign a new instance of the class |
package | A package consisting of a series of related classes |
private | Etc. represents private field or method can be accessed only from within the class |
protected | It indicates that the field can only access the class or subclass or subclass other classes in the same package |
public | Represents total property or method |
return | Method returns a value |
short | 16 digits |
static | Represented at the class level definition, shared by all instances |
strictfp | Floating-point comparison using strict rules |
super | It represents the base class |
switch | Select statement |
synchronized | Represent the same time can only be accessed by one thread block of code |
this | It represents the current instance of the call or call another constructor |
throw | Throw an exception |
throws | Definition method may throw an exception |
transient | Do not modify the sequence of the field |
try | It represents a code block to do and finally with exception handling or throw an exception indicating whether the code is executed finally |
void | Marking method does not return any value |
volatile | Flag field may be multiple threads access, do not sync |
while | while loop |
Similar to C / C ++, Java supports single-line and multi-line comments. Comment characters are ignored Java compiler.
public class HelloWorld { /* 这是第一个Java程序 *它将打印Hello World * 这是一个多行注释的示例 */ public static void main(String []args){ // 这是单行注释的示例 /* 这个也是单行注释的示例 */ System.out.println("Hello World"); } }
Blank lines, or annotated line, Java compiler will be ignored.
In Java, a class can be derived from the other classes. If you want to create a class, and already has a class property or method you need, then you can be a newly created class inherited class.
Use inherited methods, you can reuse the existing class methods and properties, without rewriting the code. Inherited class is called a superclass (super class), the derived class is called a subclass (subclass).
In Java, understood as the interface protocol between objects communicate with each other. The interface plays an important role in the succession.
Interface defines a method to use is derived, but the specific implementation of the method depends entirely on the derived class.
The next section describes the Java programming classes and objects. After you will have a clearer understanding of Java classes and objects.