1. Java data types:
1.1 Java data types are divided into two categories:
1.1.1 Basic data types and reference data types
1.2 Basic Data Types:
1.2.1 Eight data types:
The integer | Size of memory space occupied | Data range |
---|---|---|
byte | 1 byte (eight bits) | – 128 ~ 127 |
short | 2 bytes (16 bits) | – 32768 ~ 32767 |
int | 4 bytes (32 bit) | Minus 2^31 to 2^ 31-1 |
long | 8 bytes (64 bit) | – 2^63 ~ 2^63-1 |
floating-point | Size of memory space occupied | Data range |
---|---|---|
float | 4 bytes (32 bit) | ±3.4 * 10 ^ 38 |
double | 8 bytes (64 bit) | 10 ^ 308 |
character | Size of memory space occupied | Data range |
---|---|---|
char | 2 bytes (16 bits) | Can save Chinese |
Boolean type | Size of memory space occupied | Data range |
---|---|---|
boolean | It depends | true false |
1.2.2 Conversion of basic Data Types:
- All numeric variables in Java can be converted to each other. Automatic type conversion: - Automatic type conversion can be performed by default when a small range numeric type or variable is directly assigned to another large range variable. Just as there are two bottles of water, a large bottle and a small bottle, there will be no problem when the small bottle of water is poured into the big bottle. Automatic type conversion supported in Java:1,byte->short->int->long->float->double
2,char->int->long->float->doubleCast: - If you want to cast the type to the right of the arrow to the type to the left of the automatic cast type above, you must cast it. A cast can result in a loss of precision in a value or variable. When a forced conversion is performed, similar to pouring water from a large bottle into a small one, it causes water to overflow. - Caution: Be careful when trying to convert a large range of numeric types or variables to a small range of types, as data information can be lost.Copy the code
1.3 Reference Data types
1.3.1 Five types of referenced data:
Classes, interfaces, arrays, enumerations, annotationsCopy the code
1.4 Differences between basic data types and reference data types:
From a conceptual point of view: - Basic datatype: variable name refers to a specific value - Reference datatype: variable name refers to the memory address of the data object. From a memory point of view: - Basic datatype: Java allocates memory for a variable immediately after it is declared - reference datatype: Variable declaration does not allocate memory, just store a memory address in terms of usage: - Basic data type: use the specific value, use the == sign - reference data type: use can be assignednull, using the equals() methodCopy the code
1.5 constant
1.5.1 Constants in Life:
A week7Day day24Hour by hour60Minute by minute60seconds3.1415926. A B C D A B C D Code from life, the final feedback in life! Values, text and text, these can be considered constants!!Copy the code
1.5.2 Constants in development:
Definitions: Quantities that do not change during the execution of a program are called constants. Constants are conventionally divided into the following classes: integers:1 2 3 4 5 -1 -2 -3- -4 -5Decimal (floating point) :3.1415926 0.618 9.9True-false relation (Boolean type) : Ttrue F falseCharacter:'A' 'a' 'you' '我' 'he'A character constant is a string containing a single element:"Hello, are you there?" "No, I'm not."A string constant is all that is contained in double quotation marksCopy the code
1.6 variable
1.6.1 Variables in Life:
Time Weather temperature Age weight bank card balanceCopy the code
1.6.2 Format of variables defined in the code:
Format: Data type variable name = initialize data;int a = 10; The data stored in the variable name can be changed during the running of the program! Data types: what is the constraints of the current variable data type variable name: convenient operation variable name = assignment number: the number assignment to be on the right side of the data assigned to the left of the variable name Initialization data: data corresponding to the type of the initial value of the variable name naming rules may refer to: "alibaba" specification for Java development attached file download address:Copy the code
Click to download “Alibaba Java Development Specification”
2. The operator
2.1 Various operators
2.1.1 Arithmetic operators:
Life operators: + - * / () for programmers: + - * / () % 15/4 = 3 multiply and divide before adding and subtracting parentheses first deal with the contents of parentheses first Divisor cannot be 0, invalid operation basic rule is to calculate from left to right Note: 1. Pay attention to the changes in the data stored in variables during operation. Note which operator is used. 3. The value of a variable will be changed if and only if it is assigned. The variable will not change if it is not assigned.Copy the code
2.1.2 Assignment operators:
= + = = * = / = % = for example: the numA numA + numB = = = > numA + = numBCopy the code
2.1.3 Increment and decrement operators:
+ + : after the variable 】, first the current statement, then perform the operation ] before [variables, executed first since the operation, to perform the current statement - : in the "variable", the first execution of the current statement, then perform the subtraction operation ] before [variables, executed first decrease since the operation, to perform the current statement Note: The increment and decrement operators have and can only operate on variables! Increment is equivalent to the current variable +=1The decrement is equivalent to the current variable -=1Consider the readability of developers1, if using the increment and decrement operator, please separate line!2, can use +=1Or - =1Instead, don't use autoincrement and autodecrementCopy the code
2.1.4 Relational Operators:
> < ≥ ≤ ≠ = This is the relational operator > < >= <=! == = Relational operator in development The result of the relational operator istrueorfalse
Copy the code
2.1.5 Logical Operators:
&& with: true is true, false or false bank safe, open the safe need two keys, one cannot | | or: his door, and only one key to open the door There are really true, and false is false! The breaking principle of anti-logic operators:class Demo {
public static void main(String[] args) {
int num = 10;
In logic and expression, if any element is false, then the expression will not run because the result of the whole logic and expression will not change. * /
boolean ret = 3 > 4 && ++num > 5;
System.out.println("ret =" + ret); // false
System.out.println("num =" + num); / / 10
int num2 = 20;
In a logic or expression, if any condition is true, the subsequent expression will not run because the result of the whole logic or expression is already determined. Subsequent operations are meaningless! * /
ret = 4 > 3 || ++num2 > 200;
System.out.println("ret =" + ret); // true
System.out.println("num2 =" + num2); / / 20}}Copy the code
2.1.6 bit operators:
& | ~ ^ > > < < & : with operation1for1, there are0【 the 】0| : or operation1【 the 】1And the same0for0~ : takes the inverse operation1for0.0for1^ : xor operates on two operands, both equal to0Different is,1>> : Right shift operation A >> B changes the binary value of the value a from0We're going up to b minus1Bit, the whole moves b bit to the right, the sign bit remains unchanged, and the bit complement value vacated by the high position0.5 >> 1= = = >1000 0000 0000 0101 >> 1 = 1000 0000 0000 0010 = 2A >> b = a/(2^ b) << : left shift operation A << b takes the binary value of the value a from0We're going up to b minus1Bit, the whole thing moves b bit to the left, the sign bit remains the same, and the bit complement value vacated by the low bit0.5 << 1= = = >1000 0000 0000 0101 << 1 = 1000 0000 0000 1010 = 10A << b = a * (2 ^ b)
Copy the code
2.1.7 Ternary operators:
Grammar structure: (booleanExpression)? (expression1) : (expression2) Execution process: Judge firstbooleanValue of the expression: iftrue, the value of the whole operation formula is the expression1The value of the; iffalse, the value of the whole operation formula is the expression2The value of the.int a = 10, b = 1;
int max = a > b ? a : b; // if a>b is true, a is large, and the value of a is assigned to Max. If a>b is false, b is large and the value of b is assigned to Max
Copy the code
3. Simple exercises
3.1 supplement
3.1.1 Use of Scanner
The current code needs to modify the numerical value of a variable, recompile and then continue to run, for the average user, there is no value. We need to give the user a way to enter data from the keyboard. Need to use Scanner!!1Point skill point!! [Guide package] inclassbeforeimport java.util.Scanner;
2Create a Scanner"Variable"
Scanner sc = new Scanner(System.in);
3Use the Scanner method to retrieve data from the keyboard as input by the userinttypeintnum = sc.nextInt(); To obtainfloattypefloatnum = sc.nextFloat(); Get a Doubledoublenum = sc.nextDouble(); To obtainchartypechar ch = sc.nextLine().charAt(0);
Copy the code
3.1.2 Consideration of user experience friendliness
- As a software developer, you need to give users proper hints in your code, telling them whether you need input or access from the perspective of user use - this is the embodiment of user experience friendlinessCopy the code
3.1.3 Filtering user input Data
After we consider user experience friendliness, the following questions arise: - user input data is of a certain error conditions, so our program is to allow the user to enter the data of "legitimacy" problem - if the user input data is not legal, then could not enter the code to run normally, and give users a hint, if the user input data are correct, the program to continue!!!!!!Copy the code
3.2 Write some conditional judgment requirements using logical and relational operators.
Requirements: 1. Refer to the ASCII code table. 2Copy the code
3.2.1 Determine whether a variable of character type is an uppercase English letter.
public class TestUpperCase {
public static void main(String[] args) {
System.out.println("Please enter one English letter");
Scanner scanner = new Scanner(System.in);
char ch = scanner.nextLine().charAt(0);
if (ch >= 'A' && ch <= 'Z') {
System.out.println("You are typing in capital English letters!");
} else {
System.out.println("What you typed is lowercase English!"); }}}Copy the code
3.2.2 Determining whether a variable of character type is an English letter.
public class TestEnglishCase {
public static void main(String[] args) {
System.out.println("Please enter one character:");
Scanner scanner = new Scanner(System.in);
char ch = scanner.nextLine().charAt(0);
if ((ch > 'A' && ch < 'Z') || (ch > 'a' && ch < 'z')) {
System.out.println("What you typed is an English letter!");
} else {
System.out.println("What you typed is not an English letter!"); }}}Copy the code
3.2.3 Determining whether a year is a leap year.
/* Leap year condition: divisible by 4, but not by 100. Year % 4 == 0 year % 100! = 0 year % 400 == 0 */
public class TestYear {
public static void main(String[] args) {
System.out.println(Please enter a year:);
Scanner scanner = new Scanner(System.in);
int year = scanner.nextInt();
if ((year % 4= =0 && year % 100! =0) || year % 400= =0) {
System.out.println("It's a leap year!");
} else {
System.out.println("It's not a leap year!"); }}}Copy the code