The contents of this chapter:
· Key words
· Identifier
· Comments
· Constants and variables
· Operator
· Statement
· Function
· Array
Key words:
· Keyword Overview
Words that are given a specific meaning by the Java language
· Keyword features
The letters that make up the keywords are all lowercase
· Keyword considerations
Goto and const exist as reserved words and are not currently used
Advanced Notepad like notepad++, with special color tags for keywords, very intuitive
Determine which of the following are keywords
Class,helloworld,public,static,void,main,string.system
Keywords to define the data type |
Class |
Interface |
Byte |
Short |
Int |
Long |
Float |
Double |
Char |
Boolean |
void |
|
|
|
|
Used to define a data type worth a keyword |
True |
False |
Null |
|
|
Keywords to define Process Control |
If |
Else |
Switch |
Case |
Default |
While |
Do |
For |
Break |
Continue |
Return |
|
|
|
|
Keywords used to define access rights modifiers |
Private |
Protected |
Public |
|
|
Keywords for defining classes, functions, variable modifiers |
Abstract |
Final |
Static |
Synchronized |
|
Keywords used to define the relationship between classes and classes |
Extends |
Implements |
|
|
|
Used to define instance and reference instances, and to determine the keywords |
New |
This |
Super |
instanceof |
|
Keywords for exception handling |
Try |
Catch |
Finally |
Throw |
Throws |
Keywords for the package |
Package |
Import |
|
|
|
Other modifier keywords |
Native |
Strictfp |
Transient |
Volatile |
Assert |
Identifier
· Identifiers Overview
Is the sequence of characters used to name classes, interfaces, variables, etc.
· Composition rules
English uppercase and lowercase letters
numeric characters
$ and _
· Precautions
Cannot start with a number
Cannot be a keyword in Java
Case sensitive
Attention:
1: From the name, no more casual, no rules, inadequate surrounding area
2: For precautions, examples show
3: Under those legal, those not valid:
helloworld,dataclass,_983, $bS 5_c7,class (x), dataclass# (x), 98.3 (x), Hello World (x)
Identifiers (common naming rules)
· Package (in fact, is the folder, to solve the same name problem)
Single and multi-level examples respectively
· Class or interface
An example of a word and multiple words respectively
· Methods and variables
An example of a word and multiple words respectively
· Constant
An example of a word and multiple words respectively
Comments:
Comments Overview
Text used to interpret the description program
Comment Classification format in Java
Single-line Comment
Format://Comment text
Multi-line comments
Format:/* Comment Text */
Document comments
Format:/** Note text */
Precautions:
A comment is a good programming habit that a programmer must have. Beginners can develop a habit of writing a program: Write a comment and then write the code.
Put your thoughts through the comments first, in the code to reflect. Because the code is just a form of reflection of thought.
Annotated version of HelloWorld:
Requirements: Write a program in the console output HelloWorld
Analysis:
Write a Java program that defines the class first.
To be able to be called by the JVM, a program must define the Main method.
In order for the program to have output, the output statement must be used.
Realize:
The class keyword is used for defining classes, followed by the class name
Main Method Basic Format
Output Statement Basic format
Simple features of annotations:
Explain the procedure, improve the reading of the program
can help us in the wrong way, we'll explain the more advanced troubleshooting
Constant:
Constants Overview
Its value cannot be changed during program execution
Constant classification in Java
Literal constants
Custom Constants (Object-oriented section)
The contents of a string constant enclosed in double quotation marks
integer constant all integers
12,23
Decimal constant All Decimals
12.34,56.78
The contents of a character constant enclosed in single quotation marks
' A ', ' a ', ' 0 '
Boolean constants are more specific, only true and false
Empty constant Null (part of the array explained)
Different binary:
Java provides 4 forms of expression for integer constants
Binary
Octal
Decimal
Hexadecimal
Overview of the binary system
In the system: is the carry system, is a kind of carry-out method stipulated by people. For any kind of binary--x, it means that the number operation at a certain position is every x in a bit. Binary is every two in one, eight into the system is every eight into a, decimal is every ten into one, Hex is 16 into one.
Different binary data composition
Binary
Made up of 0, 1. Start with 0b
Octal
by 0,1,... 7 composition. Start with 0
Decimal
by 0,1,... 9 composition. Integers are decimal by default
Hexadecimal
by 0,1,... 9,a,b,c,d,e,f (both uppercase and lowercase). Start with 0x
Binary conversion
Other binary to Decimal
Introduction of the conclusion by decimal
Convert the 0b100,0100,0x100 into decimal
Decimal to another binary
Introduction of the conclusion by decimal
The 52 is represented as binary, octal, hexadecimal
Fast conversion of decimal and binary binary
8421 yards
100 Turn into binary
101101 turn into decimal
Binary and octal, how hexadecimal is converted
Using decimal as a bridge
Binary to octal 3-bit combination
100110
Binary to hexadecimal 4-bit combination
100110
Symbolic data representation
Within the computer, there are 3 notation numbers: The original code, the inverse code, and the complement. All data is performed in the complement.
Original code
is the binary fixed-point notation, that is, the highest bit is the sign bit, "0" means positive, "1" is negative, and the remaining bits represent the size of the value.
Anti-code
The inverse code of a positive number is the same as its original code, and the inverse of a negative number is a bitwise negation of its original code, except for the sign bit.
Complement
The complement of a positive number is the same as its original code, and the complement of a negative number is the minus 1 of its inverse code.
Variable
Variable overview
The amount of the value that can change within a range during the execution of a program
Understanding: Like the unknown in mathematics
Variable definition Format
Variable name of data type = initialization value;
Note: The format is fixed, remember the format, status quo
Data type:
The Java language is a strongly typed language that defines specific data types for each type of data, and allocates a different size of memory space in memory
Considerations for Using Variables:
Scope
The variable is defined in the curly braces, and the range of the curly braces is the scope of the variable. Two variables of the same name cannot be defined in the same scope.
Initialize value
No initialization value can be used directly
It is recommended to define only one variable on a single line
Multiple can be defined, but not recommended
+ is an operator, we should be able to understand, do the addition of data.
The Boolean type cannot be converted to another data type
Default conversion
Byte,short,char-int-long-float-double
Byte,short,char to each other, they participate in the operation first converted to the INT type
Forced conversions
Target type variable name = (target type) (data to be converted);
1: Case demonstration implicit conversion, and drawing explanation
byte B = 10;
int i = 100;
System.out.println (B+i);
Have a problem
byte BB = b + i;
System.out.println (BB);
No problem
Int J = B + i;
System.out.println (j);
2: Just above the example can not, but, we clearly know that it is within this range, I would like to assign value, swollen?
With cast
Target data type variable name = (target data type) (converted data);
3: So, what kind of conversion do we use?
Generally, it is best not to arbitrarily use forced type conversions, which can easily result in loss of data accuracy.
(1) When a large data type is converted to a small capacity data type, it is necessary to add a forced conversion character, but it may result in reduced precision or overflow; use with caution.
(2) When there are many types of data blending operations, the system first automatically converts all data into the data type with the largest capacity before it is calculated.
Interview questions
BYTE B1=3,b2=4,b;
B=B1+B2;
b=3+4;
Which is the compilation failed? Why is it?
Study Questions
byte B = 130; Is there a problem? What can I do if I want to make the assignment right? What is the result?
Exercise: byte B = 300;
Please write down the following program results
System.out.println (' a ');
System.out.println (' a ' + 1);
System.out.println ("Hello" + ' a ' + 1);
System.out.println (' A ' +1+ "Hello");
System.out.println ("5+5=" +5+5);
System.out.println (5+5+ "=5+5");
Summarize:
1: Keywords (master)
(1) Words that are given a specific meaning by the Java language
(2) Features:
All lowercase.
(3) Precautions:
A:goto and const exist as reserved words.
B: High-level notebooks like notepad++ have special color tags on keywords
2: identifier (master)
(1) is a sequence of characters that give names to classes, interfaces, methods, variables, etc.
(2) Composition rules:
A: English uppercase and lowercase letters
B: Digital
C:$ and _
(3) Precautions:
A: Cannot start with a number
B: cannot be a keyword in Java
C: Case Sensitive
(4) Common naming conventions (see Name and meaning)
A: Package All lowercase
Single-level package: lowercase
Example: liuyi,com
Multi-level package: lowercase, and separated by.
Example: Cn.itcast,com.baidu
B: Class or interface
One word: Capitalize the first letter
Example: Student,demo
Multiple words: Capitalize the first letter of each word
Example: Helloworld,studentname
C: Method or Variable
One word: First letter lowercase
Example: Name,main
Multiple words: Start with the second word, capitalize the first letter of each word
Example: Studentage,showallnames ()
D: constant
All caps
One word: Uppercase
Example: PI
Multiple words: Uppercase and separated by _
Example: Student_max_age
3: note (master)
(1) The text that explains the program
(2) Classification:
A: Single-line comment//
B: Multiline Comment/**/
C: Documentation Comments (later)/** */
(3) wrote a annotated version of the HelloWorld case.
We're going to write a procedure in the back.
Demand:
Analysis:
Realize:
The code reflects:
(4) The role of annotations
A: Explain the procedure, improve the reading of the code.
B: Can help us debug the program.
Later, we will explain a more advanced debugging tool
4: Constant (Master)
(1) The amount of the value does not change during program execution
(2) Classification:
A: Literal constants
B: Custom Constants (later)
(3) Literal constants
A: string constant "Hello"
B: integer constant 12,23
C: decimal constant 12.345
D: Character constant ' a ', ' a ', ' 0 '
E: Boolean constant True,false
F: Empty constant Null (said later)
(4) provides four representations of integer constants in Java
A: Binary consists of 0, 1. Start with 0b.
B: Octal system by 0, 1, ... 7 composition. Start with 0.
C: Decimal by 0, 1, ... 9 composition. Integers are decimal by default.
D: Hex by 0, 1, ... 9,a,b,c,d,e,f (both uppercase and lowercase) are composed. Start with 0x.
5: Binary conversion (Learn)
(1) Other binary to decimal
Coefficient: is the value on each bit
Cardinality: X-binary cardinality is X
Right: The data on each digit, from the right, and numbering starting from 0, the corresponding number is the right to the data.
Results: The sum of the coefficients * cardinality ^ power power.
(2) Decimal to other binary
In addition to the base fetch, the remainder is reversed until the quotient is 0.
(3) Fast conversion method of the binary conversion
A: Conversion between decimal and binary
8421 yards.
B: Binary to octal, hexadecimal conversion
6: Variable (master)
(1) The amount of the value that can change within a certain range during the execution of a program
(2) Define the format of the variable:
A: Data type variable name = initialization value;
B: Data type variable name;
Variable name = initialization value;
7: Data type (master)
(1) Java is a strongly typed language that provides the corresponding data types for each type of data.
(2) Classification:
A: Basic Data type: 4 classes of 8
B: Reference data type: Class, interface, array.
(3) Basic data types
A: integer consumption of bytes
BYTE 1
Short 2
int 4
Long 8
B: Floating point
Float 4
Double 8
C: Character
Char 2
D: Boolean
Boolean 1
Attention:
integers are type int by default, and floating-point numbers are double by default.
Long integers to be added L or L.
Single-precision floating-point numbers are added F or f.
8: Data type conversion (mastering)
(1) Boolean type does not participate in conversions
(2) Default conversion
A: From small to large
B:byte,short,char--INT--long--float--double
C:byte,short,char do not convert each other, directly into the int type participates in the operation.
(3) Forced conversion
A: from big to small
B: There may be a loss of precision, generally not recommended for such use.
C: Format:
Target data type variable name = (target data type) (converted data);
(4) Study Questions and interview questions:
A: Are there any differences in the following two ways?
float f1 = 12.345f;
float F2 = (float) 12.345;
B: Is there a problem with the program below, and if so, where?
BYTE B1 = 3;
byte b2 = 4;
BYTE B3 = b1 + b2;
byte B4 = 3 + 4;
C: What is the result of the following operation?
byte B = (byte) 130;
D: Character participation operation
is to find the value inside the ASCII
' A ' 97
' A ' 65
' 0 ' 48
System.out.println (' a ');
System.out.println (' a ' + 1);
E: String participation operation
This is actually a string connection.
System.out.println ("Hello" + ' a ' + 1);
System.out.println (' A ' +1+ "Hello");
System.out.println ("5+5=" +5+5);
System.out.println (5+5+ "=5+5");
Appendix:
characters commonly used characters and ASCII code Comparison table
2-1 Java language Basics