Table of Contents
java修饰符】" >java修饰符】
java分隔符】" >java分隔符】
【java关键字】
Home Java javaTutorial Detailed records of java learning basic grammar

Detailed records of java learning basic grammar

Mar 19, 2017 am 11:37 AM
java

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

See all articles