Dark Horse programmer-Java basics-basic composition of Java language

Source: Internet
Author: User
Tags bitwise operators

 

Keywords: Access Control: Private, Protected, public classes, methods, and variable modifiers: abstract declarative abstract, class, extends inheritance, final constants, implements implementation, interface, new creation, static, synchronized thread synchronization statement: break jumps out of the loop, continues to execute, return returns a value, do run, while when ...... When the loop, if ...... Just ...... , Else, and vice versa ...... , For Loop, instance, switch, case ...... And default exception handling: catch Exception Handling, finally statement to be executed no matter what the situation, throw an exception object, throws declare that an exception may be thrown, try to catch the basic types of exceptions: boolean, byte, Char, double, float, Int, long, short, null, true, false False false packets: import package and create package

Variable reference: super parent class reference, this class reference, void no return value

Identifier: Valid identifier: it cannot start with a number or use keywords or reserved words.

Note:Single line comment //, multi-line comment/**/, text comment /***/

Constants and variables: All the letters of a constant must be larger. Use final to modify them; when writing a variable, it must comply with the naming rules of the variable (it cannot start with a number, it cannot use symbols other than _ and $, and the first letter is the first letter of each word after the lowercase words in uppercase ).

Operator:

Arithmetic Operators

Value assignment operator

Logical operators

Bitwise operators

Ternary Operators

Statement:

If-ElseStatement: first, judge the condition statement. If the result of the Condition Statement isTrue.IfThe following statement, if it isFalse.ElseThe following statement. In actual development, you will often encounter nested if -- else structure or multiple if selection structures.

Syntax format:

If (Condition)

{

CodeBlock 1; (If -- else can be nested)

} Else if {// there can be multiple else if or none here. If the condition has a continuous relationship, its order is continuous and cannot be jumped, either from large to small, it is either small to large. If it's just simple mutual

Code Block 2; // no order is required for rejection

} Else {

Code block 3;

}

The following code

SwitchStatement: Switch indicates the switch. The variable can beByte, short, Int, Char, BooleanThe data type of. A case must be followed by a constant expression. There can be multiple cases, but they cannot be the same. The order is not limited. The default statement is executed when none of the case statements are met and can be placed anywhere. The break statement is used to exit the current structure.

Switch (Variable){

 

CaseVariable value1:

 

Execution statement1

 

Break;

 

CaseVariable value2:

 

Execution statement2

 

Break;

 

...

 

CaseVariable valueN:

 

Execution statementN

 

Break;

 

Default:

 

Execution statementDefault

 

}

Code experience:

Import java. util. collections;
Public class Test2 {

Public static void main (string [] ARGs ){
Wrote input = new partition (system. In );
System. Out. Print ("Enter the score:"); // friendly prompt
Int chengji = input. nextint ()/10; // enter the score on the keyboard and divide it by 10
Switch (chengji ){
Case 10:
System. Out. println ("My father buys a car for him ");
Break;
Case 9:
System. Out. println ("Mom buys him a laptop ");
Break;
Case 8:
System. Out. println ("Mom buys him a cell phone ");
Break;
Case 7:
System. Out. println ("Mom buys him a cell phone ");
Break;
Case 6:
System. Out. println ("Mom buys him a cell phone ");
Break;
Default:
System. Out. println ("no gift ");
Break;
}

 

While loop statement:Judge first and then execute

While (loop condition judgment Statement)

{

Loop operation, the statement to be executed;

Iteration statement;

}

Code Demonstration:

Public class test3 {
Public static void main (string [] ARGs ){
// 5, 10, 15, 20, 25 .. 100
// 1. Initial Value
Int I = 5;
// 2. Cyclic Conditions
While (I <= 100 ){
System. Out. println (I); // repeat the task
// 3. Change the Initial Value
I + = 5; // I = I + 5;
}

}

}

Do ---- while () loop statement: In any case, the statement must be executed once first, and then the loop condition is determined to determine whether to continue executing the loop.

Do {

Cyclic Operation

} While (loop condition judgment Statement)

Code Demonstration:

Public class test4 {
Public static void main (string [] ARGs ){

Do {

If (I % 2 = 0 ){

System. Out. println (I );

}

I ++;

} While (I <= 10); // note that the semicolon ends.

 

For Loop: expression 1 is used to assign an initial value, expression 2 is a loop condition, and expression 3 is an iteration part.

For (expression 1; expression 2; expression 3)

{

Loop body;

}

Code Demonstration:

Public class test1 {
Public static void main (string [] ARGs ){
For (int K = 1; k <= 24; k ++ ){
If (K % 3 = 0)
{
If (k = 21)
{
Break; // terminate the cycle
// Continue; // skip and continue the next loop
}
System. Out. println (k );
}
}
}
}

After writing so much, let's summarize:

1. While: first judge the condition. The loop body is executed only when the condition is met.

2. Do While: Execute the loop body first. When the judgment conditions are met, the loop body is executed again. In a simple sentence: Do While: the loop body must be executed at least once no matter whether or not the conditions are met.

3. variables have their own scopes. For: If you define the increment used to control the loop in the for statement. The variable is valid only in the for statement. The for statement has been executed. The variable is released in the memory.

4. For and while can be exchanged. If you need to define a loop increment. It is more appropriate to use.

5. When you want to execute some statements multiple times, use the loop structure.

6. Continue: it can only act on the loop structure. Continue the loop. Feature: ends this cycle and continues the next cycle

7. The range of the break and continue statements. When break and continue exist independently, there can be no statements below. Because none of them can be executed.

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.