1.1 define integer / long integer / single precision floating point... Variables
int variable name = initial value;
eg:
int num = 10;//Define an integer variable System.out.println(num);//Output defined integer variables
public class HelloWorld { public static void main (String [] args) { int a=10;//Defines a variable of type int System.out.println (a) ;//Print and wrap System.out.print (a) ;//Print without wrapping System.out.printf ( " %d \n" ,a);//Output in formatted form } }
Local variables must be initialized. In java, only local variables have no global variables. If you don't know why to initialize, initialize to 0
① Define long integer variables and single precision floating-point variables, and add f/F l/L after the initial value
long num = 10L; float num = 1.0f;
② Special examples
public static void main (String [] args) { int a = 1; int b = 2; System.out.println(a/b);//Int divided by int is still int, and the result is 0 }
public static void main (String[] args) { double a = 1; double b = 2; System.out.println(a/b);//The result is 0.5 }
public static void main(String[] args) { double num = 1.1; System.out.println(num * num);//The result is 1.2100000000 o00002 }
1.1.1 value range of common variables
System.out.println(Integer.MAX_VALUE);//Maximum value of int System.out.println(Integer.MIN_VALUE);//Minimum value of int
public class Helloworld { public static void main (string [] args) {int a=10;//Defines a variable of type int system.out.println(Integer.MAX_VALUE);//Output the maximum value of int system out. println(Integer.MIN_VALUE);// Minimum value of output int} }
1.1.2 boolean type
① There are only two values for boolean variables. True means true and false means false
② boolean in java cannot be interchanged with int type. 1 means true if it does not exist
1.2 string
① Double quotation marks + several characters represent a string
When the output result needs to be preceded by text to describe the output content, use double quotation marks to cause text content + connection
② Any type of data character and string splicing is a string
public class HelloWorld { public static void main (String[] args) { int a=10 ; int b=20 ; System.out.println (a+b) ; System.out.println("Zhang San"+a+b);//Any type of data is spliced with a string, and the structure is a string } }
③ Common escape characters
Escape character | explain |
---|---|
\n | Line feed |
\t | Horizontal tab |
\' | Single quotation mark |
\" | Double quotation mark |
\\ | Backslash |
1.3 variables
① In java, identifiers are underlined by numbers and letters
group
become
,
but
no
can
with
number
word
open
head
(
Composition, but cannot start with a number(
Composition, but cannot start with a number (and underscore can start, but it is not recommended)
② The small hump naming method is recommended, that is, the initial letter is capitalized, and Pinyin is not used as much as possible
1.4 constants
final modifier constant
public static void main (String[] args) { final int a =10; //final modifier constant System.out.println(a); }
1.5 type conversion
1.5.1 mixed operation of int and long/double
Assignment between variables of different numeric types means that types with a smaller range can be implicitly converted to types with a larger range, otherwise not (small ones can be given to large ones)
When int and long are mixed, int will be promoted to long, and the result will still be long
public static void main (String[] args) { int a = 10; long b = 20; a = b; }//Helloworld.java:81: error: incompatible type: conversion from long to int may lose a = b;
1.5.2 assignment of int and boolean to each other
int and boolean are two unrelated types and cannot be converted to each other
public static void main4 (String[] args) { int a = 10; boolean b = true; a = b; b = a; }
1.5.3 assign value to byte with int constant
public static void main (String[] args) { byte a = 100;//adopt byte n = 288;// Conversion from int to byte may be lost }
1.5.4 conversion between int and String
public static void main (String[] args) { int num = 10; String str1 = num + "";//Method 1 convert int to String System.out.println(str1); String str2 = String.valueOf(num);//Method 2 convert int to String System.out.println(str2); } //10 //10
public static void main1 (String[] args) { String str ="100"; int num = Integer.parseInt(str); //Convert String to int System.out.println(num); } //100
Summary of type conversions
① For the mixed operation between int and long/double, the small type will be converted to the large type
② int and boolean cannot be assigned to each other
③ int can be assigned to byte type, but cannot exceed the range of byte
④ int and string types can be converted to each other using statements
2 operator
2.1 arithmetic operators
2.1.1 basic operators
++ - * / %
① The result of int/int or int needs to be calculated with double. Both sides of / are int. the result is int. as long as there is a decimal, the result is a decimal
② 0 cannot be a divisor
③ % can take remainder for int and double
public static void main (String[] args) { System.out.println (5/2);//2 System.out.println(5.0/2);//2.5. If there is only one decimal on both sides, the result is decimal system out. println((float)5/2);// 2.5, first convert the top 5 to single precision floating point System.out.println((float)(5/2));//2.0, forcibly convert the result of 5 / 2 to single precision floating-point type }
2.1.2 increment operator
+= -= *= /= %=
a+=1 is equivalent to a = a + 1
2.1.3 auto increment / Auto decrement operator
++ –
public static void main(String[ ] args) { int a = 10; int b = ++a ; //Assign a to b after a+1 System.out.println (a) ; / /11 System.out.println(b); //11 } public static void main (String[] args){ int a = 10 ; int b = a++;//First assign a to b and then a+1 System.out.println (a);//11 System.out.println (b) ;//10 }
Special examples to remember
public static void main (String [] args) int i = 10; i = i++; System.out.println (i);//10 }
2.2 relational operators
!= < > <= >=
The expression return values of relational operators are of boolean type
2.3 logical operators
&& || !
The operands and return values of relational operators are of boolean type
&&And 𞓜 follow the short circuit evaluation rules
① & &, r if the expression on the left is false, the whole expression must be false. There is no need to evaluate the expression on the right
② |, if the expression on the right side is true, the whole expression must be true, and there is no need to evaluate the expression on the right side
System.out.println(10>20&&10/0 == 0);//false System.out.println(10<20&&10/0 == 0);//true //Calculating 10 / 0 will result in abnormal program error. The above error is not reported because the above logical symbols comply with short-circuit evaluation, and the expression on the right side of the operator is not calculated
2.4 bit operators
&(bitwise AND) | (bitwise OR) ~ (bitwise reverse)
^(bitwise XOR, if the binary bits of two numbers are the same, the result is 0 and the opposite is 1)
Bit operation means to operate according to binary bits, convert two numbers into binary for bit operation, get binary results, and then convert them into decimal
The result is still decimal
2.5 shift operation
<< >> >>>
① Move left < < leftmost position, don't, fill 0 on the rightmost
② Shift right > > the rightmost bit is not allowed, and the leftmost sign bit is filled (positive number is filled with 0, negative number is filled with 1)
③ Move right without symbol > > > do not use the rightmost bit, and fill 0 in the leftmost bit
2.6 conditional operators
ternary operator
Expression 1? Expression 2: expression 3
When the value of expression 1 is true, the value of the whole expression is the value of expression 2;
When the value of expression 1 is false, the value of the entire expression is the value of expression 3