Detailed description of the basic syntax of java, java syntax

Source: Internet
Author: User

Detailed description of the basic syntax of java, java syntax

Preface]

Java syntax begins with basic syntax.ClassAndObjectThe object and class are composedMethodAndVariableAnd methods includeStatementAndExpression.

  • Object(Almost) Everything is an object, such as: A pandatv, his appearance, color, he is eating, drinking, sleeping, playing
  • Class: If everything is an object, what determines the appearance and behavior of a class of objects? A class is a template that describes the behavior and status of a class of objects.
  • Class HelloWorld {/* Indicates a class, followed by the class name */}
  • Method(The method can also be calledMember Functions) Method you can think of as behavior. A class can have many methods. Logical operations, data modifications, and all actions are completed in the method.
  • Variable: Each object has a unique instance variable. The object State is determined by the value of these instance variables.

 

The first java program]

Public class HelloWolrd {/*** first Java program */public static void main (String [] args) {// print Hello World System. out. println ("Hello World ");}}

 

Save, compile, and run this program.

   

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

   

3. Switch the drive letter to your file storage directory.

   

4. Enter javac HelloWorld. java and press Enter. A HelloWorld. class file should be added to the HelloWorld folder, which compiles the code

   

   

5. Input java HelloWorld again to print out Hello World

   

 

Basic syntax]

1.Case Sensitive: Case sensitivity. For example, HelloWorld and helloworld are different.

2.Class Name: The first letter of a java class name must be capitalized, and the class name may consist of multiple letters, such as UserNameManage. the first letter of each word should be capitalized, the big camper method (Pascal), commonly known as the "camper" Method)

3.Method Name: A word is 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)

4.Keywords: All are in lower case, for example: public

5.Constant: All uppercase values, for example, PI

6.Variable: The rule is named the same as the method.

7.Package: All lowercase letters

 

[Java identifier]

All components of Java must be named. The class name, variable name, and method name are all called identifiers.

1. Only letters, numbers, underscores, and dollar characters can be used.

2. It can only start with letters, underscores, and dollar characters. That is, it cannot start with a number.

3. It is case sensitive and has no length restrictions. We recommend that you do not set the length to too long.

4. It should have a specific meaning. Keywords cannot be used as identifiers.

 

JavaModifier]

Java can use modifiers to modify class methods and attributes. There are two main modifiers:

  • Access Control modifier: default, public, protected, private
  • Non-Access Control modifier: final, abstract, strictfp

 

JavaSeparator]
  • Including space, Parentheses, curly braces, and semicolons.
  • Each statement ends with a semicolon. Blocks are multiple statements contained in {}. blocks can be nested. The blank space is inserted in the middle of the element of the Code: it consists of one or more spaces, or one or more tabs and spaces to form multiple blank lines.

[Java Note]

Writing Program comments is an essential part. Standardized and organized to facilitate future development.

 

Public class HelloWorld {/*** @ param args * first Java program * this is a document comment */public static void main (String [] args) {/** this is a multi-line comment */System. out. println ("Hello World"); // print Hello World (this is a single row comment) System. out. println ("Hello World ");}}
[Java keyword]

Keywords

Description

Abstract

It is used in the declaration of classes to indicate that a class cannot be instantiated, but can be inherited by other classes. An abstract class can use abstract methods. abstract methods do not need to be implemented, but must be implemented in subclasses.

Boolean

Boolean Type, only true or false

Break

Stops and jumps out of this layer of Loop

Byte

8 bits (BITs), that is, 8 1/0 represents, that is, binary

Case

It is used to define a group of Branch selection. If a value is the same as the value given in the switch, it is executed from this branch.

Catch

It is used to declare a block that runs when a runtime error occurs in the try statement block or a non-runtime exception occurs.

Char

Used to define a character type

Class

Class

Const

In Java, const is used as reserved words for expansion, and the same reserved words are used for expansion and goto. you can use the final keyword. final can also be used to declare methods or classes. Methods or classes declared as final cannot be inherited. Generally, the const in C corresponds to the final in java.

Continue

It is used to interrupt the current loop process and re-execute the process from the end of the current loop. If there is a tag behind it, it is executed from where the tag corresponds.

Default

The statement is output or followed by default when the switch and case are used, but no matching is found in the case.

Do

Used to declare a loop. The ending condition of this loop can be set using the while keyword.

Double

Used to define a double Type Variable

Else

If the conditions of the if statement are not met, the statement is executed.

Extends

The extends inheritance Class X adds functions through the extend class Y, or adds variables, or adds methods, or overwrites the method of class Y. One interface extends another interface to add methods.

False

False

Final

A keyword in Java. You can only define an object once and cannot change or inherit it later. More strictly speaking: A final-modified class cannot be subverted, a final-modified method cannot be overwritten, and a final-modified variable cannot change its initial value.

Finally

It is used to execute a piece of code, regardless of whether an exception or runtime error occurs in the try Statement defined earlier.

Float

Used to define a floating point variable

For

Declare a loop. Programmers can specify the statements to be cyclic, introduce conditions, and initialize variables.

Goto

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

If

A keyword of Java programming language used to generate a condition test. if the condition is true, the statement under if is executed.

Implements

A keyword of the Java (TM) programming language is optional in the class declaration to specify the interface implemented by the current class.

Import

A keyword of Java (TM) programming language. It specifies a class or the entire package to be referenced later in the start part of the source file, so that the package name does not need to be added during use.

Instaceof

The Java (TM) Language keyword of a Two-operand is used to test whether the runtime type of the first parameter is compatible with the second parameter.

Int

A keyword of Java (TM), used to define an integer variable.

Interface

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

Long

Defines a long variable.

Native

Local method.

New

Used to create a new method

Null

If the String type is not assigned a value, the value of this variable is Null.

Package

Meaning of Package

Private

Used in the sound of a method or variable. It indicates that this method or variable can only be accessed by other elements of this class.

Protected

It is used in the declaration of methods and variables. It indicates that this method or variable can only be accessed by elements in the same class, subclass, or class in the same package.

Public

It is used in the declaration of methods and variables. It indicates that this method or variable can be accessed by elements in other classes.

Return

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

Short

Defines a variable of the short type.

Static

Defines a variable as a class variable. The class only maintains a copy of the class variable, no matter how many instances the class currently has. "Static" can also be used to define a method as a class method. A class method is called by class name instead of a specific instance and can only operate class variables.

Super

Parent class

Switch

It is a selection statement used with case, default, and break.

Synchronized

Thread Synchronization

This

An instance that represents the class it appears. This can be used to sort class variables and class methods.

Threadsafe

Throw

Attackers can throw an exception object or any object that implements throwable.

Throws

It is used in the method declaration to indicate which exceptions this method does not handle, but is submitted to a higher layer of the program.

Transient

Indicates that a domain is not a serialized part of the object. When an object is serialized, the value of the transient variable is not included in the serialized representation. However, non-transient variables are included.

True

True

Try

Defines a block that may throw an exception. If an exception is thrown, an optional catch Block will handle the exception thrown in the try block. At the same time, a finally statement block is executed, regardless of whether an exception is thrown.

Void

The method declaration in Java indicates that this method does not return any value. "Void" can also be used to indicate a statement without any function.

Volatile

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

While

Defines a loop statement that is executed repeatedly. The exit condition of the loop is part of the while statement.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.