First copy Run a HelloWorld
1 Public class HelloWorld {2 Public Static void Main (String []args) {3 // Print Hello World 4 }5 }
Save As Helloworld.java (this requires the same file name as the class name)
(Linux environment) open the terminal and CD to the directory where the current Java file resides
Input: Javac Helloworld.java
Press ENTER to compile the code, you can see the file directory under the compiled file Helloworld.class
Re-enter: Java HelloWorld
You can see Hello World in the window,
(Please ignore two lines picked up _java_options)
Successfully wrote the HelloWorld
and start learning.
Basic Syntax :
Some points to note when writing a program:
1. Case sensitive, that is, HelloWorld is different from HelloWorld
2. Class names, variable names, and method (function) names are called identifiers, and the identifier naming convention is basically the same as C + +
- class name : For all classes, the first letter of the class name should be capitalized. If the class name consists of several words, the first letter of each word should be capitalized, such as Myfirstjavaclass.
- method Name : All method names should start with a lowercase letter. If the method name contains several words, the first letter of each subsequent word is capitalized.
3. The source file name must be the same as the class name. When you save the file, you should use the class name to save the filename with the suffix. java. Specifically, the class name is the same as the public class in the class, so there can be only one public class in a Java file
4. All Java programs are executed by the public static void main (String []args) method. Obviously it should be in the Publilc class.
Java modifier:
Like C + +, Java can use modifiers to decorate methods and properties in a class. There are two main types of modifiers:
- Access control modifiers: default, public, protected, private
- Non-access control modifiers: final, abstract, static
Modifier usage and meaning can be basically similar to C + +
Final similar to const,abstract similar to virtual,static similar to static
It's not believable that I'm talking about, and I'm going to go into a few more modifiers later.
Java Variable type:
There are several types of variables in Java that are mainly
Local variables
class variables (static variables)
Member variables (non-static variables)
Local variables:
- Local variables are declared in methods, construction methods, or block of statements, created when the method is executed, and when they are done, the variable is destroyed, so it is only visible in the method, constructor, or statement block that declares it
- Local variables are allocated on the stack.
- Local variables have no default values and must be initialized before they are declared to be used
(functions and constructors are just as well as C + +, statement blocks are left behind to explain)
Instance variable:
- Instance variables are declared in a class, but outside of methods, construction methods, and statement blocks
- The instance variable has a default value. The default value for a numeric variable is 0, the default value for a Boolean variable is false, and the default value for the reference type variable is null. The value of a variable can be specified at the time of declaration, or it can be specified in the constructor method
- Instance variables are created when the object is created and destroyed when the object is destroyed
- The value of the instance variable should be referenced by at least one method, constructor, or statement block, allowing external access to instance variable information (it is recommended that you have getter and setter functions
- An access modifier can decorate an instance variable, but cannot decorate the above local variable
- Instance variables are visible to methods, construction methods, or block statements in a class. In general, you should set the instance variable to private. You can make instance variables child classes visible by using the access modifier
- Instance variables can be accessed directly from the variable name. However, in static methods and other classes, you should use the fully qualified name: Obejectreference.variablename.
class variables (static variables, much like the static variables of C + + classes)
- Class variables, also known as static variables, are declared with the static keyword in a class, but must be outside of method construction methods and statement blocks
- A class has only one copy of a class variable, regardless of how many objects a class creates
- Static variables are seldom used except when declared as constants. Constants are variables declared as public/private,final and static types. Constants cannot be changed after initialization
- Static variables are stored in a static storage area. Often declared as constants, rarely using static declaration variables alone
- Static variables are created at the beginning of the program and destroyed at the end of the program
- has similar visibility to instance variables. However, in order to be visible to the consumer of a class, most static variables are declared as public types
- The default value is similar to the instance variable. The default value for numeric variables is 0, the Boolean default is False, and the reference type default value is null. The value of a variable can be specified at the time of declaration, or it can be specified in a constructor method. In addition, a static variable can be initialized in a static statement block.
- Static variables can be accessed by:classname.variablename
- Class variable names are generally recommended to use uppercase when the class variable is declared as public static final type. If static variables are not public and final, they are named in the same way as instance variables and local variables
Member variables (non-static variables)
is probably the non-static variable in the instance variable
Java keywords:
The following is a list of Java reserved words. These reserved words cannot be used for constants, variables, and names of any identifiers.
Key Words |
Description |
Abstract |
Abstract methods, modifiers for abstract classes |
Assert |
Whether the assertion condition satisfies |
Boolean |
Boolean Data types |
Break |
Jump out of a loop or label code snippet |
Byte |
8-bit Signed data types |
Case |
A condition for a switch statement |
Catch |
Match the try with catch exception information |
Char |
16-bit Unicode character data type |
Class |
Defining classes |
Const |
Not used |
Continue |
Do not execute the remainder of the loop body |
Default |
Default branch in the switch statement |
Do |
Loop statement, the loop body is executed at least once |
Double |
64-bit double-precision floating-point number |
Else |
The branch that executes when the IF condition is not established |
Enum |
Enum type |
Extends |
Represents a class that is a subclass of another class |
Final |
Indicates that a value cannot be changed after initialization Indicates that a method cannot be overridden, or that a class cannot have subclasses |
Finally |
In order to complete the execution of the code design, mainly for the robustness and integrity of the program, regardless of whether there is no exception to execute code. |
Float |
32-bit single-precision floating-point number |
For |
For Loop statement |
Goto |
Not used |
If |
Conditional statements |
Implements |
Indicates that a class implements an interface |
Import |
Import class |
instanceof |
To test whether an object is an instance of a class |
Int |
32-bit integer number |
Interface |
interface, an abstract type, with only the definition of methods and constants |
Long |
64-bit integer number |
Native |
Represents methods implemented in non-Java code |
New |
Assigning a new class instance |
Package |
A series of related classes make up a package |
Private |
Represents a private field, or method, etc., accessible only from within the class |
Protected |
Indicates that a field can only be accessed through a class or its subclasses Subclasses or other classes within the same package |
Public |
Represents a shared property or method |
Return |
Method return value |
Short |
16-digit number |
Static |
Represents a class-level definition that is shared by all instances of the |
Strictfp |
Floating-point comparisons use strict rules |
Super |
Represents a base class |
Switch |
SELECT statement |
Synchronized |
A block of code that represents a single thread that can be accessed at the same time |
This |
Represents the invocation of the current instance or call another constructor |
Throw |
Throw exception |
Throws |
Defining exceptions that a method might throw |
Transient |
Modify fields that do not serialize |
Try |
Represents a code block to do exception handling or a finally mate to indicate whether throwing an exception executes the code in the finally |
void |
Tag method does not return any values |
Volatile |
Tag fields may be accessed by multiple threads at the same time without synchronizing |
While |
While loop |
Java Annotations:
Exactly the same as the C + + comment.
At this point, you should be able to write an analogy with C + + complete to compile a small code (probably)
Some things are like C + +, but they may not be clear.
So how does the analogy vary from person to person?
Koko
Reference: http://www.runoob.com/java/java-basic-syntax.html
Java Basic syntax