Home >Java >javaTutorial >Java Basics Explained-Basic Data Types and Operations
Encoding
ASCII--0~127 65-A 97-a
Western European code table---ISO-8859-1---0-255---1 character Section
gb2312----0-65535---gbk---2 bytes
Unicode encoding system---utf-8---3 bytes
中 f
bit Byte 1Byte = 8bit 1KB=1024B MB GB TB PB---Storage unit in computer
Integer constant-- -All integers 3,99,107
Decimal constants---All decimals 3.5 100.9
Character constants---Use single quotes to identify a letter, number, or symbol 'a' ' =' ' '
String constant---Use double quotes to identify one or more characters "abc" "234" "q2" ""
Boolean constant---Use Indicates logical value---true/false
Empty constant---null
5-integer, 5.0-decimal '5'-character "5"-string '5.0'- Writing error "5.0"-String
Octal: full eights into one, 0~7, 7+1=10 It is required to start with 0 06 015
Decimal: full tenths One, 0~9
Hexadecimal: full hexadecimal one, 0~9,, A~F, 9+1=A f+1=10 It is required to start with 0x 0x5 0xad
Conversion from decimal to binary
Convert from binary to decimal: from the low bit Starting from the bit order, multiply the bit order by the power of 2, and then sum up
Convert binary to octal: starting from the low order, every three digits are divided into one group, less than three Filling the bits with 0 produces one octal number. Arrange these numbers in order to convert octal to binary: one to three --- one octal number produces three binary digits.
Convert binary to hexadecimal: the process of converting four into one
Variable
int i = 5;---No---The variable must be declared before use
int i;
System.out.println(i);---No--The variable is in use Must be initialized before
Data type
Basic data type
byte---Byte type ---1 byte--- -2^7~2^7-1 --- -128~127
byte b = 5; byte b2 = -128;
short---short integer---2 bytes--- -2^15~2^15-1 --- -32768~32767
short s = 54; short s = -900 ;
int---integer---4 bytes--- -2^31~2^31-1
int i = 100000;
int j = 100_000_000;--It is allowed starting from JDK1.7. These will be automatically ignored during compilation_ -> int j = 100000000;
int i = 00001111;---Octal
The default type of integer in Java is int
long---long integer type---8 bytes--- -2^63~2^63-1---ending with L indicates that this number is a long type number
long l = 3L;
Floating point type
float---single precision---4 bytes---must end with f
float f = 3.2f;
double---double precision---8 bytes
The default decimal type in Java is double type
double d = 3.5;
double d = 4.6D;---Yes
double d = 3.9e4; //It is scientific notation in decimal system
double d = 0x3p2; //It is hexadecimal Scientific notation -> 12
Character type
char---2 bytes--- 0 ~65535
char c = 'a';
char c = '中';
Boolean
boolean---true/false
boolean b = false;
Reference data type
Class ---class Interface ---interface Array ---[]
Implicit conversion/automatic type conversion
int i = b;
long l = 63;---Yes---when the integer value is within the range of int type , you don’t need to add the ending L
Rule 1: Small types can be converted into large types---byte->short->int->long float->double
int i = 5;
float f = i;
long l = 6;
float f = l;
Rule 2: Integer Can be converted to decimal, but precision loss may occur
char c = 'a';
int i = c;
Rule 3: Character type can be converted to integer type
short s = 'a';---Yes
char c = 100;---Yes
char c = 'a' ;
short s = c;---Not possible
defines a variable c of type char. The stored data is a character. There is no need to check the specific character encoding. When assigning a value to short type, short needs to check whether the encoding corresponding to the character is within the value range of the short type. At this time, the specific encoding corresponding to the character cannot be determined. Since the value range of the short type does not completely overlap with the char type, in order to prevent If it exceeds the range, assignment is not allowed.
short s = 97;
char c = s;--not possible
Explicit conversion/forced type Conversion
long l = 54;
int i = (int)l;
double d = 3.5;
int i = (int)d ;---When converting a decimal to an integer, the decimal part is discarded directly
double type cannot store decimals accurately
Hexadecimal--Hexadecimal
Decimal-- Decimal
Octal---Octal
Binary--Binary
+addition-subtraction*multiplication/division% modulo++auto-increment--auto-decrement+string concatenation
int i = 5210 / 1000 * 1000;--->i = 5000;
Note:
1. After the integer operation is completed, the result must be an integer
2. Integer When dividing by 0, the compilation passes and the error is reported when running---ArimeticException---Arithmetic exception
3. The result of dividing a decimal by 0 is Infinity
4. The result of 0/0.0 is NaN---Not a Number---Not a number
5. The byte/short type will be automatically promoted to the int type during operation
%Remainder operation
-5%3=-2 -4%3=-1 -3%7=-3
5%-3=2 7%-2=1 2%-8= 2
-5%-3=-2 -9%-3=0
For the remainder of a negative number, first follow the remainder operation of a positive number and look at the number to the left of the remainder sign The sign of #++/--
For ++, increment by 1 on the original basis
int i = 5;
int j = ++i;--- > i increments by 1, and then assigns the value of i to j---increments first, and then operates
int j = i++;--->Get the value of i first, 5, and increments i becomes 6, and then assign the obtained value 5 to j---operate first, then increment
int i = 3;
int j = ++i * 2;-> ; j = 8;
int j = i++ * 2;->j = 6
int i = 6;
int j = i++ + ++i;->i = 8; j = 14;
int j = ++i + i++;->i = 8; j = 14
byte b = 5;
b++;---JVM will perform forced type conversion on the result at the bottom level, and then convert the result into byte type
char c = 'a';
System.out.println(c + 4);--can
char c2 = 'd';
System.out. println(c + c2);---Promote to int type and then perform operation
+ String concatenation operation
"a" + "b"-- -> "ab"
"a" + 3---> "a3"
"a" + true-> "atrue"
2 + 4 + “f”-> “6f”
“f” + 2 + 4-> “f24”
Assignment operator
= += -= *= /= %= &= |= ^= <<= >>= >>>= ~=
int i= 5;
i + = 3; -> i = i + 3; -> i = 8;
int j;
j += 4;---No
int i = 5;
i += i -= i *= 5;--> i = -15;
i = 5 + ( 5 - (5 * 5)) ;
i += i -= i *= ++ i;--->i = -20;
i += i*= i-= (i++ + --i);---> i = -20;
i = 5 + ( 5 * (5 - (5 + 5)));
byte b = 5;
b += 3;--- Yes
byte b = 125;
b += 3;---Yes--- -128
Comparison/relational operator
= =Equal!=Not equal> < >= <= instanceof
##3 == 4;-> false instanceof---Judges the relationship between objects and classes- -Can only be used for reference data typesString s = “abd”;System.out.println(s instanceof String);---trueSystem. out.println(“def” instanceof String);---trueLogical operator is used to operate logical values&AND|OR! NOT^XOR && short circuit with || short circuit or true&true=true true&false=false false&true=false false&false=falsetrue|true=true true|false=true false|true=true false|false= false!true=false !false=truetrue^true=false true^false=true false^true=true false^false=falseFor &&, If the value of the previous expression is false, then it can be determined that the value of the entire expression is false, and the operation after && will no longer be performed.ternary/ternary/conditional operatorLogical value? Expression 1: Expression 2If the logical value is true, execute expression 1; otherwise, execute expression 2int i = 5, j = 7;i > j ? System.out.println(i): System.out.println(j);---No! There must be a result after the ternary operator operation is completed! double d = i > j ? i * 2.5 : j;---The return value types of the two expressions are either consistent or compatibleGet data from the consoleimport java.util.Scanner; //Written under package, above classScanner s = new Scanner(System.in);int i = s. nextInt();double d = s.nextDouble();String str = s.nextLine();String str2 = s.next();The above is the detailed content of Java Basics Explained-Basic Data Types and Operations. For more information, please follow other related articles on the PHP Chinese website!