整个java存在基本数据类型和引用数据类型两种形式的数据类型:
整数类型(byte、short、int、long)
数值型{
浮点类型(float、double)
基本数据类型{ 字符型(char)
布尔型(boolean)
数据类型{
类(class)
引用数据类型{ 接口(interface)
数组
一、基本数据类型
基本数据类型本身表示的是一个个具体的数值,其本身有取值的范围。
1、数值型
数值型表示的是一个个的数字,主要分为以下两种:
1)整数类型:byte、short、int、long。
2)小数类型:float、double。
在整数类型中比较常用的类型就是int类型,直接表示一个整数。
byte类型的长度<short类型的长度<int类型的长度<long类型的长度。
数据类型 大小/位 可表示的数据范围
long(长整数) 64 -9223372036854775808~9223372036854775807
int(整数) 32 -2147483648~2147483647
short(短整数) 16 -32768~32767
byte(位) 8 -128~127
char(字符) 2 0~255
float(单精度) 32 -3.4E38(-3.4*10^38)~3.4E38(3.4*10^38)
double(双精度) 64 -1.7E308(-1.7*10^308)~1.7E308(1.7*10^308)
在使用的时候,之所以说int类型比较常用,是因为一个默认的数字的类型就是int类型。
public class TestDemo02
{
public static void main(String args[])
{
byte b =300;
}
}
以上的300是一个数字,那么只要是数字,在程序中都可以使用int类型进行表示。
而且,各个数据类型之间也可以进行转型的操作,转型的原则;位数小的类型转换成位数大的类型将自动完成,如果相反,则必须强制完成
1)byte-->int:自动完成
2)int-->byte:强制完成
public class TestDemo03
{
public static void main(String args[])
{
int x = 30;byte b = (byte)x;
}
}
在数值型中还包含了float和double类型,其中double类型的数据可以存放的内容是最多的。
一个默认的小数数字,其类型就是double类型。
public class TestDemo04
{
public static void main(String args[])
{
double x = 3333111;
System.out.println(x);
}
}
那么,如果现在使用float接收以上的数字,则就会出现问题:
public class TestDeamo05
{
public static void main(String args[])
{
float x = (float)3333111;
System.out.println(x);
}
}
那么,以上的程序,也可以使用另外一种方式完成:
public class TestDeamo06
{
public static void main(String args[])
{
float x = 3333111f;
System.out.println(x);
}
}
长整型的数据中也可以在数字后加一个“L”表示出来。
public class TestDemo07
{
public static void main(String args[])
{
long x =30L;
System.out.println(x);
}
}
面试题:请计算以下程序的结果
public class TestDemo08
{
public static void main(String args[])
{
long x =30L;
System.out.println(1L+11)
System.out.println(x);
}
}