First sight of JAVA-Scanner & process control-primary evolution 04
Scanner
Function: realize the interaction between the program and people
next():
- You can end the input after reading a valid character
- The blanks encountered before entering valid characters will be removed
- next () can not get a string with spaces
nextLine():
- Enter the end, you can get a blank
Scanner scanner = new Scanner(System.in);
System.out.println("开始接收输入");
String str = scanner.nextLine();
System.out.println(str);
scanner.close();
java is a strongly typed language
basic type
byte 1B
short 2B
Int 4B
long 8B
float 4B (use with caution, about =)
double 8B
用BigDecimal
char 2B
String! is the class
boolean 1b
Reference type
Except 8 basic types are reference types
Forced conversion
High-low
overflow
Loss of accuracy
Automatic conversion
Low-high
variable
Class variable static
Instance variable: subordinate to the object, there is a default value if it is not initialized by itself, the default value of boolean is false, and the default value is null except for basic types
Local variables must be declared and initialized
constant
static final
Variable capitalization
Operator
When performing operations on different basic types of data
Convert long with long
Convert int without long
a++,++a
Auto-increment after executing the code
Bit operation
Extremely efficient
,<< *2
String concatenation+
The content has a string, it will become String
But there are operations in front of the string, and then it becomes a String
Ternary operator
X ?a;b
X judgment, true output a; false output b
Other methods: nextInt(), nextFloat()
select
if
Once the judgment is true, skip after execution
Single choice: if(){};
Double choice: if(){}else{}
Multiple choices: if(){}elseif{}else{}
switch
-
Failure to add break will cause case penetration
-
jdk7 began to support strings
-
Support char and string
cycle
while
- Most need to stop the loop, need an expression invalidation to end the loop
- In a small number of cases, it is necessary to loop and always perform such as server request response monitoring
dowhile
The difference between while and dowhile
while: judge first and then execute
dowhile: execute first and then judge, execute at least once
for
for(;;)
Perform the initialization step first, you can declare a type, you can initialize one or more loop control variables, or it can be empty
Update loop variables after executing a loop
for(x:n)
For arrays and collections
break和continue
break: exit the loop
continue: jump to this loop, continue to the next loop
goto
Don’t need to master it, rarely use now