Home > Java > javaTutorial > body text

Detailed records of java learning basic grammar

高洛峰
Release: 2017-03-19 11:37:26
Original
1344 people have browsed it

Preface

The grammar of java should be learned from the basic grammar first. The Java language is composed of classes and objects, whose objects and classes are composed of methods and Variables, and methods include statementsandexpression.

  • Object: (Almost) everything is an object, such as: a panda, his appearance, color, what he is eating, drinking, sleeping and playing

  • Class: If everything is an object, then what determines the appearance and behavior of a certain type of object? A class is a template that describes the behavior and state of a class of objects.


  • class HelloWorld{/*这表示一个类,class后跟的是类名*/}
    Copy after login
  • Method: (Method can also be called memberfunction ) Methods can be regarded as behaviors. A class can have many methods. Logical operations, data modification, and all actions are completed in methods.

  • Variables: Each object has unique instance variables, and the state of the object is determined by the values ​​of these instance variables.

First java program

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

}
Copy after login

Speak about saving and compiling, Run this program

  1. Use Notepad to save this code. After saving, change the file name to HelloWolrd.java (remember to display the file suffix) as shown in the figure:

  Detailed records of java learning basic grammar

 2. Open the dos window and find the location you saved (for example: my location is D:\HelloWorld)

  Detailed records of java learning basic grammar

 3. Switch the drive letter to your file storage directory

  Detailed records of java learning basic grammar

 4. Enter javac HelloWorld.java and press Enter. There should be an additional HelloWorld in the HelloWorld folder. class file, this compiles this code

  Detailed records of java learning basic grammar

  Detailed records of java learning basic grammar

 5. Enter java HelloWorld again and print out Hello World

Detailed records of java learning basic grammar

Basic Grammar

1. Case sensitive: Size Writing is sensitive, for example, HelloWorld and helloworld are different

2. Class name: The first letter of the java class name must be capitalized, and the class name is composed of multiple letters like UserNameManage , then the first letter of each word should be capitalized, commonly known as the big camel case nomenclature (ie: Pascal nomenclature)

3. Method name: One word Lowercase, for example: user, the first letter of multiple words is lowercase, starting from the second word, the first letter of each word is capitalized, for example: userNameManage (little camel case nomenclature)

4. Keywords : All lowercase, for example: public

5. Constant: All uppercase, for example: PI

6. Variable : The rules are the same as the method naming

7. Package: All lowercase

[java identifier]

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

 1. 只能使用字母、数字、下划线和美元符。

 2. 只能以字母、下划线和美元符开头。也就是不能以数字开头。 

 3. 严格区分大小写,没有长度限制。建议不要太长。

 4. 应该具有特定的意义,关键字不可以用作标识符。

 

java修饰符】

Java可以使用修饰符来修饰类中方法和属性。主要有两类修饰符:

 

java分隔符】

  • 具有:空格、圆括号、花括号、分号等。 

  • 每条语句无论一行还是多行都以分号结束。块(Block)是包含在{}里面的多条语句,块可以嵌套。空白插在代码的元素中间:由一个或多个空格组成,也可以由一个或多个tab空格组成多个空行。

【java注释

 写程序注释是必不可少的一部分。做规范,显条理,对于以后的开发带来了方便。

public class HelloWorld {    
    /**
     * @param args
     * 第一个Java程序
     * 这是文档注释     */
    public static void main(String[] args) {        /*
         * 这是多行注释         */
        System.out.println("Hello World");        // 打印Hello World(这是一个单行的注释)
        System.out.println("Hello World");
    }
    
}
Copy after login

【java关键字】

