JavaBasic
Java is a strongly typed language. Data types cannot be processed directly and need to be initialized first
data type
Data types are divided into
-
primitive type
-
Reference type
int i =10; //Hexadecimal output 10 int i2=010; //Octal 0 output 8 int i3=0x10 //Hex 0x output 16 float f = 0.1f; //0.1 double d = 1.0/10; //0.1 f==d; //false float d1 = 231313131313; float d2 = di+1; d1==d2; //true
float data type will have the above problems. Do not compare with floating point numbers. float is not accurate. Large numbers can be calculated and compared with bigdecimal.
char c1 = 'a'; char c2 = 'in'; System.out.println(c1); System.out.println(c2); System.out.println((int)c1); System.out.println((int)c2);
The output value is different (int). The nature of the text is still a number
97=a 65=A
char c3 = '\u0061'; // Output a
From Escape character_ Baidu Encyclopedia (baidu.com)
\a | Bell (BEL) | 007 |
---|---|---|
\b | Backspace (BS) to move the current position to the previous column | 008 |
\f | Page feed (FF) moves the current position to the beginning of the next page | 012 |
\n | Line feed (LF) moves the current position to the beginning of the next line | 010 |
\r | Press enter (CR) to move the current position to the beginning of the line | 013 |
\t | Horizontal tabulation (HT) (skip to next TAB position) | 009 |
\v | Vertical tabulation (VT) | 011 |
\ | Represents a backslash character '' | 092 |
' | Represents a single quotation mark (apostrophe) character | 039 |
" | Represents a double quote character | 034 |
\? | Represents a question mark | 063 |
\0 | Null character (NUL) | 000 |
\ddd | Any character represented by an octal number of 1 to 3 digits | Three digit octal |
\xhh | Any character represented by hexadecimal | hexadecimal |
boolean flage = true; if(flage){ //an old hand } if(flage==true){ //Novice, but it is recommended for novice }
Conversion of data types
In the operation, different types of data need to be transformed into the same type before calculation.
Strong type conversion from high to low
int i = 128; byte b = (byte) i //out of memory //b range - 128 to 127 // (type) variable
Automatic type conversion from low to high
int i = 128; double b = i; //b = 128 automatic conversion
Note:
-
boolean conversion is not allowed for boolean values
-
Object types cannot be converted to irrelevant types
-
Force conversion when converting high capacity to low capacity
-
Conversion may have memory overflow and precision problems
(int) 23.7 //23 (int) -45.89f //-45
char c = 'a'; // 97 int d = c+1; // d=98 char(d) // Convert int to char output b
Pay attention to overflow when the operation data is large:
int money = 10_0000_0000; //_ No output for easy viewing int years = 20; int total = money * years; //total should be 200_ 0000_ 0000 //But return - 1474836488 long total2 = money * years; //Still wrong, it was wrong before the conversion long total3 = money * ((long)years); //Right now
variable
The equation x of iconology is a variable that can be changed
Each variable must declare a type
matters needing attention:
-
Each variable must have a type -- 1 primitive type 2 Reference type
-
The variable name must be the same as the method, and the keyword cannot be used as the variable name
-
If a complete statement, add a semicolon at the end; And it must be in English
int a, b, c; //Also defined but not recommended //The code should be readable int a =1; int b =2; int c =3; String name = "zzz"; //Define string
Class variable
public class Demo{ static double salary = 2500 //Class variable public void main(String[] args){ System.out.println(salary); //Direct output } }
Instance variable
public class Demo{ //No initialization required int age; //Instance variable String name; //Instance variable public void main(String[] args){ Demo o = new Demo(); o.age; //Use 0 o.name; //Use null //All but the basic types are null by default } }
local variable
public class Demo{ public void main(String[] args){ //Variables are local variables here //Declaration and initialization required //It only works in the method } }
constant
But the definition cannot be changed. key word: final
static final double number = 1.1; final static double number = 1.1; //Both modifiers can be used, and the order of final and static does not affect
Naming conventions
Variable method class name: see the meaning of the name
-
Member variable: the first letter of every day is lowercase, followed by the first letter of the word is uppercase
-
Local variables: the first letter is lowercase, and the hump principle is the same as above, which is similar to member variables
-
Constants: uppercase letters and underscores MAX_NUMBER
-
Class name: initial capital Demo
-
Method name: initial lowercase big() veryBig()
operator
Arithmetic operators + - * /% (remainder) + + (self increasing) -- (self decreasing)
common problem
int a = 10; int b = 20; int c = 25; int d = 25; System.out.println(a+b); //Output 30 System.out.println(a-b); //-10 System.out.println(a*b); //200 System.out.println(a/b); //0. The problem should be 0.5 // Because it is an int class, the decimal is omitted System.out.println(a/(double)b); // This error can be eliminated by strong rotation long a = 121231231L; int b = 123; short c =10; byte d = 8; // If there is a long, it is long. If there is a double, it is double // No, this is Int System.out.println(a+b+c+d;) //Long System.out.println(b+c+d); //Int System.out.println(c+d); //Int //++ -- int a = 3; a++; // a = a+1 a =4 int b = 3; b--; // b = b-1 b = 2 int a = 2; int b = a++; System.out.println(b); // a=3 System.out.println(b); //b still runs after 2++ // Is a + + different from + + A in that + + a operates first or a + + assigns first //Special power operation has some tools of Math class to operate Math.pow(2,3); //The third power of 2 2 ^ 3
Relational operator
A Boolean value is always returned
int a = 10; int b = 20; System.out.println(a>b); System.out.println(a<b); System.out.println(a==b); System.out.println(a!=b); //Not equal to
Logical operators {| (or) & & (and)! (not}
boolean a = true; boolean b = false; System.out.println(a&&b); //If the second is true, it is true System.out.println(a||b); //If both are false, it is false System.out.println(!a&&b); //If true, false becomes false and true //Short circuit operation b&&c // If b is false, it will stop running directly
Bit operation
A = 0011 1100
B = 0000 1101
A & B = 00001100 / / two 1s are 1
A|B = 0011 1101 / / if there is a 1, it is 1
A^B= 0011 0001 / / 0 for the same but 1 for the different
~B= 1111 0010 / / reverse
<< // It's multiplied by two. It's very efficient >> // It's divided by two. It's very efficient 0000 0000 0 0000 0001 1 0000 0010 2 0000 0011 3 0000 0100 4
Ease of use operator (doge)
int a = 10; int b = 20; a+=b // Is a=a+b; a-=b // Is a=a-b; System.out.println(a+b); // Is int 30 System.out.println(""+a+b); //Is the string "1020" System.out.println(a+b+""); // Is "30" x ? y : z //x==true is y, otherwise z a==10 ? "equal": "Unequal"; //Will output equal