Java Syntax 1

Source: Internet
Author: User
Tags binary to decimal decimal to binary integer division modulus


1. Constant -- constant indicates a value that cannot be changed
Classification of constants in Java:
1, an integer constant. All integers
2. Decimal constant. All decimal places
3. boolean constants. It is special and has only two values. True False.
4, character constant. Use single quotation marks ('') To identify a number, letter, or symbol.
5. A String constant. Mark one or more characters with double quotation marks.
6. null constant. Only one value is null.
For integers: Java has three forms.

  • Decimal: 0-9, full 10 into 1.
  • Octal: 0-7, full 8 into 1. Start with 0.
  • Hexadecimal: 0-9, A-F, full 16 into 1. It is expressed with the beginning of 0x.

For integers: Java has three forms.

  • Decimal: 0-9, full 10 into 1.
  • Octal: 0-7, full 8 into 1. Start with 0.
  • Hexadecimal: 0-9, A-F, full 16 into 1. It is expressed with the beginning of 0x.

Basic hexadecimal conversion
Decimal Binary Conversion

Convert decimal to binary:

Divide by 2 and obtain the remainder. For example, the binary value of 6/2 quotient 3 remainder 0 3/2 quotient 1 remainder 1 1/2 quotient 0 remainder 1 6 is 110.

Convert binary to decimal
Multiply by the power of 2, such as 1001 1*2 ^ 3 + 2*2 ^ 0 = 9
Decimal octal Conversion
Hexadecimal conversion
Negative binary representation (reverse code)
Corresponding positive number binary decimal 1
For example,-6
6 00000000 00000000 00000000 00000110
Decimal 11111111 11111111 11111111 11111001
+ 1 1
--------------------------------------------------------------------------------
11111111 11111111 11111111 11111010
Decimal-"hexadecimal method: The method first converts decimal to binary and then converts binary to (four digits represent one digit) hexadecimal
For example, 90 ----> 0101 1010 ----> 0x5a
2. Variables
Variable concept:

  • A storage area in the memory
  • This region has its own name (variable name) and type (data type)
  • Data in this region can change continuously within the same type.

Why do we need to define variables:
It is used to store constants of the same type and can be reused.
Note the following when using variables:

  • Scope of the variable (valid)
  • Initialization value

Define the variable format: data type variable name
 
= Initialization value;

Note: The format is fixed. Remember the format to be retained. Understanding: variables are like unknowns in mathematics.

Data Type

Basic Data Type
Numeric type
Char)
Boolean)
Integer type (byte, short, Int, long)
Floating point type (float, double)
Reference data type
Class)
Interface)
Array ([])
Integer default: int decimal default: double
Java is a strongly typed language. A specific data type is defined for each type of data, and different memory sizes are allocated in total.
3. type conversion
Automatic type conversion (also called implicit type conversion)
Forced type conversion (also called explicit type conversion)
Principle of type conversion
When should I use forced type conversion?
The expression data type is automatically upgraded.

  • All byte, short, and char values are upgraded to int.
  • If an operand is of the long type, the calculation result is of the long type;
  • If an operand is of the float type, the calculation result is of the float type;
  • If an operand is of the double type, the calculation result is of the double type.