##extends Inherit class X to add functionality by extending class Y, or Add variables, or add methods, or override methods of class Y. An FalseFalseFinalA keyword in Java language. You can only define an entity once and cannot change it or inherit from it later. More strictly speaking: a final-modified class cannot be subclassed, a final-modified method cannot be overridden, and a final-modified variable cannot change its initial value. Finally is used to execute a piece of code regardless of whether there is an exception or runtime in the previously defined try statement. An error occurred. ForGotoC language##IfNewPackage means is used in the declaration of methods or variables. It means that this method or variable can only be accessed by other elements of this class. is used in the declaration of methods and variables. It means that this method or variable can only be used by the same method or variable. Accessed from elements in a class, a subclass, or a class in the same package. is used in the declaration of methods and variables. It indicates that this method or variable can be used by other classes Access elements in . ##Parent ClassSwitch is a selection statement, used with case, default, and break. SynchronizedThread synchronizationThis# is used to represent an instance of the class in which it appears. this can be used to access class variables and class methods. ThreadsafeThrow Allows the user to throw an ##Throws TransientTrueTry throw an exception Voidvolatilewhile

关键字

含义

Abstract

用在类的声明中来指明一个类是不能被实例化的,但是可以被其它类继承。一个抽象类可以使用抽象方法,抽象方法不需要实现,但是需要在子类中被实现

Boolean

布尔类型,只有true或者false

Break

停止,并跳出本层循环

Byte

8bit (位),也就是8个1/0表示,即二进制

Case

用来定义一组分支选择,如果某个值和switch中给出的值一样,就会从该分支开始执行。

Catch

用来声明当try语句块中发生运行时错误或非运行时异常时运行的一个块。

Char

用来定义一个字符类型

Class

Const

在Java中,const是作为保留字以备扩充,同样的保留字以备扩充还有goto.你可以用final关键字.final也可以用于声明方法或类,被声明为final的方法或类不能被继承。一般C里是const 对应java用final

Continue

用来打断当前循环过程,从当前循环的最后重新开始执行,如果后面跟有一个标签,则从标签对应的地方开始执行。

Default

配合switch跟case使用,但case中没有找到匹配时,则输出或者为default后面的语句。

Do

用来声明一个循环,这个循环的结束条件可以通过while关键字设置

Double

Used to define a double type variable

##Else

If the condition of the

if statement is not met, the statement will be executed.

Extends

interface

extends another interface to add methods.

Float

## is used to define a floating point variable

is used to declare a loop. Programmers can specify statements to loop through, derive conditions, and initialize variables.

Although it is a Java keyword, it is only used in
, Java does not provide the Goto statement

##Java

a keyword in the programming language

, used to generate a conditional test. If the condition is true, the statement under the if is executed.

Implements
A keyword in the Java(TM) programming language that is optional in a class declaration , used to indicate the interface implemented by the current class.

Import
A keyword in the Java(TM) programming language that is specified at the beginning of the source file A class or entire package to be referenced, so that the package name does not have to be included when using it.

Instaceof
A two-operand Java(TM) language keyword used to test the first Whether the runtime type of the parameter is compatible with the second parameter.

Int
A keyword in Java(TM) used to define an integer variable

Interface
A keyword in Java(TM) used to define a series of methods and constants. It can be implemented by a class through the implements keyword.

Long
Used to define a long type variable

Native
Native method.

## Used to create a new method

Null

#When the String type is not assigned a value, the value of the variable is Null

Package

Private

Protected

Public

Return

is used to end the execution of a method. It can be followed by a value required in the method declaration.

Short

is used to define a short type variable.

Static

is used to define a variable as a class variable. A class maintains only one copy of a class variable, regardless of how many instances of the class currently exist. "static" can also be used to define a method as a class method. Class methods are called by the class name rather than a specific instance, and can only operate on class variables.

##Super

exception

object or any object that implements throwable

Used in the method declaration to indicate which exceptions are not handled by this method, but submitted to a higher level of the program.

is used to indicate that a field is not part of the serialization of the object. When an object is serialized, the values ​​of transient variables are not included in the serialized representation, whereas non-transient variables are included.

True

is used to define a statement block that may
. If an exception is thrown, an optional catch block handles the exception thrown in the try block. At the same time, a finally block will be executed regardless of whether an exception is thrown.

# is used in a method declaration in the Java language to indicate that this method does not have any return value. "void" can also be used to express a statement without any function.

is used in the declaration of a variable to indicate that the variable is modified asynchronously by several threads running at the same time. of.

is used to define a loop statement that is executed repeatedly. The loop's exit condition is part of the while statement.

The above is the detailed content of Detailed records of java learning basic grammar. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template