Home >Java >javaTutorial >JAVA tutorial | basic syntax
A Java program can be thought of as a collection of objects, and these objects work together by calling each other's methods. The following briefly introduces the concepts of classes, objects, methods and instance variables.
##Object: An object is an instance of a class and has state and behavior. For example, a dog is an object. Its status includes: color, name, and breed; its behaviors include: wagging its tail, barking, eating, etc.
Hello World .
public class HelloWorld { /* 第一个Java程序 * 它将打印字符串 Hello World */ public static void main(String []args) { System.out.println("Hello World"); // 打印 Hello World } }
C : > javac HelloWorld.java C : > java HelloWorld Hello World
When writing Java programs, you should pay attention to the following points:
##Case Sensitive: Java is case sensitive, which means that the identifiers Hello and hello are different.
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, for example, 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.
Source file name: The source file name must be the same as the class name. When saving the file, you should use the class name as the filename (remember Java is case-sensitive) and the filename suffix .java. (If the file name and class name are different, a compilation error will occur).
Main method entrance: All Java programs consist of public static void main(String []args)method Begin execution.
All components of Java need names.
Class names, variable names, and method names are all called identifiers.
## Regarding Java identifiers, there are the following points to note:
## Examples of illegal identifiers: 123abc, -salary
(scope)
Like other languages, Java can use modifiers to modify methods and properties in a class. There are two main types of modifiers:
访问控制修饰符 : default, public , protected, private
非访问控制修饰符 : final, abstract, strictfp
Java中主要有如下几种类型的变量
局部变量
类变量(静态变量)
成员变量(非静态变量)
数组是储存在堆上的对象,可以保存多个同类型变量。在后面的章节中,我们将会学到如何声明、构造以及初始化一个数组。
Java 5.0引入了枚举,枚举限制变量只能是预先设定好的值。使用枚举可以减少代码中的bug。
例如,我们为果汁店设计一个程序,它将限制果汁为小杯、中杯、大杯。这就意味着它不允许顾客点除了这三种尺寸外的果汁。
注意:枚举可以单独声明或者声明在类里面。方法、变量、构造函数也可以在枚举中定义。
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 ; } }
Java reserved words are listed below. These reserved words cannot be used in the names of constants, variables, and any identifiers.
Keyword Description abstract Abstract method, abstract class modifier assert Assert whether the condition is met boolean Boolean data type break Break out of the loop or label code segment byte 8-bit signed data type case A condition of the switch statement catch Combined with try to capture exception information char 16-bit Unicode character data type class Definition class const Unused continue Do not execute the remainder of the loop default Default branch in switch statement do Loop statement, the loop body will be executed at least once double 64-bit double precision floating point number else The branch executed when the condition is not true enum Enumeration type extends Indicates that one class is a subclass of another class final Indicates that a value cannot be changed after initialization
Indicates that the method cannot be overridden, or a class cannot have subclassesfinally Designed to complete the executed code, mainly for the robustness and stability of the program Integrity, code is executed regardless of whether an exception occurs. float 32-bit single precision floating point number for for loop statement goto Not used if Conditional statement implements Indicates that a class implements the interface import Import class instanceof Test whether an object is an instance of a class int 32-bit integer interface Interface, an abstract type with only definitions of methods and constants long 64-bit integer native Indicates that the method is implemented in non-java code new Allocate a new class instance package A series of related classes form a package private indicates that private fields, methods, etc. can only be accessed from within the class protected indicates that the field can only be accessed from within the class Access via class or its subclass
Subclasses or other classes in the same packagepublic represents shared properties or methods return Method return value short 16-digit number static means in Class level definition, shared by all instances strictfp Floating point comparison uses strict rules super Indicates the base class switch Select statement synchronized Indicates only the same time A block of code that can be accessed by a thread this represents a call to the current instance
Or call another constructorthrow throws exception throws define method possible Thrown exception transient Modify fields not to be serialized try Indicates the code If the block needs to handle exceptions or cooperate with finally to indicate whether an exception is thrown, the code in finally will be executed void The marked method does not return any value volatile Marked fields may be accessed by multiple threads at the same time without synchronization while while loop
in comments will be ignored by the Java compiler
## Similar to C/C++ , Java also supports single-line and multi-line comments. The characters.
##Blank lines or lines with comments will be ignored by the Java compiler.
#Inheritance
#In Java, a class Can be derived from other classes. If you are creating a class and there is already a class that has the properties or methods you need, then you can inherit the newly created class from that class.
Using inherited methods, you can reuse methods and properties of existing classes , without having to rewrite these codes. The inherited class is called a super class, and the derived class is called a subclass.
interfaceIn Java, an interface can be understood as a protocol for communication between objects. Interfaces play a very important role in inheritance.##
The interface only defines the methods to be used by the derived class, but the specific implementation of the method completely depends on the derived class.
## is shown in the figure below:
# The above is the content of JAVA entry tutorial | basic syntax. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!