Today's content
Data type conversion
Arithmetic operator
Comparison operator
Logical operator
Ternary operator
Simple method definitions and calls
Teaching objectives
Understand data type casting
Understand automatic conversion of data types
Understanding ASCII encoding tables
Understand the operation principle of int type and char type
Understand the operation mode of operator + + --
Understand the function of + symbol in string
Understanding comparison operators
Understanding logical operators
Master the format and calculation results of ternary operators
Understand the concept of method
Master the definition format of no return value and no parameter method
Understand the considerations for method definition
Chapter 1 data type conversion
The data required to participate in the calculation in Java programs must ensure the consistency of data types. If the data types are inconsistent, type conversion will occur.
1.1 automatic conversion
What data type will result from the addition of an int variable and a byte variable?
As a result of the operation, the type of the variable will be int, which is the phenomenon of automatic type conversion of data types.
Automatic conversion: automatically promote a type with a small value range to a type with a large value range.
int i = 1 ;
byte b = 2 ;
Schematic diagram of conversion principle
Byte type memory occupies 1 byte, which will be promoted to int type during operation with int type, and 3 bytes will be automatically supplemented. Therefore, the calculated result is still int type
Type.
Similarly, when an int variable and a double variable operate, the int type will be automatically promoted to double type for operation.
Conversion rules
Types with a small range are promoted to types with a large range. byte, short and char operations are directly promoted to int.
1.2 forced conversion
class Test {
public static void main(String[] args) {
int i = 1;
byte b = 2;
// byte x = b + i; // report errors
//Int type and byte type operation, and the result is int type
int j = b + i;
System.out.println(j);
}
}
class Test2{
public static void main(String[] args) {
int i = 1;
double d = 2.5;
//int type and double type operation, and the result is double type
//int type is promoted to double type
double e = d+i;
System.out.println(e);
}
}
byte,short,char‐‐>int‐‐>long‐‐>float‐‐>double
1.2 forced conversion
What happens when 1.5 is assigned to a variable of type int? Failed to generate compilation. It must be unable to assign value.
Double type memory is 8 bytes, and int type memory is 4 bytes. 1. 5 is double, and the value range is greater than int. Double is 8
For a 4-liter kettle, int is a 4-liter kettle. You can't put the water in the big kettle directly into the small kettle.
If you want to assign a value successfully, you can assign a value only by casting a double type to an int type.
Forced type conversion: converts a type with a large value range into a type with a small value range.
In comparison, automatic conversion is performed automatically by Java, and forced conversion needs to be performed manually by ourselves.
Conversion format:
Assign 1.5 to int type, and modify the code to:
Similarly, when a short type is added to 1, we know that the type will be promoted, but if we want to assign the result to a short type variable, we need to cast.
Schematic diagram of conversion principle
int i = 1. 5 ; // error
Data type variable name = (data type) transferred data value;
// double type data is forcibly converted to int type, and the decimal point is directly removed.
int i = (int) 1. 5 ;
public static void main(String[] args) {
//short type variable, 2 bytes in memory
short s = 1 ;
/*
Compilation failure occurred
s When doing operations with 1, 1 is of type int, and s will be promoted to type int
s+ 1 The result after is of type int, and an error occurs when assigning the result to type short
short Memory 2 bytes, int type 4 bytes
int must be forced to short to complete the assignment
*/
s = s + 1 ;//Compilation failed
s = (short)(s+ 1 );//Compilation succeeded
}
Strong attention
Converting a floating point to an integer directly cancels the decimal point, which may cause loss of data accuracy.
int is forcibly converted to short and 2 bytes are cut off, which may cause data loss.
1.3 ASCII coding table
There are binary 0 and 1 data inside the computer. How can the computer directly recognize human characters? The concept of coding table is generated.
Coding table: it is to form a table by corresponding human words with a decimal number.
// Define s as the maximum value in the short range
short s = 32767 ;
// After the operation, it is forced to convert. After cutting off 2 bytes, there will be uncertain results
s = (short)(s + 10 );
public static void main(String[] args) {
//Character type variable
char c = 'a';
int i = 1 ;
//Character type and int type calculations
System.out.println(c+i);//The output is 98
}
Arithmetic operators include:
+Addition operation, string connection operation
*Multiplication
/Division operation
%Modulo operation, division of two numbers and remainder
++, -- self increasing and self decreasing operation
Character value
0 48
9 57
A 65
Z 90
a 97
z 122
It is stipulated that:
All English letters, numbers and symbols are corresponding to the decimal system, so the world's first coding table ASCII is generated(
American Standard Code for Information Interchange.
Tips:
During the calculation of char type and int type, char type characters first query the coding table to get 97, and then sum with 1 to get 98. Char type promotion
For int type. char type memory 2 bytes, int type memory 4 bytes.
Chapter 2 operators
2.1 arithmetic operator
In Java, integers use the above operators. No matter how they are calculated, they will not get decimals.
++Operation, the variable increases by 1. On the contrary, -- operation reduces the variable by 1, and the usage is consistent with + + operation.
Independent operation:
When variables are operated independently, there is no difference between pre + + and post + +.
Before variable + +: for example, + + i.
After variable + +: for example, i + +.
public static void main(String[] args) {
int i = 1234 ;
System.out.println(i/ 1000 * 1000 );//The result is 1000
}
Assignment operators include:
=Equal sign
+=Plus equals
*=Multiply equal
/=Division equals
%=Mold taking, etc
Mixed operation:
Together with other variables, the former + + and the latter + + are different.
Before variable + +: add 1 to variable a, and assign the result after adding 1 to b, that is, a calculates first. The results of a and b are both 2.
After variable + +: variable a assigns its own value 1 to variable b. at this time, the value of variable b is 1, and variable a adds 1. The result of a is 2, b
The result is 1.
+Operation of symbol in string:
+When a symbol encounters a string, it indicates the meaning of connection and splicing.
The result of "a" + "b" is "ab", connecting the meaning
2.2 assignment operator
The assignment operator is to assign the value on the right of the symbol to the variable on the left.
2.3 comparison operators
public static void main(String[] args) {
int a = 1 ;
int b = ++a;
System.out.println(a);//The result is 2
System.out.println(b);//The result is 2
}
public static void main(String[] args) {
int a = 1 ;
int b = a++;
System.out.println(a);//The result is 2
System.out.println(b);//The calculation result is 1
}
public static void main(String[] args){
System.out.println(" 5 + 5 ="+ 5 + 5 );//Output 5 + 5 = 55
}
public static void main(String[] args){
int i = 5 ;
i+= 5 ;//The calculation method is i=i+ 5. Add 5 to variable i first, and then assign variable i
System.out.println(i); //The output is 10
}
Comparison operators include:
==Compare whether the data on both sides of the symbol are equal. The result of equality is true.
< compare whether the data on the left of the symbol is less than the data on the right. If less than, the resu lt is true.
>Compare whether the data on the left of the symbol is greater than the data on the right. If greater than, the result is true.
< = compare whether the data on the left of the symbol is less than or equal to the data on the right. If less than, the resu lt is true.
>=Compare whether the data on the left of the symbol is greater than or equal to the data on the right. If less than, the result is true.
!= Not equal to the symbol. If the data on both sides of the symbol is not equal, the result is true.
Logical operators include:
&&Short circuit and
1. Both sides are true, and the result is true
2. One side is false, and the result is false
Short circuit feature: false on the left and no operation on the right
||Short circuit or
1. Both sides are false, and the result is false
2. One side is true, and the result is true
Short circuit feature: the left side of the symbol is true, and the right side is no longer calculated
! Reverse
1.! true the result is false
2.! false the result is true
2.3 comparison operators
A comparison operator is an operation that compares two data. The result of the operation is a Boolean value of true or false.
2.4 logical operators
Logical operator is an operator used to connect two Boolean results. The operation results are Boolean values true or false
public static void main(String[] args) {
System.out.println( 1 == 1 );//true
System.out.println( 1 < 2 );//true
System.out.println( 3 > 4 );//false
System.out.println( 3 <= 4 );//true
System.out.println( 3 >= 4 );//false
System.out.println( 3 != 4 );//true
}
2.5 ternary operator
Ternary operator format:
Ternary operators are evaluated:
The result of boolean type expression is true, and the overall result of ternary operator is 1, which is assigned to variable.
The result of Boolean expression is false, and the overall result of ternary operator is result 2, which is assigned to variable.
Chapter III introduction to methods
3.1 general
When we learn operators, we create a new class and main method for each operator separately. We will find that writing code in this way is very cumbersome and easy
Too many duplicate codes. Can we avoid these repeated codes? We need to use methods to implement them.
Method: extract a function and define the code in a brace to form a separate function.
When we need this function, we can call it. This not only realizes the reusability of the code, but also solves the phenomenon of code redundancy.
3.2 definition of method
Define format:
Definition format interpretation:
Modifier: the current fixed writing method is public static.
public static void main(String[] args) {
System.out.println(true && true);//true
System.out.println(true && false);//false
System.out.println(false && true);//false, not calculated on the right
System.out.println(false || false);//falase
System.out.println(false || true);//true
System.out.println(true || false);//true, not calculated on the right
System.out.println(!false);//true
}
Data type variable name = Boolean expression? Result 1: result 2
public static void main(String[] args) {
int i = ( 1 == 2 ? 100 : 200 );
System.out.println(i);// 200
int j = ( 3 <= 4 ? 500 : 600 );
System.out.println(j);// 500
}
Modifier return value type method name (parameter list)
Code
return ;
}
Return value type: the current fixed writing method is void. Other return value types will be explained in later courses.
Method name: name the method we define, which meets the specification of identifier and is used to call the method.
Parameter list: there are no parameters at present. Methods with parameters will be explained in later courses.
Return: the method ends. Because the return value type is void, the return in method braces can not be written.
give an example:
3.3 method call
After the method is defined, the method will not run by itself and must be called to execute. We can call the method defined by ourselves in the main method. stay
In the main method, write the name of the method to be called directly.
3.4 calling exercises
Extract the ternary operator code into a custom method and call.
3.5 precautions
Precautions for method definition:
Methods must be defined outside of methods in a class
A method cannot be defined inside another method
public static void methodName() {
System.out.println("This is a method");
}
public static void main(String[] args) {
//Call the defined method
method();
}
//Defines a method that is called by the main method
public static void method() {
System.out.println("Self defined methods need to be main Call run");
}
public static void main(String[] args) {
//Call the defined method operator
operator();
}
//Define a method in which ternary operators are defined
public static void operator() {
int i = 0 ;
i = ( 1 == 2 ? 100 : 200 );
System.out.println(i);
int j = 0 ;
j = ( 3 <= 4 ? 500 : 600 );
System.out.println(j);
}
Chapter 4: JShell scripting tools
JShell scripting tool is a new feature of JDK 9
When will we use the JShell tool? When we write very little code, but we are not willing to write classes, main methods, or compile and run
OK, you can use the JShell tool at this time.
Start the JShell tool and directly enter the JShell command on the DOS command line.
Next, you can write Java code without writing classes and methods. You can write the code in the method directly. At the same time, you can enter directly without compiling and running
public class Demo {
public static void main(String[] args){
}
//Write correctly. In the class, methods can be defined outside the main method
public static void method(){}
}
public class Demo {
public static void main(String[] args){
//Wrong writing. One method cannot be defined inside another method
public static void method(){}
}
}
Tips:
JShell tool is only suitable for testing fragment code. It is recommended to write more content in the method.
Chapter V expanding knowledge points
5. Extension of 1 + = symbol
Is there a problem with the following procedure?
Analysis: s += 1 is logically regarded as s = s + 1. The calculation result is promoted to int type, and an error occurs when assigning value to short type, because the value range cannot be changed
A large type is assigned to a type with a small value range. However, s=s+ 1 is operated twice, + = is an operator, which is operated only once, and has the characteristics of coercion,
In other words, s += 1 is s = (short)(s + 1), so the program has no problem. It compiles and runs with a result of 2
5.2 operation of constants and variables
Is there a problem with the following procedure?
public static void main(String[] args){
short s = 1 ;
s+= 1 ;
System.out.println(s);
}
Analysis: b 3 = 1 + 2. 1 and 2 are constants and are fixed data. When compiling (compiler javac), it has been determined that the result of 1 + 2 does not change
There is a value range beyond byte type, which can be assigned to variable b 3, so b 3 = 1 + 2 is correct.
On the contrary, B 4 = b 2 +b 3, b 2 and b 3 are variables, and the value of variables may change. At the time of compilation, the compiler javac is not sure what the result of b 2 +b 3 is
Therefore, the result will be processed as int type, so int type cannot be assigned to byte type, so compilation fails.
Reflected in jshell:
public static void main(String[] args){
byte b 1 = 1 ;
byte b 2 = 2 ;
byte b 3 = 1 + 2 ;
byte b 4 =b 1 + b 2 ;
System.out.println(b 3 );
System.out.println(b 4 );
}