Dark Horse programmer _ JavaSE learning summary 02nd _ basic Java syntax, _ Java

Source: Internet
Author: User

Dark Horse programmer _ JavaSE learning summary 02nd _ basic Java syntax, _ Java

------- Android training, java training, and hope to communicate with you! ----------

02.01 keyword overview and usage

Keyword Overview: words that are given specific meanings by the Java language

Keyword features: All letters that make up a keyword are in lowercase.

Key word note: goto and const exist as reserved words and are not currently used. Advanced Notepad similar to Notepad ++ has special color tags for keywords, which are very intuitive.

Reserved Words: may be promoted to keywords in the new JDK version.

02.02 identifiers overview and composition rules

Identifier Overview: it refers to the character sequence used to name a class, interface, method, variable, etc.

Composition rules: uppercase/lowercase letters, numbers, $, and _

Note:

1. Cannot start with a number

2. It cannot be a keyword in Java.

3. Case Sensitive

02.03 common naming rules in identifiers

Requirements for common naming rules: see naming conventions

Packages: all lower-case packages, separated.

Class or interface: the first letter of a word is capitalized, and the first letter of a word is capitalized when multiple words are used.

Methods and variables: lowercase letters for a word, and uppercase letters for each letter starting from the second word

Constant: all uppercase letters. Multiple words are separated _.

02.04 comments overview and Classification

Description Description: text used to explain the description Program

Annotation classification format in Java

Single line comment: Format: // comment text

Multi-line comment: Format:/* Comment text */

Document Note: Format:/** comment text */

