Java Basic Grammar Learning notes _java

Source: Internet
Author: User
Tags arithmetic arithmetic operators case statement numeric logical operators java keywords

0, Java Keywords
declarations for classes and interfaces: Class, extends, implements, interface
Package Introduction and Package Declaration: import, Package
Data type: Boolean, Byte, char, short, int, long, float, double
Optional values for some data types: false, True, NULL
Process Control: Default, Return, if, else, for, switch, case, does, while, break, continue,
Exception handling: Try, catch, finally, throw, throws
Modifiers: Abstract, final, native, private, protected, public, static, static, synchronized, transient, volatile
Operator: instanceof
Create object: New
References: this, super
Method return type: void
java reserved words: const and GOTO

One, Java comments: There are three kinds of forms
1. Single-line Comment
2./* One or more lines of comment * *
3./** Document Comment * * It must be placed outside the method body and can be used to generate HTML documents using the command Javadoc.

two, the ";", "{}", " 
    java Statements in Java code are separated by semicolons
    The Java code block is contained within curly braces
    ignores spaces.
   
Three, identifiers: used to name classes, methods and variables, and packages
identifier naming rules:
    1. With characters, "_" or "$ Beginning Can only be letters, numbers, "_" and "$"
    2. Case sensitive.
    3. No length limit.
    4. Class/Interface name first uppercase;
    5. Method name, variable name first letter lowercase, remaining first letter uppercase;
    6. The constant name is all uppercase;
    7. Package name all lowercase.
   
Four, basic data types:

4 bytes

TD width= "Si" >
name

specifier Span lang= "en-US"

Save Storage requirements

range or precision

description

byte type

/td>
byte

1 byte

-128 to 127

< Span lang= "en-US" > 

short integer

/td>
short

2 byte

-2^15 to 2^15-1

< Span lang= "en-US" > 

integer

int

4 byte

-2^31 to 2^31-1

< Span lang= "en-US" > 

Long integer

/td>
long

8 byte

-2^63 to 2^63-1

< span> Long integer number has a suffix l

single-precision floating-point type p> float

Valid decimal places 6~7 bit

indicates float type data needs to be appended with a suffix Span lang= "en-us" >f, does not have a suffix f floating-point data is considered double type of

double-precision floating-point type p>

double

8 bytes

Valid decimal places 15 bit )

 

character

/td>
char

2 byte

  span>

to store unicode The characters in the encoded table.

char is unsigned 16 bit integer literal values must be enclosed in single quotes note single Chinese characters enclosed in single quotes are also correct

Boolean type

Boolean

Not sure

only true and false Two values , it can't be converted to integers .




Note: In Java, the size of the memory space for all numeric types in memory is not platform-independent, and there is no unsigned type in java.

The scope and lifetime of variables:
1. A variable is the base unit for storing data.
2. Variables can be declared anywhere in a block of code
3. Block starts with opening curly braces and ends with closing braces
4. The scope of the variable is the block in which it resides
5. Member variable: Declared in the class, its scope is the entire class. (can be defined anywhere other than methods in the class)
6. Local variables: Internal declarations within a method's internal or a code block of a method. If declared within a method, its scope is the entire method, and if declared inside a code block of a method, its scope is this block of code. (Local variables must be defined before use)
7. Method parameter: The normal method or the construction method parameter, its scope is the whole method not.
8. Exception Handling parameter: its scope is the code block immediately following the catch (Exception e) statement.

Vi. Data Conversion Type:
1. Automatic type conversion: Automatic type conversion occurs when two types of variables are assigned to a variable of another type, and when the target type is greater than the source type. The following figure shows a legal conversion between numeric types: (The solid arrow is a conversion without information loss, and a virtual arrow indicates a possible loss of precision):

2. Force type conversions: casts are used for explicit type conversions. If the data type of the converted value is greater than its target type, some information is lost
A type cast causes a program to treat a variable as a type, although this variable contains another type of data.
Syntax: (target type) The name of the variable to be converted;

Example:
float C = 34.56789f;
int b = (int) c; Convert C to integral type

Seven, operators:
1.

Operator

Describe

Example

Arithmetic operators

Arithmetic operators use numeric operands. These operators are used primarily for mathematical calculations

+, -, *, /, %

relational operator

The

relational operator is used to test the relationship between two operands. The result of the expression using the relational operator is type

==, ">=, <=, /span>

logical operator p>

logical operators for Boolean operands

&, |, ^, &&, | |,!

conditional operator The p>

conditional operator is unique because it is a ternary operator that makes up an expression with three operands. It can replace some type of If-else statement

:

Assignment operator

the assignment operator is an equal sign = , which assigns a value to a variable

=, *=, /=, +=, -=




2. Priority level:


Order

Operator

1.

brackets, such as () and []

2.

A unary operator, such as - , ++ , - - and !

3.

arithmetic operators, such as * , / , % , + and -

4.

relational operators, such as > , >= , < , <= , == and !=

5.

logical operators, such as & , ^ , | , && , ||

6.

conditional operators and assignment operators, such as ? :, = , *= , /= , += and -=




In general, it is not intentional to remember that the sequence of operators may be specified using parentheses when the order of execution of the operator cannot be determined.

Eight, control flow statements:
1. Judgment statement:
(1). If-else statement:
General Syntax:

  if (< condition >) { 
    < statement block 1> 
  } else { 
   < statement block 2> 
  } 

If the condition is true, execute the statement in Block 1 of the statement;
If the condition is false, the statement following else is executed (the statement in Block 2 of the statement).
(2). Switch-case statement:
General Syntax:

  switch (expression) {case  1: 
     The statement that operates 1; 
     break; 
    Case 2: 
     Statement of Operation 2; 
     break; 
    ... case 
    N: 
     operation N of the statement; 
     break; 
    Defaults: 
     default statement; 

Note: The value type of the expression in switch brackets must be a basic type compatible with type int (including Byte, short, char, int), and a break at the end of each case clause;

2. Circular statement:
(1). While loop: Executes the loop body whenever the specified condition is true.  If the condition is false at the outset, the while loop is never executed. The syntax is as follows:

  while (condition) { 
    //Loop body statement 
  }

(2). Do-while Loop: Execute the loop body first before testing the condition. The syntax is as follows:

  do{ 
    //Loop body statement 
  } while (condition);

(3). For loop: Mainly used to execute statements or block of statements at a predetermined number of times: The syntax is as follows:

  for (initialize; test; update counter) { 
    //action statement; 
  }

3. Jump statements:
(1). Break Interrupt Loop
(2). Countinue only interrupts this cycle
(3). Return exit this method, skip to the top call method. If the return type of this method is not void, you need to provide the corresponding return value.

IX. Arrays: A data structure that stores a set of data of the same type.
1. You can declare an array in the following three ways:
(1). data type [] identifier; Declaring an array
(2). data type [] identifier = new data type [size]; Create an array
(3). data type [] identifier = {value 1, value 2, ...    Value N}; declaration, creating and initializing
2. The first value of an array can be accessed by an integer subscript. For example: A is an array of integers, then a[0] is the first element in the array.
3. Once the array is created, it cannot change its size anymore.
4. You can use the array name. length to get the number of elements in the array.
5. Multidimensional Arrays:
Two-dimensional array: is actually an array of arrays.
such as: int [] [] arr = new INT[2][3]; In this program fragment, a two-dimensional array object with 2 rows of 3 columns is configured. Its configuration relationship can be expressed as shown in the figure:
So, based on the above principles, you can create an irregular array.

X. Command line parameters:
the entry method in the Java program: The Main method has a string[] args parameter, which means that the main method receives an array of characters, which is the command-line argument.

Xi. Escape characters:

Escape sequence

Name

Describe

/A

Warning

produce a warning.

/n

Line Wrap

Moves the cursor to the first grid on the next line.

/R

Enter

Moves the cursor to the first grid of the current line.

/t

Horizontal tabulation

Moves the cursor to the next horizontal tab position.

/'

Single quotation mark

Produces a single quotation mark.

/"

Double quotes

Produces a double quote.

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.