java中的各种数据类型

整数类型

数据类型占用存储空间表数范围
byte1字节=8bit-128~127
short2字节-215~215-1
int4字节-231~231-1
long8字节-263~263-1

赋值表示:
byte b = 126;
short s = 1;
int i = 1;
long l = 3l;(这个long类型变量赋值时要在值的后面跟上一个字母l,3表示数值大小,l表示长整型,输出时只显示3,大可放心)

浮点类型

数据类型占用存储空间表数范围精度
单精度float4字节-2128~21287位有效数字
双精度double8字节-21024~2102416位有效数字

赋值表示:
float f = 1.22f;(值后面跟上字母f)
double d = 1.22;

字符类型:char
char c = ‘a’;
char c = ‘1’;
char c = ‘$’;
转义字符

转义字符说明
\b退格符
\n换行符
\r回车符
\t制表符
\"双引号
\'单引号
\\反斜线

例如:
char c = ‘\n’;(换行)
char c = ‘’’;(单引号)

布尔类型:boolean
boolean b1 = true;
boolean b2 = false
只允许取值true和false,无null;
boolean类型适于逻辑运算,一般用于程序流程控制:if、while、do-while、for

引用类型
凡是引用类型,都可以用null作为值,也就是可以在初始化的时候赋值为null。
String类是引用类型,也就是可以使用null作为值。
String str1 = “hello”+“world”;

public class Test{
	public static void main(String[] args){
	
	byte b = 1;
	System.out.println(b);
	
	short s = 2;
	System.out.println(s);
	
	int i = 4;
	System.out.println(i);
	
	long l = 23l;
	System.out.println(l);
	
	float f = 1.22f;
	System.out.println(f);
	
	double d = 1.45;
	System.out.println(d);
	
	char c = 'a';
	System.out.println(c);
	
	boolean b1 = true;
	System.out.println(b1);
	
	String str1 = "hello"+"world";
	System.out.println(str1);
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值