The following editor will bring you an article on the basic data types and operation methods of Java (a must-read article). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.
Encoding
ASCII--0~127 65-A 97-a
Western European code table--- ISO-8859-1---0-255---1 byte
gb2312----0-65535---gbk---2 bytes
Unicode Coding system---utf-8---3 bytes
f
bit Byte byte 1Byte = 8bit 1KB=1024B MB GB TB PB---storage unit in computer
Constant
Integer constant---all integers 3,99,107
Decimal constant---all decimals 3.5 100.9
Character constant---Use single quotation marks to identify a letter, number, or symbol 'a' '=' ' '
String constant---Use double quotation marks to identify a Or multiple characters to identify "abc" "234" "q2" ""
Boolean constant---used to represent logical values---true/false
Empty constant--- null
5-Integer, 5.0-Decimal '5'-Character "5"-String '5.0'-Wrong writing "5.0"-String
Enter System
Binary: full binary into one, 0~1 1+1=10 0b10011 0b0011, starting from JDK1.7, it is allowed to start with 0b to identify a number as a binary number
Octal system: 0 to 7, 7+1=10 must start with 0 06 015
Decimal system: 1 to 10, 0~9
Hexadecimal: Full hexadecimal one, 0~9,, A~F, 9+1=A f+1=10 requires 0x as the beginning 0x5 0xad
hexadecimal system Conversion
decimalConversionto binary: Keep dividing by 2 to take the remainder, and then put the remainder in reverse order
Conversion from binary to decimal: from the low order Starting from, multiply by the power of 2 in bit order, and then sum up
Convert binary to octal: Starting from the low order, every three digits are divided into one group, and any missing three bits are filled with 0 to produce one digit. Octal numbers, arrange these numbers in order
Convert octal to binary: one to three---one octal digit produces three binary digits
Convert binary to hexadecimal : The process of changing four into one
Variable
System.out.println(i);
int i = 5;---No---Variables must be declared before use
int i;
System.out.println(i);---No--variables must be initialized before use
Data type
Basic data type
Numeric type
Integer 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 ---[]
Data type conversion
Implicit conversion/Automatic type conversion
byte b = 100; int i = b; long l = 63;---可以---当整数的值在int类型的范围内的时候,可以不用添加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: Integers can be converted to decimals, but precision loss may occur
char c = ‘a'; int i = c;
Rule 3: Character types can be converted to integers
short s = ‘a';---可以 char c = 100;---可以 char c = ‘a'; short s = c;---不可以
The variable c of type char is defined. The stored data is a character. There is no need to check the specific character encoding. When assigning a value to the 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 this character cannot be determined. Since the value range of the short type does not completely overlap with the char type, in order to prevent the value from exceeding the range, assignment is not allowed.
short s = 97; char c = s;--不可以
Explicit conversion/casting
long l = 54; int i = (int)l; double d = 3.5; int i = (int)d;---小数强转成整数的时候,小数部分直接舍弃 double类型不能精确存储小数 Hexadecimal--十六进制 Decimal--十进制 Octal---八进制 Binary--二进制
Operator
+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. 整数除以0的时候,编译通过,运行报错---ArimeticException---算术异常
3. 小数除以0的结果是Infinity
4. 0/0.0的结果是NaN---Not a Number---非数字
5. byte/short类型在运算的时候会自动提升为int类型
%取余运算 -5%3=-2 -4%3=-1 -3%7=-3 5%-3=2 7%-2=1 2%-8=2 -5%-3=-2 -9%-3=0
对于负数的取余,先按照正数的取余运算,看取余符号左边的数字的符号,如果左边是负数,那么结果就是负数
5%1.2=0.2 6%1.3=0.8 4.3%1.4=0.1 ++/--
对于++在原来的基础上自增1
int i = 5; int j = ++i;---> i自增1,然后将i的值赋值给j---先自增,再运算 int j = i++;--->先获取i的值5,i自增变成6,然后将获取的值5赋值给j---先运算,再自增 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在底层会对结果进行强制类型转换,将结果再转换为byte类型 char c = ‘a'; System.out.println(c + 4);--可以 char c2 = ‘d'; System.out.println(c + c2);---提升为int类型之后再进行运算 + 字符串拼接运算 “a” + “b”---> “ab” “a” + 3---> “a3” “a” + true-> “atrue” 2 + 4 + “f”-> “6f” “f” + 2 + 4-> “f24”
= += -= *= /= %= &= |= ^= <<= >>= >>>= ~= int i= 5; i += 3; -> i = i + 3; -> i = 8; i -= 2;-> i = i - 2;-> i = 3; int j; j += 4;---不行 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;---可以 byte b = 125; b += 3;---可以--- -128
比较/关系运算符
==相等 !=不等 > < >= <= instanceof 3 == 4;-> false instanceof---判断对象与类的关系的--只能用于引用数据类型 String s = “abd”; System.out.println(s instanceof String);---true System.out.println(“def” instanceof String);---true
用于运算逻辑值
&与 |或 !非 ^异或 &&短路与 ||短路或 true&true=true true&false=false false&true=false false&false=false true|true=true true|false=true false|true=true false|false=false !true=false !false=true true^true=false true^false=true false^true=true false^false=false
对于&&,如果前一个表达式的值为false,那么就能确定整个表达式的值为false,&&后边的运算就不再进行了
三元/三目/条件运算符
逻辑值?表达式1:表达式2
如果逻辑值为true,执行表达式1;反之执行表达式2
int i = 5, j = 7; i > j ? System.out.println(i): System.out.println(j);---不行!三元运算符运算完成之后必须有结果!
double d = i > j ? i * 2.5 : j;---两个表达式的返回值类型要么一致,要么相容
从控制台获取数据
import java.util.Scanner; //写在package之下,class 之上 Scanner 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 Analysis of operation methods and basic data types in Java (Collection). For more information, please follow other related articles on the PHP Chinese website!