This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging
Java Learning Notes series — Sun Bujian 1208
【 Detail + super basic 】Java- Learning Notes 01
【 Detail + super basic 】Java- Study Notes 02
【 Detail + super basic 】Java- Study Notes 03
Ongoing updates….
2. Java Language foundation
Overview of identifiers
Identifiers in Java are sequences of characters used to mark the names of packages, classes, interfaces, objects, methods, variables, and so on.
Identifiers start with a letter, underscore (__), or dollar sign and can be followed by any letter, number, underscore, or dollar sign.
Identifier Attention
Identifiers in Java cannot start with a number, are case-sensitive, and cannot be the same as keywords.
Naming rules
1. Name the class name and interface name: Capitalize the first letter of the word and lower case the other letters.
2, variables, methods: the first word in all lowercase, starting from the second word, the first letter of the word in upper case, the rest of the lowercase.
3. Naming constants: Capitalize every word and connect words with “__”.
4. Package name naming: All letters of the package name are lowercase.
The keyword
Keywords in Java have a specific meaning, are reserved for the compiler, cannot be used as identifiers, and all keywords are lowercase.
Keyword for data processing: byte short int long float double char Boolean
Keywords for flow control statements: if else switch case default while for break continue
Private public protected final static abstract synchronized
Exception handling keyword: try catch finally throw throws
New extends implements class this super instance of new extends implements class this super instance of
Method related keyword: return void
Package-related keywords: package import
Other keywords: true false null
Delimiters and comments
Delimiter:; {} ().// Single-line comments
/* Multi-line comments */
/** Document comments */
Copy the code
variable
Variables as the most basic storage unit in the program, the elements include: variable type, variable name, scope
Declare variables:
Local variable
A variable defined within a method or statement must be declared before it is assigned.
Member variable
For variables defined outside a method or inside a class, member variables are automatically initialized
Static variable
Use the static definition to belong to the class
Naming conventions for variables and constants
- All variable, method, class names: see name for meaning
- Class member variables: lowercase first letter and hump principle: monthSalary
- Local variables: capital letters and the hump principle
- Constants: uppercase letters and underscores: MAX_VALUE
- Class name: Uppercase first letter, hump principle
- Method names: lowercase and hump principles: run(),runRun()
Data type:
Data types in Java fall into two categories: basic data types and reference data types
Byte: 1 byte short: 2 bytes
Int: 4 bytes long: 8 bytes
Float: 4 bytes double: 8 bytes
Char: two bytes Boolean: 1 bit
Reference data type: 4 bytes, used to represent the address of the object
Integer variables/constants
It is a decimal integer, such as 99, -500, and 0
An octal integer that must start with a 0, such as 015
The hexadecimal number must start with 0x or 0x, for example, 0x15
A binary number must start with 0b or 0B. For example, 0H01110011
Floating point variables/constants
It is in decimal notation, for example, 3.14 314.0 0.314
Scientific notation, such as 314e2 314E2 314E-2 (e2 = 10^2 E-2 = 10^(-2))
Note: Floating-point variables are inexact and cannot be used for comparison. To do so, use the useful classes BigInteger and BigDecimal in the Java.math package
Character variables/constants
2 bytes in memory, using Unicode encoding table
Unicode has encodecs ranging from 0 to 65535, and they are usually represented as hexadecimal values from ‘\u000’ to ‘\uFFF’ (the prefix U for Unicode).
Escape character
Escape character | meaning | Unicode value |
---|---|---|
\b | Backspace | \u0008 |
\n | A newline | \u000a |
\r | enter | \u000d |
\t | TAB character | \u0009 |
\” | Double quotation marks | \u0022 |
\ ‘ | Single quotes | \u0027 |
\ | The backslash | \u005c |
Boolean type variable
You cannot use 0 or non-0 integers to replace true and false in memory (not a byte), unlike in C. The Boolean type is used to determine logical conditions and is generally used for program flow control
Conversion between different types of data
1. Automatic type conversion
Automatic conversions are those that are performed automatically by the compiler from low to high levels. The rules are as follows
- byte short int long float double
- char int long float double
byte b=4;
int x=b;
Copy the code
2. Cast a type
A cast is a cast that forces the compiler to perform in the format: variable name
double money=76.69;
int balance=(int) money;
Copy the code
The operator
Operator classification
Arithmetic operator
The operation rules for binary operators:
Integer arithmetic
- If either of the operands is Long, the result is also Long
- In the absence of Long, the result is int. Even if the operands are all short or byte, the result is an int
Floating-point operations:
- If either of the operations is a double, the result is a double.
- The result is float only if both operands are float.
Modulus operation
The operation can be a floating point number, usually an integer, and the result is a “remainder,” which has the same symbol as the operation on the left.
For example: 7%3 = 1 -7%3 = -1 7 % -3 = 1
The ++, – – of the arithmetic operators are unary operators that require only one operand
Assignment and its extended assignment operator
Relational operator
Logical operator
An operator
String connector
Conditional operator
X ? Y : Z
Operator precedence
Be familiar with the priority of logical and, logical or, logical not! (Logic not > logic and > logic or).
Such as: a | | b & c operation result is: a | | (b & c), instead of (a | | b) & c
Symbolic operationThe + + + a
Note:
a++ ++a
A ++ and ++a are both increment operators. The difference is that it takes time to increment the value of the variable a.
A++ is value first, then increment.
Plus plus a is increment and then value.
For example, suppose x=3 and y=4;
(1) (x++)+(++x)=8
Explanation:
For the first one (x++), because x++ is the increment, so (x++) gets 3, and then x increments, so x is equal to 4;
For the second (++x), since ++x is incrementing and then valuing, (++x) yields a value of 5. In this case, x=5, so the result is 8.
(2) (x++)/3+(–y)*2-(x–)%6+(y++)*3-(y–)
1 + 6-4 + 9-4 = 8
Explanation:
First (x++) is 3, then x=4; And then (–y) is 3, so y is 3; And then (x–) is 4, and then x is 3; And then y++ takes 3, and then y is equal to 4; And then y– let’s take 4, and then y is 3;
Note :() can increase the priority of the arithmetic, so the expression in parentheses is evaluated first, but x++ is valued as x, and then x is incremented.
Important: the ++ operation has a high priority
Interview questions:
int i = 10 i = i++ ; Copy the code
Now, what is the value of I?
A: The value of I is 10
Resolution:
First I ++ is 10, then I is incrementing, and I is 11, and then I is assigned, so I is 10.
Welcome to subscribe column to invite you to drink a cup of Java, hope to bring convenience to friends in need, but also hope to get everyone’s attention and support.