Second, Java Basic syntax

Source: Internet
Author: User

First, identifiers

The sequence of characters used by Java to name features such as variables, methods, and classes becomes an identifier, and popular points, where you can name them, are called identifiers, and they follow the rules of identifiers.

1. Naming rules for identifiers:

1) identifiers consist of characters, underscores, dollar symbols, or numbers.

2) identifiers should start with a character, underscore, dollar symbol

3) Java identifier case sensitive, unlimited length

4) Conventional, Java identifier selection should pay attention to "see the meaning of" and not with the Java language Keywords (eclipes in the color of the basic is a keyword) duplicate name

A valid identifier

An illegal identifier.

Helloword Class
DataClass dataclass#
_983 98.3
$6s5_c7 Hello World

Second, the key word

Some of the key strings in Java that are assigned to specific meanings for specialized purposes are keywords (keyword), and most editors mark keywords in a special way

All Java keywords are lowercase english

Some common keywords

Abstract Default If Private This
Boolean Do Implements Protected Throw
Break Double Import Public Throws
Byte Else instanceof Return Transient
Case Extends Int Short Try
Catch Final Interface Static void
Char Finally Long Stricrfp Volatile
Class Float Native Super While
Const For New Switch Null
Continue Goto Package Stnchronized
Basic data type 1.java constants for Java

The constant value of Java is represented by a string, and the extents are divided into different data types. Such as:

Integral type constant: 1234

Real constant: 3.14

Character constant: ' A '

Logical constants: TRUE, False

String constants: "HelloWorld"

Attention:

1) Distinguishing between character constants and string constants

A character constant is a single quotation mark, and a string constant is an unlimited length that is caused by double quotation marks.

Since Java is Unicode encoded, each character occupies two bytes, so it can be represented by the hexadecimal encoding of the day and, of course, in a Chinese language (a single Chinese takes Two bytes)

2) The "constant" position will also be used in other contexts to represent variables with immutable values

2.java variables

Java variables are the most basic storage units in a program, and their features include variable names, variable types, and scopes.

Each variable in a Java program belongs to a specific data type, which must be declared before it is used, in the form of:

Type VarName [=value]

For example:

int i = 100;

Foloat f=12.3f;

Double d1,d2,d3=0.123; (three variables declared, where D1,D2 is the default value, d3=0.123)

String s= "Hello"

In essence, a variable is actually a small area of memory, using the variable name to access the area, so each variable must be declared before it is used, and then it must be assigned before it can be used.

1) Java variable classification

Divided by the declared location:

Local variable: A variable defined inside a method or statement block that can only be used within a defined method or statement block

Member variable: A variable that is defined inside a method outside of a class and can be used throughout a class, including a method within a class or a block of statements

Note: Outside the class is a declaration that cannot have a variable

By the data type that belongs to:

Basic Data type variables

Reference data type variables

Iv. operators

The Java language supports the following operators:

Arithmetic operators: +-*/% + +--

Relational operators:> < >= <= = = =!

Logical operator:!        & | ^ && | |

Bitwise operator:& | ^ ~ >> << >>>

Assignment operator: =

Extended assignment Operator: + = = *=/=

String link operator: +

Three-mesh conditional operator? :

1. Arithmetic Operators

Attention:

1)

In the bitwise operator! , &, |, and ^ are the same as the Logitech operators, except that bitwise operators operate on the binary of variables, and I don't personally use them, so I'm not here to introduce more.

2)

+ + (-)

First operation and then the value

To take a value before the operation

2. Logical operators

For example:

3. Extended Assignment operators

4. String connector

As long as one of the operands on either side of the "+" operator is a string type, the system automatically converts the other operand to a string and then joins it, for example:

4 + 5 = 9

4 + "AB" = "4ab"

5. Three Mesh condition operator

Syntax format:

X? Y:z

where x is a Boolean expression, the value of x is evaluated first, and if true, the result of the entire trinocular operation is the value of the expression Y, otherwise the entire operation result is the value of the expression Z.

V. Expressions and statements 1. Expressions

