Compare commits
2 Commits
94a5e249bc
...
f5d6faa31c
| Author | SHA1 | Date | |
|---|---|---|---|
| f5d6faa31c | |||
| 8695169cfb |
@@ -21,29 +21,37 @@ Java中的默认类型:整数类型是int 、浮点类型是double 。
|
|||||||
*/
|
*/
|
||||||
public class Demo02 {
|
public class Demo02 {
|
||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
//定义字节型变量
|
//定义字节型变量(1个字节)
|
||||||
byte b = 100;
|
byte b = -128;
|
||||||
|
b = 100;
|
||||||
System.out.println(b);
|
System.out.println(b);
|
||||||
//定义短整型变量
|
//定义短整型变量(2个字节)
|
||||||
short s = 1000;
|
short s = -32768;
|
||||||
System.out.println(s);
|
System.out.println(s);
|
||||||
//定义整型变量
|
//定义整型变量
|
||||||
int i = 123456;
|
int i = 123456;
|
||||||
System.out.println(i);
|
System.out.println(i);
|
||||||
//定义长整型变量
|
//定义长整型变量(8字节)
|
||||||
long l = 12345678900L;
|
long l = 12345678900L;
|
||||||
System.out.println(l);
|
System.out.println(l);
|
||||||
//定义单精度浮点型变量
|
//定义单精度浮点型变量
|
||||||
float f = 5.5F;
|
float f = 5.5F;
|
||||||
System.out.println(f);
|
System.out.println(f);
|
||||||
//定义双精度浮点型变量
|
//定义双精度浮点型变量
|
||||||
double d = 8.5;
|
double d = -8.5;
|
||||||
System.out.println(d);
|
System.out.println(d);
|
||||||
//定义布尔型变量
|
//定义布尔型变量
|
||||||
|
//ctrl+D:复制一行
|
||||||
boolean bool = false;
|
boolean bool = false;
|
||||||
|
boolean bool1 = true;
|
||||||
System.out.println(bool);
|
System.out.println(bool);
|
||||||
//定义字符型变量
|
//定义字符型变量
|
||||||
char c = 'A';
|
char c = ' ';
|
||||||
System.out.println(c);
|
System.out.println(c);
|
||||||
|
//注意事项:变量可以先定义后赋值的,但是使用之前必须赋值
|
||||||
|
//定义一个num的int型变量,不赋值
|
||||||
|
int num;
|
||||||
|
num = 200;
|
||||||
|
System.out.println(num);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
23
day02/src/com/inmind/Demo01.java
Normal file
23
day02/src/com/inmind/Demo01.java
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package com.inmind;
|
||||||
|
/*
|
||||||
|
自动转换:将取值范围小的类型自动提升为取值范围大的类型
|
||||||
|
*/
|
||||||
|
public class Demo01 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//定义2个int变量(4个字节)
|
||||||
|
int a = 10;
|
||||||
|
int b = 20;
|
||||||
|
|
||||||
|
//2个整数相加
|
||||||
|
int sum = a+b;
|
||||||
|
System.out.println(sum);//30
|
||||||
|
|
||||||
|
//定义一个字节型变量(1个字节)
|
||||||
|
byte b1 = 2;
|
||||||
|
int sum1 = a+b1;//先将1字节的b1,自动提升(转换)为4字节的int型,再进行计算的
|
||||||
|
System.out.println(sum1);//12
|
||||||
|
|
||||||
|
//12的整数是可以被一个byte所表示
|
||||||
|
//byte b2 = a+b1;//错误,a+b1发生了自动类型转换,计算之后是一个4字节的int型的值,是不能直接赋值给1字节的类型
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user