Java Learning Summary

Source: Internet
Author: User
Tags case statement class definition terminates throw exception java keywords

The following points should be noted when writing Java programs:
Case-sensitive: Java is case-sensitive, which means that the identifier Hello is different from hello.
Class name: For all classes, the first letter of the class name should be capitalized. If the class name consists of several words, the first letter of each word should be capitalized, such as Myfirstjavaclass.
Method Name: All method names should start with a lowercase letter. If the method name contains several words, the first letter of each subsequent word is capitalized.
Source file name: The source file name must be the same as the class name. When saving the file, you should use the class name to save the filename (remember that Java is case-sensitive), and the suffix of the file name is. java. (a compilation error is caused if the file name and the class name are not the same).
Main method Entry: All Java programs are executed by the public static void main (String args[]) method.




For the Java identifiers, there are a few things to note:
All identifiers should start with a letter (A-Z or a-Z), a dollar symbol ($), or an underscore (_).
Can be a combination of any character after the first character
Keyword cannot be used as an identifier
Identifiers are case-sensitive
Examples of legal identifiers: Age, $salary, _value, __1_value
Examples of illegal identifiers: 123abc,-salary


Java keywords

The following is a list of Java reserved words. These reserved words cannot be used for constants, variables, and names of any identifiers.
Keyword description
Abstract abstraction method, modifier of abstract class
Assert asserts whether the condition satisfies
Boolean Boolean data type
Break jumps out of the loop or label code snippet
Byte8-bit Signed data types
A condition for the Caseswitch statement
Catch and try match catch exception information
Char16-bit Unicode character data type
Class Definition Classes
Const not used
Continue do not perform the remainder of the loop body
The default branch in the DefaultSwitch statement
Do Loop statement, the loop body executes at least once
Double64-bit double-precision floating-point number
ElseIf the branch that is executed when the condition is not established
enum Enum type
Extends indicates that a class is a subclass of another class
Final indicates that a value cannot be changed after initialization.
Indicates that a method cannot be overridden, or that a class cannot have subclasses
Finally in order to complete the execution of the code design, mainly for the robustness and integrity of the program, regardless of whether there is no exception to execute code.
Float32-bit single-precision floating-point number
Forfor Loop Statements
Goto not used
If condition statement
Implements indicates that a class implements an interface
Import Imports Class
Instanceof testing whether an object is an instance of a class
Int32 bit integer number
Interface interface, an abstract type, with only the definition of methods and constants
Long64 bit integer number
Native representation method implemented in non-Java code
New to assign a fresh class instance
Package a series of related classes make up a bundle
Private indicates a proprietary field, or method, etc., accessible only from within the class
Protected indicates that a field can only be accessed through a class or its subclasses
Subclasses or other classes within the same package
Public indicates a common attribute or method
Return method returns a value
Short16 bit number
The static representation is defined at the class level, and all instances share the
STRICTFP floating-point comparison using strict rules
Super represents the base class
Switch Selection statement
Synchronized represents a block of code that can only be accessed by one thread at a time
This means that the current instance is called
or call another constructor
Throw throw exception
Throws defines a method that may throw an exception
Transient modifying fields that do not serialize
Try means that the code block is going to do exception handling, or that a finally mate indicates whether throwing an exception executes the code in the finally
void tag method does not return any value
Volatile tag fields may be accessed by multiple threads at the same time without synchronizing
Whilewhile Cycle


Two big data types for Java:
Basic data types
Reference data type
The Java language provides eight basic types. Six types of numbers (four integers, two floating-point types), one character type, and one Boolean type.


The Java language supports some special sequences of escape characters.
Symbolic character meaning
\ nthe line break
\ r Enter
\f Page Break
\b Backspace
\s spaces
\ t tab
\ "Double quotation marks
\ ' Single quotation mark
\ back Slash


Relational operators

The following table is a Java-supported relational operator
The value of the instance integer variable A in the table is 10, and the value of variable B is 20:
Operator Description Examples
= = Checks if the values of the two operands are equal, the condition is true if they are equal. (A = = B) is False (not true).
! = Checks if the values of the two operands are equal, and the condition is true if the values are not equal. (A! = B) is true.
> Checks if the value of the left operand is greater than the value of the right operand, and if so the condition is true. (a> B) not true.
< checks if the value of the left operand is less than the value of the right operand, and if so the condition is true. (A <b) is true.
> = checks whether the value of the left operand is greater than or equal to the value of the right operand, and if so the condition is true. (a> = B) is false.
<= checks if the value of the left operand is less than or equal to the value of the right operand, and if so the condition is true. (a <= B) is true.


Java Branching Structure-if...else/switch

Sequential structures can only be executed sequentially, not judged and chosen, so branching structures are required.
There are two types of branching structures in Java:
If statement
Switch statement
If statement

