Dark Horse programmer-java basics-basic knowledge (1), dark horse java
------ Android training, java training, and hope to communicate with you! ------ A java keyword is a word defined in the programming language in advance and given a special meaning, also known as reserved words.
Keywords |
Description |
Abstract |
Abstract method, modifier of abstract class |
Assert |
Whether the asserted condition is met |
Boolean |
Boolean data type |
Break |
Skip loop or label code segment |
Byte |
8-bit Signed Data Type |
Case |
A condition of the switch statement |
Catch |
Catch exceptions with try |
Char |
16-bit Unicode character data type |
Class |
Definition class |
Const |
Unused |
Continue |
Do not execute the remaining part of the loop body |
Default |
Default branch in the switch statement |
Do |
Loop statement. The loop body must be executed at least once. |
Double |
64-bit double-precision floating point number |
Else |
Branch to be executed when the if condition is invalid |
Enum |
Enumeration type |
Extends |
Indicates that a class is a subclass of another class. |
Final |
Indicates that a value cannot be changed after initialization. Indicates that the method cannot be overwritten, or a class cannot have a subclass. |
Finally |
It is designed to complete the executed code, mainly for the robustness and integrity of the program. The code is executed no matter whether exceptions occur. |
Float |
32-bit Single-precision floating point number |
For |
For Loop statement |
Goto |
Unused |
If |
Condition Statement |
Implements |
Indicates that a class implements the interface |
Import |
Import class |
Instanceof |
Test whether an object is an instance of a class. |
Int |
32-bit integer |
Interface |
Interface, an abstract type, with only the definition of methods and constants |
Long |
64-bit integer |
Native |
Non-java code implementation |
New |
Allocate a new class instance |
Package |
A series of related classes form a package |
Private |
Indicates a private field or method, which can only be accessed from the class. |
Protected |
Indicates that a field can only be accessed through a class or its subclass. Subclass or other classes in the same package |
Public |
Indicates a common property or method. |
Return |
Method Return Value |
Short |
16-digit |
Static |
Indicates that all instances are shared at the class level. |
Strictfp |
Strict rules for comparing floating point numbers |
Super |
Base Class |
Switch |
Select statement |
Synchronized |
Indicates a code block that can only be accessed by one thread at a time. |
This |
Indicates that the current instance is called. Or call another constructor. |
Throw |
Throw an exception |
Throws |
Possible exceptions thrown by defining methods |
Transient |
Modify fields not serialized |
Try |
Indicates that the code block must be exception handled or used together with finally to indicate whether to throw an exception and execute the code in finally. |
Void |
The flag method does not return any value. |
Volatile |
The marked field may be accessed by multiple threads at the same time without synchronization. |
While |
While Loop |
A binary identifier can be considered as a name. It is used to identify the package name, class name, method name, parameter name, and variable name. Java identifiers have strict naming rules: 1. all identifiers should start with a letter (A-Z or a-z), dollar sign ($), or underscore (_)
2. A combination of any characters after the first character
3. Keywords cannot be used as identifiers
4. The identifier is case sensitive.
Examples of valid identifiers: age, $ salary, _ value, and _ salesvalue
Example of invalid identifiers: 123abc and-salary we recommend that you follow the naming rules below to make the code more standardized: 1. All letters in the package are in lowercase, for example, com. java. one. 2. The class name and the first letter of each word in the interface name should be capitalized, such as Arraylist and Iterator. 3. All the letters of the constant name are in uppercase, And the words are connected by underscores, for example, KAY_ONE_HONM. 4. the first letter of the variable name and method name is lowercase, and the first letter of each word starts from the second word, for example, lineNumber. 5. In a program, use meaningful words to define identifiers for easy reading. For example, userName indicates the user name. 3. Some escape characters supported in java
Symbol |
Character meaning |
\ N |
Line feed (0x0a) |
\ R |
Press enter (0x0d) |
\ F |
Page feed (0x0c) |
\ B |
Return (0x08) |
\ S |
Space (0x20) |
\ T |
Tab |
\" |
Double quotation marks |
\' |
Single quotes |
\\ |
Backslash |
\ Ddd |
Octal characters (ddd) |
\ Uxxxx |
Hexadecimal Unicode character (xxxx)
|
4. Data Types in java
1. Basic data type:
Java provides eight basic types. Six numeric types (four integers, two floating point types), one character type, and one boolean type.
The eight basic data types are embedded in the java language and have the same size and attributes in any operating system.
Byte:
- The byte data type is an 8-bit, signed, and binary complement integer;
- The minimum value is-128 (-2 ^ 7 );
- The maximum value is 127 (2 ^ 7-1 );
- The default value is 0;
- The byte type is used in large arrays to save space. It mainly replaces integers because the byte Variable occupies only 1/4 of the int type space;
- Example: byte a = 100, byte B =-50.
Short:
- The short data type is a 16-bit signed integer in binary complement.
- The minimum value is-32768 (-2 ^ 15 );
- The maximum value is 32767 (2 ^ 15-1 );
- The Short data type can also save space as a byte. A short variable is 1/2 of the space occupied by int variables;
- The default value is 0;
- Example: short s = 1000, short r =-20000.
Int:
- The int data type is a 32-bit, signed integer expressed in binary complement;
- The minimum value is-2,147,483,648 (-2 ^ 31 );
- The maximum value is 2,147,485,647 (2 ^ 31-1 );
- Generally, the default Integer type is int;
- The default value is 0;
- Example: int a = 100000, int B =-200000.
Long:
- The long data type is a 64-bit, signed integer expressed in binary complement;
- The minimum value is-9,223,372,036,854,775,808 (-2 ^ 63 );
- The maximum is 9,223,372,036,854,775,807 (2 ^ 63-1 );
- This type is mainly used on systems that require large integers;
- The default value is 0L;
- Example: long a = 100000L, int B =-200000L.
Float:
- Float data types are single-precision, 32-bit floating point numbers that comply with the IEEE 754 standard;
- Float saves memory space when storing large floating point groups;
- The default value is 0.0f;
- Floating point numbers cannot be used to represent exact values, such as currency;
- Example: float f1 = 234.5f.
Double:
- The double data type is a double-precision, 64-bit floating point number that complies with the IEEE 754 standard;
- The default floating point type is double;
- The double type cannot represent exact values, such as currency;
- The default value is 0.0f;
- Example: double d1 = 123.4.
Boolean:
- Boolean data type indicates the information of one digit;
- There are only two values: true and false;
- This type is only used as a flag to record true/false cases;
- The default value is false;
- Example: boolean one = true.
Char:
- Char is a single 16-bit Unicode character;
- The minimum value is '\ u0000' (0 );
- The maximum value is '\ uffff' (65,535 );
- Char data can store any character;
- Example: char letter = 'A '.
2. Reference Data Type
Reference data types are defined by programmers in java programs.
- Reference type variables are created by class constructors and can be used to access referenced objects. These variables are declared as a specific type. Once declared, the type cannot be changed.
- Objects and arrays are reference data types.
- The default values of all reference types are null.
- A reference variable can be used to reference any compatible type.
5. constants and variables 1. Constants
A constant is a fixed value in a program and cannot be changed.
In java, constants include-Integer constants, floating point constants, character constants, string constants, Boolean constants, and null constants.
The hexadecimal conversions in Integer constants are understood in the C language and are no longer recorded.
2. Variables
During the running of the program, some temporary data may be generated at any time, and the application will store the data in some memory units. The variable is to apply for memory to store the data. That is to say, when creating a variable, you need to apply for space in the memory.
The memory management system allocates storage space for variables based on their types. The allocated space can only be used to store data of this type. Therefore, you can store integers, decimals, or characters in the memory by defining different types of variables.
3. type conversion of variables 1. Automatic type conversion
Automatic type conversion is also called implicit type conversion. It means that the two data types do not need to be explicitly declared during the conversion process (the programmer does not need to emphasize the Declaration ). To enable automatic type conversion, you must meet two conditions at the same time:
First, the two data types are compatible with each other;
Second, the value range of the target type is greater than that of the source type.
For example:
Byte a = 3; int x = B; // convert the byte type variable to the int type without special Declaration
2. Forced type conversion
Forced type conversion is also called display type conversion. It refers to the declaration that the two data types need to be displayed. When the two data types are incompatible with each other, or the value range of the target type is smaller than the source type, automatic type conversion cannot be performed, You need to force type conversion (requires manual conversion by the programmer ).
For example:
public class Test { public static void main(String[] args) { int a = 4; byte b = a; System.out.println(b); } }
When the program is compiled, the following error message is displayed:
Compilation error, error: incompatible type: conversion from int to byte may cause loss, because when an int type value is assigned to the byte type, if the value range of the target type is smaller than the source type, that is, the value range of the int type is greater than the value range of the byte type, conversion may lead to data loss, A single byte variable cannot store four bytes of data.
In this case, the conversion will not result in an error. In this case, the forced type conversion format is as follows:
Target type variable = (target type) Value
public class Test { public static void main(String[] args) { int a = 4; byte b =(byte) a; System.out.println(b); } }
Compile again to pass
3. Automatic Upgrade of expression type
For example:
public class Test { public static void main(String[] args) { byte a = 4; byte b = 5; byte c = a+b; System.out.println(c); } }
At this time, an error is reported during compilation. Error: incompatible type: conversion from int to byte may cause loss, the reason is that during expression a + B operation, variables a and B are automatically upgraded to the int type, so the value of a + B becomes the int type, when a value of the int type is assigned to the byte type, an error occurs when the value range of the target type is smaller than the source type.
The error disappears after being modified to forced type conversion.
public class Test { public static void main(String[] args) { byte a = 4; byte b = 5; byte c = (byte)(a+b); System.out.println(c); } }
4. Scope of the Variable
We know that variables can be used only after they are defined, but the use of variables is limited in scope. That is to say, variables can only be used within their scope. This scope is called scope,
In a program, the scope of variables is generally divided into: "member variables" and "local variables ".
Member variables: the variables defined in the class body are called member variables, which are valid throughout the class.
Local variable: The variable defined in the method body of the class (that is, the variable declared in the code between "{" and "}"), called a local variable, it is only valid in the current code block (that is, it is valid between "{" and ).