class IdentifierTest{
public static void main(String[] args){
// 变量的定义
int myNumber = 1001;
// 变量的使用
System.out.println(myNumber);
// 变量的声明
int my2Number;
// 变量的赋值
my2Number = 1002;
System.out.println(my2Number);
}
}
整型: byte, short, int, long
浮点型: float, double
字符型: char
布尔型: boolean
类(class)
接口(interface)
数组(array)
类型 | 占用存储空间 | 表数范围 |
---|---|---|
byte | 1字节=8bit位 | -128~127 |
short | 2字节 | -2^15 ~ 2^15-1 |
int | 4字节 | -2^31 ~ 2^31-1(约21亿) |
long | 8字节 | -2^63 ~ 2^63-1 |
class VariableTest {
public static void main(String[] args){
byte b1 = 12;
byte b2 = -128;
System.out.println(b1);
System.out.println(b2);
short s1 = 128;
int i1 = 1234;
long l1 = 1234567L;
System.out.println(s1);
System.out.println(i1);
System.out.println(l1);
}
}
浮点型常量有两种表示方式:
类型 | 占用存储空间 | 表数范围 |
---|---|---|
单精度 float | 4字节 | -3.403E38~3.403E38 |
双精度 double | 8字节 | -1.798E308~1.798E308 |
class VariableTest {
public static void main(String[] args){
// 浮点型:float(4字节), double (8字节)
// 浮点型,表示带小数点的数值
// float 表示数值的范围比 long 还大
double d1 = 123.3;
System.out.println(d1 + 1);
// 定义 float 类型时,需要以 "f" 或 "F" 结尾
float f1 = 12.3F;
System.out.println(f1);
// 通常定义浮点型变量时,使用 double 型
}
}
class VariableTest {
public static void main(String[] args){
// 字符型: char(1个字符=2个字节)
// 定义 char 型变量,通常使用一对 ‘‘
char c1 = ‘a‘;
char c2 = ‘中‘;
System.out.println(c1);
System.out.println(c2);
// 声明一个转义字符
char c3 = ‘\n‘;
System.out.print("Hello" + c3);
System.out.println("World");
char c4 = ‘\u0043‘;
System.out.println(c4);
}
}
boolean 型只能取两个值之一: true, false
常常在条件判断,循环结构中使用
class VariableTest {
public static void main(String[] args){
boolean bb1 = true;
System.out.println(bb1);
boolean isMarried = true;
if(isMarried){
System.out.println("Good");
} else {
System.out.println("Not bad");
}
}
}
class VariableTest4 {
public static void main(String[] args) {
String s1 = "Hello World!";
System.out.println(s1); // Hello World!
}
}
class StringTest{
public static void main(String[] args){
char c = ‘a‘;
int num = 10;
String str = "hello";
System.out.println(c + num + str); //107hello
System.out.println(c + str + num); //ahello10
System.out.println(c + (num + str)); //a10hello
System.out.println(str + num + c); //hello10a
System.out.println("* *"); //* *
System.out.println(‘*‘ + ‘\t‘ + ‘*‘); //93
System.out.println(‘*‘ + "\t" + ‘*‘); //* *
System.out.println(‘*‘ + ‘\t‘ + "*"); //51*
System.out.println(‘*‘ + (‘\t‘ + "*")); //* *
}
}
原文:https://www.cnblogs.com/klvchen/p/14149253.html