Analysis:
The difference between system. Out. println ('A') and system. Out. println ('A' + 1.

System. Out. println ('A'); // output

System. Out. println ('A' + 1); // type is increased by 97 + 1 = 98
Automatic type upgrade
Byte B = 3;
Int x = 4;
X = x + B; // B is automatically upgraded to the int type for calculation.
Forced type conversion
Byte B = 3;
B = B + 4; // Error
B = (byte) B + 4; // force type conversion, forcibly convert the result of B + 4 to the byte type, and then assign
Value: B.
Thoughts:
Byte b1 = 3, b2 = 4, B;
B = b1 + B2;
B = 3 + 4;
Which of the following statements fails to be compiled? Why?
Short S = 3;
S = S + 2; // If the compilation fails, the result of S + 2 is an integer and cannot be received by short.
S + = 2 // compiled, + = after the number operation on both sides, the two sides will automatically increase the type
Attention to arithmetic operators: If you modulo a negative number, you can ignore the negative modulus, for example, 5%-2 = 1. However, if the modulus is negative, this is another case.
For the division number "/", its integer division is different from the fractional Division: When division is performed between integers, only the integer part is retained and the fractional part is discarded.

For example, int x = 3510; X = x/1000*1000; X returns 0.

In addition to the string addition function, "+" can also convert non-string to string,

For example: system. Out. println ("5 + 5 =" + 5 + 5); // What is the result?
System. Out. println ("5 + 5 =" + 5 + 5); // output: 5 + 5 = 55
Value assignment operator
Symbol:
=, + =,-=, * =,/=, % =
Example:
Int A, B, C; a = B = c = 3;
Int A = 3; A + = 5; equivalent operation a = a + 5;
Comparison Operators
4> = 3 true (true if one of them is greater than or equal to true)
NOTE 1: The comparison operator results in the boolean type, that is, either true or false.
Note 2: The comparison operator "=" cannot be mistakenly written as "= ".
Logical operators
Logical operators are used to connect boolean expressions. They are not allowed in Java.
Write it as 3 <x <6, and write it as x> 3 & x <6.
The difference between "&" and:

  • Perform operations on the left, whether true or false, and on the right of a ticket;
  • If the left side is true, the right side is involved in the operation. If the left side is false, the right side is not involved in the operation.

The difference between "|" and "|" is the same. In double or mode, the left side is true, and the right side is not involved in calculation.
The difference between XOR (^) and or (|) is that when both the left and right values are true, the result is false.
Bitwise operators

  • <Empty position 0: The removed high position is discarded, and the empty position is 0.
  • > The highest bitwise of the binary to be shifted is 0. After the right shift, the vacant bitwise is 0, the highest bitwise is 1, and the vacant bitwise is 1.
  • >>> Whether the highest bit of the binary to be shifted is 0 or 1, the vacant bit is filled with 0.
  • & Perform & operation on Binary bits. If the value is 1 & 1, the result is 1. Otherwise, the result is 0;
  • | Binary bit | operation, only 0 | 0 indicates that the result is 0; otherwise, the result is 1;
  • ^ Perform the ^ operation on any of the same binary bits. The result is 0. 1 ^ 1 = 0, 0 ^ 0 = 0 is not the same binary bits. ^ The result is 1. 1 ^ 0 = 1, 0 ^ 1 = 1
  • <Empty position 0: The removed high position is discarded, and the empty position is 0.
  • > The highest bitwise of the binary to be shifted is 0. After the right shift, the vacant bitwise is 0, the highest bitwise is 1, and the vacant bitwise is 1.
  • >>> Whether the highest bit of the binary to be shifted is 0 or 1, the vacant bit is filled with 0.
  • & Perform & operation on Binary bits. If the value is 1 & 1, the result is 1. Otherwise, the result is 0;
  • | Binary bit | operation, only 0 | 0 indicates that the result is 0; otherwise, the result is 1;
  • ^ Perform the ^ operation on any of the same binary bits. The result is 0. 1 ^ 1 = 0, 0 ^ 0 = 0 is not the same binary bits. ^ The result is 1. 1 ^ 0 = 1, 0 ^ 1 = 1
  • Left shift: <move the high position to the left, and set the low position to 0. It is actually the power of the number of digits to be moved multiplied by the number of digits to be moved.
  • Right Shift:> move the low position to the right. If the number of moves in the high position is negative, use 1 to fill the positive number with 0, which is actually the power of the number of moves divided by 2.

2 <3 = 2*2 ^ 3 = 2*8 = 16 6> 2 = 6/2 ^ 2 = 6/4 = 1
The same number or the same number is equal to the original number.
For example, 7 ^ 4 ^ 4 = 7 (which can be used for encryption and decryption)
Do not set the third variable to swap two variables
N = n ^ m;
M = n ^ m; // n ^ m = (N ^ m) ^ m = N
N = n ^ m; // n ^ m = (N ^ m) ^ n = m
Ternary Operators
Format

  • (Conditional expression )? Expression 1: expression 2;
  • If the condition is true, expression 1 is returned;
  • If the condition is false, expression 2 is returned;

Example:
Returns the large number of two numbers.
Int x = 3, y = 4, Z;
Z = (x> Y )? X: Y; // The Z variable stores the large numbers of two numbers.
Public class test {
Public static void main (string [] ARGs)
{
Char x = 'y ';
Int I = 1;
System. Out. println (true? X: I );
System. Out. println (false? 1: X );
}
} Why is the output 89 and Y, not y and Y?
Analysis:
(1) If expression 1 and expression 2 share the same type, the type of the result of the entire conditional operator is the same.

(2) If the type of an expression is t, t is byte, short, or char, and the type of another expression is a constant expression of the int type,

In addition, the value of this constant expression can be expressed with type T (that is, the value of the constant expression is within the value range of type T ),

The result type of the entire conditional operator is T.

(3) In addition to the above cases, if expression 1 and expression 2 are of different types, the type is upgraded. the type of the result of the entire conditional operator is the upgraded type.

System. Out. println (true? X: 0); // expression 1 is a char type, expression 2 is a constant expression of the int type,

The value 0 of this constant expression can be represented by the char type. Therefore, the type of the result of the entire conditional operator should be char type, so the output result is 'A '.

System. Out. println (true? X: 1111111110 );. Expression 1 is a char type, expression 2 is a constant expression of int type,

However, the value of this constant expression has exceeded the value range of the char type. Therefore, we need to increase the type to int type,

The result type of the entire conditional operator is int, so the output result is 65 (ASCII code of Character ).

System. Out. println (false? I: X); // expression 1 is a variable of the int type, expression 2 is a variable of the char type,

There is a constant expression. Therefore, we need to increase the char type to the int type. The type of the result of the entire conditional operator is the int type. Therefore, the output result is 65.

Program Process Control
(1) Judgment statement if
1. If (conditional expression)
{
Execute the statement;
}
2. If (conditional expression)
{
Execute the statement;
}
Else
{
Execute the statement;
}
3. If (conditional expression)
{
Execute the statement;
}
Else if (conditional expression)
{
Execute the statement;
}
......
Else
{
Execute the statement;
}
If statement features:
A. Each format is a single statement.
B. The difference between the second format and the third-party OPERATOR: Operations of the third-party operator
A value is required. The advantage is that it can be written in other expressions.
C. No matter what the conditional expression looks like, the final structure is
Whether it is true or false;
(2) Select the statement Switch
Switch statement format:
Switch (expression)
{
Case value 1:
Execute the statement;
Break;
Case value 2:
Execute the statement;
Break;
......
Default:
Execute the statement;
Break;
}
Switch statement features:
A. The switch statement has only four types: byte, short, Int, and char.
B. There is no order between case and default. First, execute the first case. If no matching case exists, execute default.
C. When the switch statement is terminated, run the break until the switch statement ends.
D. If the matched case or default does not have a corresponding break, the program will continue to run the statements that can be executed until the break or switch ends.
************
The switch () Statement receives four types of parameters: byte short char int jdk5.0 support enumerative jdk7.0 support strings
Int c = 5;
Switch (c)
{
Default:
System. Out. println ("default ");
Break;
Case 1:
System. Out. println ("11111111 ");
Break;
Case 2:
System. Out. println ("2222222 ");
Break;
Case 3:
System. Out. println ("3333333 ");
Case 4:
System. Out. println ("4444444 ");
Break;
Case 5:
System. Out. println ("5555555 ");

} // The default will not be executed because the braces are displayed after case 5 is executed.
Int c = 6;
Switch (c)
{
Default:
System. Out. println ("default ");
Case 1:
System. Out. println ("11111111 ");
Case 2:
System. Out. println ("2222222 ");
Break;
Case 3:
System. Out. println ("3333333 ");
Case 4:
System. Out. println ("4444444 ");
Break;
Case 5:
System. Out. println ("5555555 ");
Break;
} // If the condition is not met, the system executes the default statement and stops the process only when case 1 case 2 encounters a break.
(3) Loop Structure
Statement: while, do while,
Do While is characterized by conditions whether or not they are met,
The loop body must be executed at least once.
Other process control statements:
Break (bounce ),
Continue (continue)
Break statement: Application Scope: select the structure and loop structure.
Continue statement: applies to the loop structure.
Note:
A. the two statements leave the application scope, and it makes no sense to exist.
B. The two statements are independent and cannot be executed.
C. The continue statement ends this loop and continues the next loop.
D. The appearance of the label allows the two statements to apply to the specified range.


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.