Java的基本的数据类型一共有8种
String 不是一个基础数据类型,String是一个char数组 char[]
类型 | 说明 | 范围 | 默认值 |
byte | 8比特类型 | (-128 to 127)-2^7 to 2^7-1 | 0 |
short | 16比特类型 | (–32,768 to 32,767)-2^15 to 2^15-1 | 0 |
int | 32比特类型 | (–2,147,483,648 to 2,147,483,647)-2^31 to 2^31-1 | 0 |
long | 64比特类型 | (–9,223,372,036,854,775,808 to 9 ,223,372,036,854,775,807)-2^63 to 2^63-1 | 0L |
float | 32比特类型,符合IEEE 754标准的浮点数 | (±1.4E-45 to ±3.4028235E38) | 0.0f |
double | 64比特类型,符合IEEE 754标准的浮点数 | (±4.9E-324 to ±1.7976931348623157E308) | 0.0d |
boolean | 1 比特 | 只有两种值true,false | false |
char | 16比特类型 unsigned integer | (0 -- 65535) |
'u0000' |
计算机单位换算
1 byte = 8 bits.
1 kilobyte (K / Kb) = 2^10 bytes = 1,024 bytes.
1 megabyte (M / MB) = 2^20 bytes = 1,048,576 bytes.
1 gigabyte (G / GB) = 2^30 bytes = 1,073,741,824 bytes.
写个例子来感觉下数据类型
BasicType.java
/**
java-er.com
learn java is so easy
Basic Type
*/
public class BasicType {
public static void main(String[] args){
byte b = 10; // b < 127 and b >-128
System.out.println(b);
short s = 10;
System.out.println(s);
int i = 3;
System.out.println(i);
long l = 199999999L;
System.out.println(l);
float f = 2.34f;
System.out.println(f);
double d = 3.1415926;
System.out.println(d);
boolean bx = true;
System.out.println(bx);
char c = 97; //97 equals 'a'
char c1 = 'a';
System.out.println(c);
System.out.println(c1);
}
}
命令行:
javac BasicType.java
java BasicType
10
10
3
199999999
2.34
3.1415926
true
a
a
String(字符串) 是一个常用的类型, String 类型用双引号括起来。
String s1 = "My name is ";
String s2 = new String("Jack");
String s3 = s1 + s2;
System.out.println(s3);
输出
My name is Jack