An If statement contains a Boolean expression and one or more statements.
Grammar

The syntax for the IF statement is as follows:
if (Boolean expression)
{
If the Boolean expression is true, the statement executed
}
If the value of the Boolean expression is true, the code block in the IF statement is executed. Otherwise execute the code after the IF statement block.
public class Test {

public static void Main (String args[]) {
int x = 10;

if (x < 20) {
System.out.print ("This is an if statement");
}
}
}
The results of the above code compilation run as follows:
This is the IF statement
If...else statements

The If statement can be followed by the Else statement, and if the Boolean expression value of the If statement is false, the ELSE statement block is executed.
Grammar

The usage of If...else is as follows:
if (Boolean expression) {
True if the value of a Boolean expression
}else{
If the value of the Boolean expression is False
}
Instance

public class Test {

public static void Main (String args[]) {
int x = 30;

if (x < 20) {
System.out.print ("This is an if statement");
}else{
System.out.print ("This is the Else statement");
}
}
}
The results of the above code compilation run as follows:
This is the Else statement
If...else if...else Statements

The If statement can be followed by a elseif...else statement, which detects a variety of possible scenarios.
When using the If,else if,else statement, the following points need to be noted:
The IF statement has at most 1 else statements, and ELSE statements after all ElseIf statements.
An If statement can have several ElseIf statements, which must precede the Else statement.
Once one of the else if statements detects true, the other else if and else statements will skip execution.
Grammar

The IF...ELSE syntax format is as follows:
if (Boolean expression 1) {
If the value of the Boolean expression 1 is true, the code is executed
}else if (Boolean expression 2) {
If the value of the Boolean expression 2 is true, the code is executed
}else if (Boolean expression 3) {
If the value of the Boolean expression 3 is true, the code is executed
}else {
If none of the above Boolean expressions executes code for true
}
Instance

public class Test {

public static void Main (String args[]) {
int x = 30;

if (x = = 10) {
System.out.print ("Value of X is 10");
}else if (x = = 20) {
System.out.print ("Value of X is 20");
}else if (x = = 30) {
System.out.print ("Value of X is 30");
}else{
System.out.print ("This is the Else statement");
}
}
}
The results of the above code compilation run as follows:
Value of X is 30
Nested IF...ELSE statements

It is legal to use nested IF-ELSE statements. This means that you can use an if or ElseIf statement in another if or ElseIf statement.
Grammar

The nested IF...ELSE syntax format is as follows:
if (Boolean expression 1) {
If the value of the Boolean expression 1 is true, the code is executed
if (Boolean expression 2) {
If the value of the Boolean expression 2 is true, the code is executed
}
}
You can nest else if...else like an if statement.
Instance

public class Test {

public static void Main (String args[]) {
int x = 30;
int y = 10;

if (x = = 30) {
if (y = = 10) {
System.out.print ("X = Y = 10");
}
}
}
}
The results of the above code compilation run as follows:
X = + and Y = 10
Switch statement

The switch statement determines whether a variable is equal to a value in a series of values, and each value is called a branch.
Grammar

The switch syntax format is as follows:
switch (expression) {
Case Value:
Statement
Break Options available
Case Value:
Statement
Break Options available
You can have any number of case statements
Default://Optional
Statement
}
The switch statement has the following rules:
The variable type in a switch statement can be only a byte, short, int, or char.
A switch statement can have more than one case statement. Each case is followed by a value and a colon to compare.
The data type of the value in the case statement must be the same as the data type of the variable, and can only be constants or literal constants.
When the value of a variable is equal to the value of the case statement, the statement after the case statement starts executing until the break statement appears and jumps out of the switch statement.
The switch statement terminates when a break statement is encountered. The program jumps to the statement following the switch statement execution. The case statement does not have to contain a break statement. If no break statement appears, the program continues to execute the next case statement until a break statement appears.
A switch statement can contain a default branch, which must be the last branch of a switch statement. Default is executed when there is no case statement with the value equal to the value of the variable. The default branch does not require a break statement.
Instance

public class Test {

public static void Main (String args[]) {
Char grade = Args[0].charat (0);
Char grade = ' C ';

Switch (grade)
{
Case ' A ':
System.out.println ("excellent");
Break
Case ' B ':
Case ' C ':
System.out.println ("good");
Break
Case ' D ':
SYSTEM.OUT.PRINTLN ("Pass");
Case ' F ':
SYSTEM.OUT.PRINTLN ("You need to keep trying");
Break
Default:
SYSTEM.OUT.PRINTLN ("Invalid rating");
}
System.out.println ("Your grade is" + grade);
}
}
The results of the above code compilation run as follows:
Good
Your rank is C.

