3.变量相关
3.1 知识点
Java中的有符号整型
static byte byteVale;
static short shortValue;
static int intValue;
static long longValue;
// byte(1个字节,8位,-2^7 ~ 2^7 - 1,-128 ~ 127)
System.out.println("byte位数" + Byte.SIZE); // byte位数8
System.out.println("byte最大值" + Byte.MAX_VALUE); // byte最大值127
System.out.println("byte最小值" + Byte.MIN_VALUE); // byte最小值-128
// short(2个字节,16位,-2^15 ~ 2^15 - 1,-32768 ~ 32767)
System.out.println("short位数" + Short.SIZE); // short位数16
System.out.println("short最大值" + Short.MAX_VALUE); // short最大值32767
System.out.println("short最小值" + Short.MIN_VALUE); // short最小值-32768
// int(4个字节,32位,-2^31 ~ 2^31 - 1,-2,147,483,648 ~ 2,147,483,647)
System.out.println("int位数" + Integer.SIZE); // int位数32
System.out.println("int最大值" + Integer.MAX_VALUE); // int最大值2147483647
System.out.println("int最小值" + Integer.MIN_VALUE); // int最小值-2147483648
// long(8个字节,64位,-2^63 ~ 2^63 - 1,9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807)
System.out.println("long位数" + Long.SIZE); // long位数64
System.out.println("long最大值" + Long.MAX_VALUE); // long最大值9223372036854775807
System.out.println("long最小值" + Long.MIN_VALUE); // long最小值-9223372036854775808
// 注意:Java最初设计中没有无符号类型
// Java 8中添加了一些和无符号类型有关的一些方法(但是一般使用较少)
// 默认值
System.out.println(byteVale); // 0
System.out.println(shortValue); // 0
System.out.println(intValue); // 0
System.out.println(longValue); // 0
Java中的浮点型
// float(4个字节,32位)
System.out.println("float位数" + Float.SIZE); // float位数32
System.out.println("float最大值" + Float.MAX_VALUE); // float最大值3.4028235E38
System.out.println("float最小值" + Float.MIN_VALUE); // float最小值1.4E-45
float f = .5f;
System.out.println(f); // 0.5
f = 1.f;
System.out.println(f); // 1.0
// double(8个字节,64位)
System.out.println("double位数" + Double.SIZE); // double位数64
System.out.println("double最大值" + Double.MAX_VALUE); // double最大值1.7976931348623157E308
System.out.println("double最小值" + Double.MIN_VALUE); // double最小值4.9E-324
double d = 1.5;
Java中的其它类型
// boolean(单个boolean在编译时使用int类型,这时4个字节;boolean数组时,编译时用字节数组,所以占1个字节)
// 默认值是false
boolean b0 = true;
boolean[] bs;
// char(2个字节,16位)
System.out.println("char位数" + Character.SIZE); // char位数16
char c = 'A';//注意要单引号 双引号报错
System.out.println(c); // A
// String(根据字符串长度而定)
String str = "123";//String类一定要大写
System.out.println(str); // 123
Java中的常量
static final int FINAL_VALUE = 19;
// 关键字:final
// 特点:必须初始化,不能被修改 Java常量要手动声明静态 默认不是静态的
// FINAL_VALUE = 199;//报错 不能被修改
类型转换
// 隐式转换:低级(字节少的)到高级(字节多的)
// 记住一句话 大容器 可以装 小容器 不需要我们去处理
// byte——>short——>char——>int——>long——>float——>double
byte b2 = 111;
short s2 = b2;
// 显示转换:括号强转,高级到低级会丢失精度或者报错
int i3 = 40000;
s2 = (short) i3;
System.out.println(s2); // -25536 丢失精度
float f2 = 2.5f;
i3 = (int) f2;
System.out.println(i3); // 2
// 字符串转数值 包装类中的parse相关方法
i3 = Integer.parseInt("101", 2);//第二个参数代表转成2进制数
System.out.println(i3); // 5
总结
Java的变量类型和C#非常的类似,其中需要注意的是:
- Java中没有专门的无符号类型。
- Java中的常量关键字和C#不一样。
- 一些关键字和方法写法不同。
其它的变量相关操作表现和C#中极其相似。
3.2 知识点代码
public class Lesson03_变量相关
{
static byte byteVale;
static short shortValue;
static int intValue;
static long longValue;
static final int FINAL_VALUE = 19;
public static void main(String[] args)
{
//知识点1:Java中的有符号整型
//1.byte(1个字节,8位,-2^7 ~ 2^7 - 1,-128 ~ 127)
System.out.println("byte位数" + Byte.SIZE); // byte位数8
System.out.println("byte最大值" + Byte.MAX_VALUE); // byte最大值127
System.out.println("byte最小值" + Byte.MIN_VALUE); // byte最小值-128
//2.short(2个字节,16位,-2^15 ~ 2^15 - 1,-32768 ~ 32767)
System.out.println("short位数" + Short.SIZE); // short位数16
System.out.println("short最大值" + Short.MAX_VALUE); // short最大值32767
System.out.println("short最小值" + Short.MIN_VALUE); // short最小值-32768
//3.int(4个字节,32位,-2^31 ~ 2^31 - 1,-2,147,483,648 ~ 2,147,483,647)
System.out.println("int位数" + Integer.SIZE); // int位数32
System.out.println("int最大值" + Integer.MAX_VALUE); // int最大值2147483647
System.out.println("int最小值" + Integer.MIN_VALUE); // int最小值-2147483648
//4.long(8个字节,64位,-2^63 ~ 2^63 - 1,9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807)
System.out.println("long位数" + Long.SIZE); // long位数64
System.out.println("long最大值" + Long.MAX_VALUE); // long最大值9223372036854775807
System.out.println("long最小值" + Long.MIN_VALUE); // long最小值-9223372036854775808
//注意:Java最初设计中没有无符号类型
//Java 8中添加了一些和无符号类型有关的一些方法(但是一般使用较少)
//5.默认值
System.out.println(byteVale); // 0
System.out.println(shortValue); // 0
System.out.println(intValue); // 0
System.out.println(longValue); // 0
//知识点2:Java中的浮点型
//1.float(4个字节,32位)
System.out.println("float位数" + Float.SIZE); // float位数32
System.out.println("float最大值" + Float.MAX_VALUE); // float最大值3.4028235E38
System.out.println("float最小值" + Float.MIN_VALUE); // float最小值1.4E-45
float f = .5f;
System.out.println(f); // 0.5
f = 1.f;
System.out.println(f); // 1.0
//2.double(8个字节,64位)
System.out.println("double位数" + Double.SIZE); // double位数64
System.out.println("double最大值" + Double.MAX_VALUE); // double最大值1.7976931348623157E308
System.out.println("double最小值" + Double.MIN_VALUE); // double最小值4.9E-324
double d = 1.5;
//知识点3:Java中的其它类型
//1.boolean(单个boolean在编译时使用int类型,这时4个字节;boolean数组时,编译时用字节数组,所以占1个字节)
//默认值是false
boolean b0 = true;
boolean[] bs;
//2.char(2个字节,16位)
System.out.println("char位数" + Character.SIZE); // char位数16
char c = 'A';//注意要单引号 双引号报错
System.out.println(c); // A
//3.String(根据字符串长度而定)
String str = "123";//String类一定要大写
System.out.println(str); // 123
//知识点4:Java中的常量
//关键字:final
//特点:必须初始化,不能被修改 Java常量要手动声明静态 默认不是静态的
//FINAL_VALUE = 199;//报错 不能被修改
//知识点5:类型转换
//1.隐式转换:低级(字节少的)到高级(字节多的)
//记住一句话 大容器 可以装 小容器 不需要我们去处理
//byte——>short——>char——>int——>long——>float——>double
byte b2 = 111;
short s2 = b2;
//2.显示转换:括号强转,高级到低级会丢失精度或者报错
int i3 = 40000;
s2 = (short) i3;
System.out.println(s2); // -25536 丢失精度
float f2 = 2.5f;
i3 = (int) f2;
System.out.println(i3); // 2
//3.字符串转数值 包装类中的parse相关方法
i3 = Integer.parseInt("101", 2);//第二个参数代表转成2进制数
System.out.println(i3); // 5
//总结
//Java的变量类型和C#非常的类似
//其中需要注意的是
//1.Java中没有专门的无符号类型
//2.Java中的常量关键字和C#不一样
//3.一些关键字和方法写法不同
//其它的变量相关操作表现和C#中极奇相似
}
}
3.3 练习题
请说出Java中的9种常用变量类型
整型
byte、short、int、long
浮点型
float、double
其它
char、String、boolean
Java中如何将2进制字符串”10011”转换为int类型?
int i = Integer.parseInt("10011", 2);
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com