1. for single-line and multi-line comments, the comments will not be interpreted and executed by JVM (Java Virtual Machine.

2. For document comments, they are java-specific comments. The comments can be parsed by javadoc, the JDK tool, and a set of instructions for the program is generated in the form of a webpage file.

02.05 Add the HelloWorld case to the comments version

Example:

1/* 2 requirement: write a program and output HelloWorld 3 analysis on the console: 4 1. Write a java program and define the class first. 5 2. To be able to be called by jvm, the main method must be defined. 6 3. The output statement must be used for the program to have output results. 7 8 Implementation: 9 1. the class keyword is used to define the class, followed by the class name 10 2. basic Format of main method: public static void main (String [] args) 11 3. basic output statement format: System. out. println ("helloworld"); 12 13 */14 // helloworld case 15 class Demo16 {17 // main method 18 public static void main (String [] args) 19 {20 // output statement 21 System. out. println ("helloworld"); 22} 23}

02.06 comments

1. Explain the program and improve the readability of the program.

2. It can help us debug the program

02.07 overview and use of constants

Constant Overview: the value of a program cannot be changed during execution.

Constant classification in Java: constants and custom Constants

Literal Value constant:

1. String constant: Content enclosed in double quotation marks: "helloworld"

2. integer constant: All integers, for example, 12, 23

3. Decimal constant: All decimal places: 12.34, 56.78

4. character constants: Content enclosed in single quotes (for example, 'A', 'A', and '0'

5. boolean constant: only true and false values are supported.

6. null constant: null

02.08 hexadecimal overview and binary, octal, and hexadecimal

Java provides four forms for Integer constants: binary, octal, decimal, and hexadecimal.

Hexadecimal Overview: hexadecimal is a carry-in method defined by people.

For any kind of hexadecimal-X hexadecimal, it indicates that the number operation at a certain position is regarded as one X.

Binary is the result of two-to-one Operations, octal is the result of eight-to-one Operations, decimal is the result of ten-to-one operations, and hexadecimal is the result of sixteen-to-one operations.

Conversion:

1 byte (bytes) = 8 bit (Binary)

1 K = 1024 byte

Regular. The larger the hexadecimal value, the shorter the representation.

02.09 representation of different hexadecimal data

Binary: 0, 1, full 2 into 1, starting with 0 B

Octal: 0 ~ 7, full 8 into 1, starting with 0

Decimal: 0 ~ 9. If the value is 10 to 1, the integer is in decimal format by default.

Hexadecimal: 0 ~ 9. ~ F, full 16 to 1, starting with 0x

Any data exists in binary form in a computer.

02.10 convert any hexadecimal to decimal

Binary: 110 to decimal: 1*22 + 1*21 + 1*20 = 6

Octal: 110 to decimal: 1*82 + 1*81 + 1*80 = 72

Hexadecimal: 110 to decimal: 1*162 + 1*161 + 1*160 = 272

02.11 any decimal to decimal exercise

Convert 0b10101 to decimal: 1*24 + 0*23 + 1*22 + 0*21 + 1*20 = 21

Convert 0123 to decimal: 1*82 + 2*81 + 3*80 = 83

Convert 0x3C to decimal: 3*161 + 12*160 = 60

Conversion from decimal to any Italian hexadecimal value

Convert decimal: 56 to binary:

56/2 = 28... 0

28/2 = 14... 0

14/2 = 7 .. 0

7/2 = 3... 1

3/2 = 1... 1

1/2 = 0... 1

Combine the remainder from bottom to top, that is, 0b 111000

Convert decimal: 56 to octal:

56/8 = 7 .. 0

7/8 = 0... 7

Combine the remainder from bottom to top, that is, 0 70

Convert decimal: 56 to hexadecimal:

56/16 = 3... 8

3/16 = 0... 3

Combine the remainder from bottom to top, that is, 0x38

02.13 exercise in any decimal notation

Convert the decimal 52 into binary, octal, and hexadecimal

Convert decimal: 52 to binary: 0b 110100

52/2 = 26... 0

26/2 = 13... 0

13/2 = 6... 1

6/2 = 3... 0

3/2 = 1... 1

1/2 = 0... 1

 

Convert decimal: 52 to octal: 0 64

52/8 = 6... 4

6/8 = 0... 6

 

Convert decimal: 52 to hexadecimal: 0x34

52/16 = 3... 4

3/16 = 0... 3

02.14 fast hexadecimal conversion method

Fast conversion of decimal and binary data

8421 yards:

1 1 1 1 1 1 1 1

128 64 32 16 8 4 2 1

Example:

Convert 100 to binary: 0b 01100100

Convert 101101 to decimal: 32 + 8 + 4 + 1 = 45

Binary and octal, hexadecimal conversion

1. Use decimal as the bridge

2. Binary to octal ratio: Binary to octal ratio from low to high each three groups, less than high to zero

Example: 100100 = (100) (100) = 0 44

3. Convert binary to hexadecimal: Convert binary to hexadecimal from low to high. Each group contains four digits.

Example: 100100 = (0010) (0100) = 0x24

02.15 back-Code complement

Symbol data Notation: in a computer, there are three types of notation for the number of symbols: source code, reverse code, and complement code.

All data operations are performed using a complement code.

Original code: it is a binary fixed-point representation, that is, the highest bit is the symbol bit, "0" indicates positive, "1" indicates negative, and the remaining bit indicates the value size.

Anticode: the anticode of a positive number is the same as the original code. The anticode of a negative number is the inverse of its original code by bit, except for the symbol bit.

Positive Value: the positive value is the same as the original value. A negative value is added with 1 at the end of the reverse value.

Example:

7 source code: 0 0000111

Anti-code 7: 0 0000111

Complement 7: 0 0000111

 

-7 source code: 1 0000111

-7 anti-code: 1 1111000 (based on the original code, except for the bitwise anti-code)

-Complement 7: 1 1111001 (add 1 to the last side of the anti-code)

02.16 original code anti-code makeup exercise

The original code of a specified number is 10110100, And the complement and reverse code are required.

Original code: 1 0110100

Reverse code: 1 1001011

Complement: 1 1001100

The complement code of a certain number is 11101110, and the original code is required.

Complement: 1 1101110

Reverse code: 1 1101101 (the last digit of the complement code minus 1)

Original code: 1 0010010 (bitwise reverse)

02.17 overview and format of Variables

Variable Overview: Amount of variable values that can be changed within a certain range during Program Execution

Variable Definition Format: data type variable name = initialization value;

02.18 overview and classification of data types

Java is a strongly typed language. Each data type is defined with a specific data type, and memory space of different sizes is allocated in the memory.

 

02.19 define variables of different data types

Example:

1 // define a byte variable 2 byte B = 10; 3 4 // define a short integer variable 5 short s = 100; 6 7 // define an integer variable 8 int I = 1000; 9 10 // define a long integer variable with the suffix l or L labeled 11 long l = 1_1_1_l; 12 13 // define a floating-point variable. The single-precision floating-point suffix is added with f or F to mark 14 float f = 12.34F; 15 16 // define the character variable 17 char ch = 'a '; 18 19 // define the boolean variable 20 boolean flag = true;

02.20 precautions for using variables

Notes for using variables:

Scope: The variable defines the level of braces, and the range of which braces is the scope of the variable. Two variables with the same name cannot be defined in the same scope.

Initialization value: No initialization value cannot be used directly

We recommend that you define only one variable for one row. You can define multiple variables, but we do not recommend that you

02.21 default conversion for data type conversion

The boolean type cannot be converted to another data type.

Default conversion: byte, short, char → int → long → float → double

Byte, short, and char are not converted to each other. They are involved in the operation and are first converted to the int type.

02.22 interpretation of variable types involved in Calculation

Example:

1 byte a = 3; 2 int B = 4; 3 byte c = a + B; // error 4 int d = a + B; // correct

Explanation: byte data is upgraded to the int type by default. The result of a + B is int type and cannot be assigned to byte type c.

02.23 forced conversion of data types

Forced conversion

Target type variable name = (target type) (converted data );

Example:

1 byte a = 3;2 int b = 4;3 byte c = (byte)(a + b);

The previous statement indicates that the first three bytes of the result int of a + B are discarded, and the last byte is reserved and assigned to c.

02.24 questions about forced conversion

1. Is there any problem with the following assignment?

Double d = 12.345;

Float f = d;

A: You cannot assign d of the double type to f of the float type.

Mandatory conversion: float f = (float) d;

2. Are there any differences in the following definitions?

Float f1 = (float) 12.345;

Float f2 = 12.345F;

A: There is a difference. f1 is strongly converted to float type through double type. F2 is a float type.

02.25 differences between variable addition and constant addition

Byte b1 = 3, b2 = 4, B;

B = b1 + b2; // The compilation fails. For the variable, the type is upgraded first. The b1 + b2 result is int type.

B = 3 + 4; // correct, constant. Calculate the result first to check whether it is within the byte range. No error is reported.

02.26 result calculation after forced conversion data Overflow

Byte B = 130; is there any problem? What should I do if I want to make the assignment correct? What is the result?

Answer: If the value of 130 exceeds the byte range, you can use the forced conversion of byte B = (byte) 130;

Result:

Int 130 source code: 00000000 00000000 00000000 10000010

Int 130 complement: 00000000 00000000 00000000 10000010

After forced conversion, the result of dropping the third-digit is 10000010.

Complement: 1 0000010

Reverse code: 1 0000001

Original code: 1 1111110 → the result is decimal-126

02.27-character data involved in Calculation

System. out. println ('A'); //

System. out. println ('A' + 1); // 98. When character a is used for computation, the 97

02.28 string involved in Computation

System. out. println ('A' + 1 + "hello"); // 98 hello

System. out. println ("hello" + 'A' + 1); // helloa1

System. out. println ("5 + 5 =" + 5 + 5); // 5 + 5 = 55

System. out. println (5 + 5 + "= 5 + 5"); // 10 = 5 + 5

Related Article

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.