There are three main loop structures in Java:
While loop
Do...while Cycle
For loop
An enhanced for loop that is primarily used in arrays is introduced in Java5.
While loop

While is the most basic loop, its structure is:
while (Boolean expression) {
Looping content
}
As long as the Boolean expression is true, the loop experience continues.
Instance

public class Test {
public static void Main (String args[]) {
int x = 10;
while (x <) {System.out.print ("Value of x:" + x); x + +; System.out.print ("\ n"); } } }
The results of the above example compilation run as follows:
Value of X:10
Value of X:11
Value of X:12
Value of X:13
Value of X:14
Value of X:15
Value of X:16
Value of X:17
Value of X:18
Value of x:19
Do...while Cycle

For a while statement, you cannot enter a loop if the condition is not met. But sometimes we need to do it at least once, even if we don't meet the conditions.
The Do...while loop is similar to the while loop, but the Do...while loop executes at least once.
do {
Code statements
}while (boolean expression);
Note: The Boolean expression is behind the loop body, so the statement block is executed before the Boolean expression is instrumented. If the value of the Boolean expression is true, the statement block executes until the value of the Boolean expression is false.
Instance

public class Test {

public static void Main (String args[]) {
int x = 10;

do{
System.out.print ("Value of x:" + x);
x + +;
System.out.print ("\ n");
}while (x < 20); } }
The results of the above example compilation run as follows:
Value of X:10
Value of X:11
Value of X:12
Value of X:13
Value of X:14
Value of X:15
Value of X:16
Value of X:17
Value of X:18
Value of x:19
For loop

Although all loop structures can be represented by while or Do...while, Java provides another kind of statement--for Loop, making some loop structures easier.
The number of times the For loop executes is determined before execution. The syntax format is as follows:
for (initialize; Boolean expression; update) {
Code statements
}
There are several explanations for the For loop:
The initialization step is performed first. You can declare a type, but you can initialize one or more loop control variables, or you can be an empty statement.
Then, the value of the Boolean expression is detected. If true, the loop body is executed. If False, the loop terminates, starting execution of the statement following the loop body.
After the loop is executed, the loop control variable is updated.
Re-detects the Boolean expression. The loop executes the above procedure.
Instance

public class Test {

public static void Main (String args[]) {

for (int x = ten; x < x = x+1) {System.out.print ("Value of x:" + x); System.out.print ("\ n"); } } }
The results of the above example compilation run as follows:
Value of X:10
Value of X:11
Value of X:12
Value of X:13
Value of X:14
Value of X:15
Value of X:16
Value of X:17
Value of X:18
Value of x:19
Java Enhanced for Loop

JAVA5 introduces an enhanced for loop that is primarily used for arrays.
The Java enhanced for Loop syntax format is as follows:
For (Declaration statement: expression)
{
Code sentences
}
Declaration statement: Declares a new local variable that must have a type that matches the type of the array element. Its scope is scoped to the Loop statement block, whose value is equal to the value of the array element at this time.
Expression: The expression is the name of the array to access, or is the method that returns the value to the group.
Instance

public class Test {

public static void Main (String args[]) {
int [] numbers = {10, 20, 30, 40, 50};

for (int x:numbers) {
System.out.print (x);
System.out.print (",");
}
System.out.print ("\ n");
String [] Names ={"James", "Larry", "Tom", "Lacy"};
for (String name:names) {
System.out.print (name);
System.out.print (",");
}
}
}
The results of the above example compilation run as follows:
10,20,30,40,50,
James,larry,tom,lacy,
The Break keyword

Break is used primarily in loop statements or switch statements to jump out of the entire block of statements.
Break jumps out of the innermost loop, and continues execution of the statement below the loop.
Grammar

The use of break is simple, which is a statement in the loop structure:
Break
Instance

public class Test {

public static void Main (String args[]) {
int [] numbers = {10, 20, 30, 40, 50};

for (int x:numbers) {
if (x = = 30) {
Break
}
System.out.print (x);
System.out.print ("\ n");
}
}
}
The results of the above example compilation run as follows:
10
20
Continue keywords

The Continue is suitable for any loop control structure. The function is to let the program jump immediately to the next iteration of the loop.
In the For loop, the continue statement causes the program to jump immediately to the UPDATE statement.
In the while or Do...while loop, the program immediately jumps to the judgment statement of the Boolean expression.
Grammar

Continue is a simple statement in the loop body:
Continue
Instance

public class Test {

public static void Main (String args[]) {
int [] numbers = {10, 20, 30, 40, 50};

for (int x:numbers) {
if (x = = 30) {
Continue
}
System.out.print (x);
System.out.print ("\ n");
}
}
}
The results of the above example compilation run as follows:
10
20
40
50

Java Learning Summary

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.