An expression is a sequence of operators and operands that conform to a certain syntax rule, such as:

A

5.0 + A

(a–b) * c–4

I < && i%10!=0

1) Type and value of an expression

The result of an operand in an expression is called the value of an expression

The data type of an expression value is the type of an expression

2) Order of operation of expressions

should be in order of precedence of operators from highest to lowest

Operators with the same precedence are implemented in the same way as implementation conventions

I personally think that the sequence of operations can be ignored, first of all, my logical thinking ability is not particularly strong, but also my memory is not particularly good, so if I need to prioritize in the expression I would choose parentheses. But I think for some of the more complex and critical logic operations, if the individual logical computing ability plus memory is better to ensure that no error, the best use of Logitech operator priority is not a way to let others understand your code copy. is a kind of small protection, anyway, I am such a person will not try to analyze this code, too tired ~ ~

2. Branch (conditional) statements

If

If ... else

If ... else if

If-else if ... else if

Switch () {

Case XX:

...............

Case XX:

...............

Default

...............

}

A switch statement in 1.java can only detect values of type int (a value of type char is also possible, because he can convert city int type)

2. Be careful with case penetration, so it is best to

3. Multiple cases can be used, such as the following example code can also be written (when the i=1,2,18 will output 18):

Example:

3. Looping statements

for (...; ...; ...) {......}

while (...) {......} Judge the contents of the curly braces before you decide if you want to continue

do{...} whille (...); Execute the contents of the curly brace before judging if you want to continue execution

4.break & Continue Statements

The break statement is used to terminate execution of a block of statements. In the loop body statement, you can force out of the loop.

The continue statement is used in a loop body statement to terminate a looping process, skipping a loop that is not executed under the Continue statement in the loop, and starting the next loop.

Viii. methods

Java's approach is similar to a function in other languages, a snippet of code used to accomplish a particular function, declaring the format:

[modifier 1 modifier 2 ...] Return value type method name (formal parameter list) {

Java statements

Formal parameters: Used to accept data entered by the outside of the method when it is invoked

Arguments: Data that is actually pure to the method when the method is called

Return value: The method returns data to the environment that called him after execution is complete

Return value type: The data type of the implementation contract return value, such as no return value, must be given the return value type void

Calling method in the Java language: Object name. Method Name (argument list)

The number, data type, and order of the arguments must match the parameter list of the called method declaration

The return statement terminates the run of the method and specifies the data to be returned

When passing parameters in a function call in Java, follow the principle of value passing:

The base type passes the data value itself, referencing the heart of the United States is passing a reference to the object, not the object itself

In the example of Method 1, the previously defined data type is void, so the method cannot have a return value, in method 4 because there is a return value, so the first must be defined as the type of the return value, that is, the M4 in front of the int

Nine, Recursive call

Recursive invocation refers to the invocation of the method itself during the execution of a method

Let's look at an example, which is a simple recursive call:

Under Simple analysis:

1. First the main method of the output string, the content is the test method when the parameter equals 5 of the return value

2. Then pass the parameter 5 into the test method, the return value is: 5 * Test (4)

3. Pass the parameter 4 again into the test method, the return value is: 4 * Test (3)

4. Then pass the parameter 3 into the test method, the return value is: 3 * Test (2)

5. Re-say parameter 2 in the incoming test method: The return value is: 2 * Test (1)

6. Then pass the parameter 1 into the method: The return value is: 1

Then the program starts to walk back, and the return value is passed in test (1) to get 2*1

Go back and pass the 2*1 you just got into Test (2) to get 3*2*1

Go back and get the 3*2*1 you just got into Test (3) 4*3*2*1

And go back and pass the 4*3*2*1 you just got to the return value of test (5) in Test (4) and get 5*4*3*2*1

Finally, the return value of TEST5 is passed to our main method output in the statement 5*4*3*2*1=120, then our output statement output should be 120

This is an example of a simple recursive invocation.

Let's look at one more example:

A non-recursive notation:

Please understand yourself.

Second, Java Basic syntax

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.