Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Java's underlying syntax


May 10, 2021 Java


Table of contents


Java's underlying syntax

A Java program can be thought of as a collection of objects that work together by calling each other's methods. The following is a brief introduction to the concepts of classes, objects, methods, and instance variables.

  • Object: An object is an instance of a class that has states and behaviors. For example, a dog is an object whose status is: color, name, breed, behavior: wagging its tail, barking, eating, etc.
  • Class: A class is a template that describes the behavior and state of a class of objects.
  • Method: Methods are behaviors, and a class can have many methods. Logical operations, data modifications, and all actions are done in the method.
  • Instance variables: Each object has a unique instance variable, and the state of the object is determined by the values of those instance variables.

The first Java program

Here's a look at a simple Java program that will print the string Hello World

public class MyFirstJavaProgram {
   /* 第一个Java程序.  
    * 它将打印字符串 Hello World
    */
    public static void main(String []args) {
       System.out.println("Hello World"); // 打印 Hello World
    }
} 

Here's a step-by-step look at how to save, compile, and run the program:

  • Open notepad and add the code above;
  • Save the file name as: MyFirstJavaProgram .java;
  • Open the cmd command window and go to the location of the target file, assuming that it is C:
  • Type javac MyFirstJavaProgram in the command-.java press enter to compile the code. I f the code is error-correct, the cmd command prompt goes to the next line. (Suppose the environment variables are all set).
  • Then type java MyFirst JavaProgram and press Enter to run the program

You'll see Hello World in the window

C : > javac MyFirstJavaProgram.java
C : > java MyFirstJavaProgram 
Hello World

Basic syntax

When writing Java programs, you should be aware of the following:

  • Case sensitive: Java is case sensitive, which means that the identifier Hello is different from Hello.
  • Class Name: For all classes, the first letter of the class name should be capital. If the class name consists of several words, the initials of each word should be capital, such as MyFirstJavaClass.
  • Method name: All method names should begin with lowercase letters. If the method name contains several words, the initials of each subsequent word are capitaled.
  • Source file name: The source file name must be the same as the class name. W hen you save a file, you should use the class name as the file name (remember java is case sensitive) and the file name has a suffix .java. (If the file name and class name are not the same, a compilation error can result).)
  • Main method entry: All Java programs are [] executed by the public static void main (String ( ) args method.

Java identifier

All components of Java require a name. Class names, variable names, and method names are all referred to as identifiers.

There are a few points to note about Java identifiers:

  • All identifiers should start with the letter (A-Z or a-z), the dollar sign ($), or the underscore
  • The first character can be followed by a combination of any character
  • Keywords cannot be used as identifiers
  • The identifier is case sensitive
  • Examples of legal identifiers: age, $salary, _value, __1_value
  • Examples of illegal identifiers: 123abc, -salary

Java modifier

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

  • Access control modifiers : default, public, protected, private
  • Non-access control modifiers : final, abstract, static, synchronized, and volatile

We'll dive into Java modifiers in a later section.

Java variable

There are several main types of variables in Java

  • The local variable
  • Class variables (static variables)
  • Member variables (non-static variables)

Java array

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


Java enumerity

Java 5.0 introduces enumeration, which limits variables to pre-set values. Use enumeration to reduce bugs in your code.

For example, we design a program for juice shops that 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.


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 ;
   }
}

Note: Enumerity can be declared separately or inside a class. Methods, variables, and constructors can also be defined in enumerations.


Java keywords

The Java reserved word is listed below. T hese reserved words cannot be used for the names of constants, variables, and any identifiers.

Keywords Describe
abstract Abstract methods, modifiers of abstract classes
Assert Whether the assertion condition is met
boolean Boolean data type
break Jump out of a loop or label snipp
byte 8-bit signed data type
case A condition of the switch statement
Catch Match with try to catch abnormal information
char 16-bit Unicode character data type
class Define the class
const Not used
continue The rest of the loop body is not executed
default The default branch in the switch statement
do Loop statement, the loop body executes at least once
double 64-bit double floating point
else The branch that is executed when the if condition is not established
Enum The enumerity type
extends Indicates that one class is a sub-class of another class
final Indicates that a value cannot be changed after initialization
The indicates that the method cannot be overrided, or that a class cannot have sub-classes
finally The code that is executed is designed primarily for the robustness and integrity of the program, with or without exceptions.
float 32-bit single-precision floating point
for For loop statement
Goto Not used
if Conditional statement
implements Indicates that a class implements an interface
import Import the class
instanceof Test whether an object is an instance of a class
Int 32-bit integer number
interface Interface, an abstract type, defined only by methods and constants
long 64-bit integer number
native The means method is implemented with non-java code
new Assign a new class instance
package A series of related classes form a package
private Represents a private field, or method, etc., that can only be accessed from inside the class
protected Indicates that a field can only be accessed through a class or its sub-classes
Sub-classes or other classes within the same package
public Represents a common property or method
return Method returns a value
short 16 digits
static Represents what is defined at the class level and shared by all instances
strictfp Floating points use strict rules
super Represents the base class
switch Select the statement
synchronized Represents a block of code that can only be accessed by one thread at a time
this Represents the call to the current instance
Or call another constructor
throw Throw an exception
throws Define the exception that the method might throw
transient Decorate fields that do not serialize
try Indicates that the block of code does exception handling or cooperates with the finally to indicate that the code in the finally is executed whether or not the exception is thrown
void The tag method does not return any values
volatile Tag fields may be accessed by multiple threads at the same time without synchronization
while While loop

Java comments

Similar to C/C, Java also supports single-line and multi-line annotations. The characters in the comment are ignored by the Java compiler.

public class MyFirstJavaProgram{
   /* 这是第一个Java程序
    *它将打印Hello World
    * 这是一个多行注释的示例
    */
    public static void main(String []args){
       // 这是单行注释的示例
       /* 这个也是单行注释的示例 */
       System.out.println("Hello World"); 
    }
} 

Java empty line

Blank lines, or only commented lines, are ignored by the Java compiler.


Inherited

In Java, one class can be derived from other classes. If you are creating a class and already have a class with the properties or methods you need, you can inherit the class from the newly created class.

Inherited methods allow you to reuse methods and properties of existing classes without overrideing the code. Inherited classes are called superclasses, and derived classes are called subclasses.

Interface

In Java, interfaces can be understood as protocols for objects to communicate with each other. Interfaces play an important role in inheritance.

Interfaces define only the methods that are derived, but the implementation of the method depends entirely on the derived class.

The next section describes classes and objects in Java programming. You'll then have a clearer understanding of the classes and objects in Java.