变量就是申请内存来存储值。也就是说,当创建变量的时候,需要在内存中申请空间。
内存管理系统根据变量的类型为变量分配存储空间,分配的空间只能用来储存该类型数据。

因此,通过定义不同类型的变量,可以在内存中储存整数、小数或者字符。
Java 的两大数据类型:
- 内置数据类型
- 引用数据类型
内置数据类型
Java语言提供了八种基本类型。六种数字类型(四个整数型,两个浮点型),一种字符类型,还有一种布尔型。
byte:
- byte 数据类型是8位、有符号的,以二进制补码表示的整数;
- 最小值是 -128(-2^7);
- 最大值是 127(2^7-1);
- 默认值是 0;
- byte 类型用在大型数组中节约空间,主要代替整数,因为 byte 变量占用的空间只有 int 类型的四分之一;
- 例子:byte a = 100,byte b = -50。
short:
- short 数据类型是 16 位、有符号的以二进制补码表示的整数
- 最小值是 -32768(-2^15);
- 最大值是 32767(2^15 – 1);
- Short 数据类型也可以像 byte 那样节省空间。一个short变量是int型变量所占空间的二分之一;
- 默认值是 0;
- 例子:short s = 1000,short r = -20000。
int:
- int 数据类型是32位、有符号的以二进制补码表示的整数;
- 最小值是 -2,147,483,648(-2^31);
- 最大值是 2,147,483,647(2^31 – 1);
- 一般地整型变量默认为 int 类型;
- 默认值是 0 ;
- 例子:int a = 100000, int b = -200000。
long:
- long 数据类型是 64 位、有符号的以二进制补码表示的整数;
- 最小值是 -9,223,372,036,854,775,808(-2^63);
- 最大值是 9,223,372,036,854,775,807(2^63 -1);
- 这种类型主要使用在需要比较大整数的系统上;
- 默认值是 0L;
- 例子: long a = 100000L,long b = -200000L。 "L"理论上不分大小写,但是若写成"l"容易与数字"1"混淆,不容易分辩。所以最好大写。
float:
- float 数据类型是单精度、32位、符合IEEE 754标准的浮点数;
- float 在储存大型浮点数组的时候可节省内存空间;
- 默认值是 0.0f;
- 浮点数不能用来表示精确的值,如货币;
- 例子:float f1 = 234.5f。
double:
- double 数据类型是双精度、64 位、符合 IEEE 754 标准的浮点数;
- 浮点数的默认类型为 double 类型;
- double类型同样不能表示精确的值,如货币;
- 默认值是 0.0d;
-
例子:
double d1 = 7D ;
double d2 = 7.;
double d3 = 8.0;
double d4 = 8.D;
double d5 = 12.9867;7 是一个 int 字面量,而 7D,7. 和 8.0 是 double 字面量。
boolean:
- boolean数据类型表示一位的信息;
- 只有两个取值:true 和 false;
- 这种类型只作为一种标志来记录 true/false 情况;
- 默认值是 false;
- 例子:boolean one = true。
char:
- char 类型是一个单一的 16 位 Unicode 字符;
- 最小值是 \\u0000(十进制等效值为 0);
- 最大值是 \\uffff(即为 65535);
- char 数据类型可以储存任何字符;
- 例子:char letter = 'A';。
实例
对于数值类型的基本类型的取值范围,我们无需强制去记忆,因为它们的值都已经以常量的形式定义在对应的包装类中了。请看下面的例子:
实例
public class PrimitiveTypeTest { public static void main(String[] args) { // byte System.out.println("基本类型:byte 二进制位数:" + Byte.SIZE); System.out.println("包装类:java.lang.Byte"); System.out.println("最小值:Byte.MIN_VALUE=" + Byte.MIN_VALUE); System.out.println("最大值:Byte.MAX_VALUE=" + Byte.MAX_VALUE); System.out.println(); // short System.out.println("基本类型:short 二进制位数:" + Short.SIZE); System.out.println("包装类:java.lang.Short"); System.out.println("最小值:Short.MIN_VALUE=" + Short.MIN_VALUE); System.out.println("最大值:Short.MAX_VALUE=" + Short.MAX_VALUE); System.out.println(); // int System.out.println("基本类型:int 二进制位数:" + Integer.SIZE); System.out.println("包装类:java.lang.Integer"); System.out.println("最小值:Integer.MIN_VALUE=" + Integer.MIN_VALUE); System.out.println("最大值:Integer.MAX_VALUE=" + Integer.MAX_VALUE); System.out.println(); // long System.out.println("基本类型:long 二进制位数:" + Long.SIZE); System.out.println("包装类:java.lang.Long"); System.out.println("最小值:Long.MIN_VALUE=" + Long.MIN_VALUE); System.out.println("最大值:Long.MAX_VALUE=" + Long.MAX_VALUE); System.out.println(); // float System.out.println("基本类型:float 二进制位数:" + Float.SIZE); System.out.println("包装类:java.lang.Float"); System.out.println("最小值:Float.MIN_VALUE=" + Float.MIN_VALUE); System.out.println("最大值:Float.MAX_VALUE=" + Float.MAX_VALUE); System.out.println(); // double System.out.println("基本类型:double 二进制位数:" + Double.SIZE); System.out.println("包装类:java.lang.Double"); System.out.println("最小值:Double.MIN_VALUE=" + Double.MIN_VALUE); System.out.println("最大值:Double.MAX_VALUE=" + Double.MAX_VALUE); System.out.println(); // char System.out.println("基本类型:char 二进制位数:" + Character.SIZE); System.out.println("包装类:java.lang.Character"); // 以数值形式而不是字符形式将Character.MIN_VALUE输出到控制台 System.out.println("最小值:Character.MIN_VALUE=" + (int) Character.MIN_VALUE); // 以数值形式而不是字符形式将Character.MAX_VALUE输出到控制台 System.out.println("最大值:Character.MAX_VALUE=" + (int) Character.MAX_VALUE); } }
运行实例 »
编译以上代码输出结果如下所示:
基本类型:byte 二进制位数:8
包装类:java.lang.Byte
最小值:Byte.MIN_VALUE=-128
最大值:Byte.MAX_VALUE=127
基本类型:short 二进制位数:16
包装类:java.lang.Short
最小值:Short.MIN_VALUE=-32768
最大值:Short.MAX_VALUE=32767
基本类型:int 二进制位数:32
包装类:java.lang.Integer
最小值:Integer.MIN_VALUE=-2147483648
最大值:Integer.MAX_VALUE=2147483647
基本类型:long 二进制位数:64
包装类:java.lang.Long
最小值:Long.MIN_VALUE=-9223372036854775808
最大值:Long.MAX_VALUE=9223372036854775807
基本类型:float 二进制位数:32
包装类:java.lang.Float
最小值:Float.MIN_VALUE=1.4E-45
最大值:Float.MAX_VALUE=3.4028235E38
基本类型:double 二进制位数:64
包装类:java.lang.Double
最小值:Double.MIN_VALUE=4.9E-324
最大值:Double.MAX_VALUE=1.7976931348623157E308
基本类型:char 二进制位数:16
包装类:java.lang.Character
最小值:Character.MIN_VALUE=0
最大值:Character.MAX_VALUE=65535
Float和Double的最小值和最大值都是以科学记数法的形式输出的,结尾的"E+数字"表示E之前的数字要乘以10的多少次方。比如3.14E3就是3.14 × 103 =3140,3.14E-3 就是 3.14 x 10-3 =0.00314。
https://ibaotu.com/tupian/388820293.html https://ibaotu.com/tupian/388820214.html https://ibaotu.com/tupian/388820143.html https://ibaotu.com/tupian/388820066.html https://ibaotu.com/tupian/388819984.html https://ibaotu.com/tupian/388819903.html https://ibaotu.com/tupian/388819834.html https://ibaotu.com/tupian/388819742.html https://ibaotu.com/tupian/388819663.html https://ibaotu.com/tupian/388819608.html https://ibaotu.com/tupian/388819529.html https://ibaotu.com/tupian/388819451.html https://ibaotu.com/tupian/388819381.html https://ibaotu.com/tupian/388819299.html https://ibaotu.com/tupian/388819228.html https://ibaotu.com/tupian/388819151.html https://ibaotu.com/tupian/388819076.html https://ibaotu.com/tupian/388818999.html https://ibaotu.com/tupian/388818931.html https://ibaotu.com/tupian/388818862.html https://ibaotu.com/tupian/388818778.html https://ibaotu.com/tupian/388818697.html https://ibaotu.com/tupian/388818629.html https://ibaotu.com/tupian/388818548.html https://ibaotu.com/tupian/388818466.html https://ibaotu.com/tupian/388818387.html https://ibaotu.com/tupian/388818306.html https://ibaotu.com/tupian/388818236.html https://ibaotu.com/tupian/388818166.html https://ibaotu.com/tupian/388818087.html https://ibaotu.com/tupian/388818018.html https://ibaotu.com/tupian/388817946.html https://ibaotu.com/tupian/388817858.html https://ibaotu.com/tupian/388817781.html https://ibaotu.com/tupian/388817717.html https://ibaotu.com/tupian/388817648.html https://ibaotu.com/tupian/388817579.html https://ibaotu.com/tupian/388817494.html https://ibaotu.com/tupian/388817418.html https://ibaotu.com/tupian/388817345.html https://ibaotu.com/tupian/388817278.html https://ibaotu.com/tupian/388817213.html https://ibaotu.com/tupian/388817146.html https://ibaotu.com/tupian/388817069.html https://ibaotu.com/tupian/388816991.html https://ibaotu.com/tupian/388816929.html https://ibaotu.com/tupian/388816853.html https://ibaotu.com/tupian/388816781.html https://ibaotu.com/tupian/388816702.html https://ibaotu.com/tupian/388816637.html https://ibaotu.com/tupian/388816575.html https://ibaotu.com/tupian/388816512.html https://ibaotu.com/tupian/388816448.html https://ibaotu.com/tupian/388816389.html https://ibaotu.com/tupian/388816320.html https://ibaotu.com/tupian/388816247.html https://ibaotu.com/tupian/388816185.html https://ibaotu.com/tupian/388816119.html https://ibaotu.com/tupian/388816042.html https://ibaotu.com/tupian/388815964.html https://ibaotu.com/tupian/388815897.html https://ibaotu.com/tupian/388815808.html https://ibaotu.com/tupian/388815679.html https://ibaotu.com/tupian/388815545.html https://ibaotu.com/tupian/388815419.html https://ibaotu.com/tupian/388815299.html https://ibaotu.com/tupian/388815191.html https://ibaotu.com/tupian/388815032.html https://ibaotu.com/tupian/388814860.html https://ibaotu.com/tupian/388814717.html https://ibaotu.com/tupian/388814570.html https://ibaotu.com/tupian/388814437.html https://ibaotu.com/tupian/388814312.html https://ibaotu.com/tupian/388814179.html https://ibaotu.com/tupian/388814031.html https://ibaotu.com/tupian/388813892.html https://ibaotu.com/tupian/388813768.html https://ibaotu.com/tupian/388813618.html https://ibaotu.com/tupian/388813473.html https://ibaotu.com/tupian/388813344.html https://ibaotu.com/tupian/388813213.html https://ibaotu.com/tupian/388813059.html https://ibaotu.com/tupian/388812916.html https://ibaotu.com/tupian/388812776.html https://ibaotu.com/tupian/388812634.html https://ibaotu.com/tupian/388812496.html https://ibaotu.com/tupian/388812381.html https://ibaotu.com/tupian/388812240.html https://ibaotu.com/tupian/388812091.html https://ibaotu.com/tupian/388811956.html https://ibaotu.com/tupian/388811812.html https://ibaotu.com/tupian/388811694.html https://ibaotu.com/tupian/388811540.html https://ibaotu.com/tupian/388811389.html https://ibaotu.com/tupian/388811235.html https://ibaotu.com/tupian/388811116.html https://ibaotu.com/tupian/388810965.html https://ibaotu.com/tupian/388810837.html https://ibaotu.com/tupian/388810704.html https://ibaotu.com/tupian/388810578.html https://ibaotu.com/tupian/388810464.html https://ibaotu.com/tupian/388810339.html https://ibaotu.com/tupian/388810204.html https://ibaotu.com/tupian/388810064.html https://ibaotu.com/tupian/388809936.html https://ibaotu.com/tupian/388809799.html https://ibaotu.com/tupian/388809655.html https://ibaotu.com/tupian/388809534.html https://ibaotu.com/tupian/388809402.html https://ibaotu.com/tupian/388809285.html https://ibaotu.com/tupian/388809164.html https://ibaotu.com/tupian/388809082.html https://ibaotu.com/tupian/388809025.html https://ibaotu.com/tupian/388808962.html https://ibaotu.com/tupian/388808898.html https://ibaotu.com/tupian/388808833.html https://ibaotu.com/tupian/388808773.html https://ibaotu.com/tupian/388808698.html https://ibaotu.com/tupian/388808630.html https://ibaotu.com/tupian/388808561.html https://ibaotu.com/tupian/388808502.html https://ibaotu.com/tupian/388808430.html https://ibaotu.com/tupian/388808372.html https://ibaotu.com/tupian/388808307.html https://ibaotu.com/tupian/388808238.html https://ibaotu.com/tupian/388808173.html https://ibaotu.com/tupian/388808114.html https://ibaotu.com/tupian/388808051.html https://ibaotu.com/tupian/388807985.html https://ibaotu.com/tupian/388807918.html https://ibaotu.com/tupian/388807844.html https://ibaotu.com/tupian/388807783.html https://ibaotu.com/tupian/388807712.html https://ibaotu.com/tupian/388807602.html https://ibaotu.com/tupian/388807454.html https://ibaotu.com/tupian/388807341.html https://ibaotu.com/tupian/388807181.html https://ibaotu.com/tupian/388807063.html https://ibaotu.com/tupian/388806922.html https://ibaotu.com/tupian/388806797.html https://ibaotu.com/tupian/388806639.html https://ibaotu.com/tupian/388806539.html https://ibaotu.com/tupian/388806412.html https://ibaotu.com/tupian/388806301.html https://ibaotu.com/tupian/388806160.html https://ibaotu.com/tupian/388806035.html https://ibaotu.com/tupian/388805895.html https://ibaotu.com/tupian/388805765.html https://ibaotu.com/tupian/388805637.html https://ibaotu.com/tupian/388805514.html https://ibaotu.com/tupian/388805360.html https://ibaotu.com/tupian/388805217.html https://ibaotu.com/tupian/388805102.html https://ibaotu.com/tupian/388804948.html https://ibaotu.com/tupian/388804829.html https://ibaotu.com/tupian/388804690.html https://ibaotu.com/tupian/388804555.html https://ibaotu.com/tupian/388804419.html https://ibaotu.com/tupian/388804291.html https://ibaotu.com/tupian/388804159.html https://ibaotu.com/tupian/388804030.html https://ibaotu.com/tupian/388803901.html https://ibaotu.com/tupian/388803785.html https://ibaotu.com/tupian/388803666.html https://ibaotu.com/tupian/388803599.html https://ibaotu.com/tupian/388803533.html https://ibaotu.com/tupian/388803469.html https://ibaotu.com/tupian/388803408.html https://ibaotu.com/tupian/388803351.html https://ibaotu.com/tupian/388803279.html https://ibaotu.com/tupian/388803216.html https://ibaotu.com/tupian/388803165.html https://ibaotu.com/tupian/388803100.html https://ibaotu.com/tupian/388803033.html https://ibaotu.com/tupian/388802976.html https://ibaotu.com/tupian/388802914.html https://ibaotu.com/tupian/388802852.html https://ibaotu.com/tupian/388802795.html https://ibaotu.com/tupian/388802734.html https://ibaotu.com/tupian/388802674.html https://ibaotu.com/tupian/388802604.html https://ibaotu.com/tupian/388802540.html https://ibaotu.com/tupian/388802484.html https://ibaotu.com/tupian/388802424.html https://ibaotu.com/tupian/388802366.html https://ibaotu.com/tupian/388802304.html https://ibaotu.com/tupian/388802244.html https://ibaotu.com/tupian/388802187.html https://ibaotu.com/tupian/388802117.html https://ibaotu.com/tupian/388802060.html https://ibaotu.com/tupian/388801992.html https://ibaotu.com/tupian/388801926.html https://ibaotu.com/tupian/388801869.html https://ibaotu.com/tupian/388801813.html https://ibaotu.com/tupian/388801738.html https://ibaotu.com/tupian/388801683.html https://ibaotu.com/tupian/388801629.html https://ibaotu.com/tupian/388801563.html https://ibaotu.com/tupian/388801496.html https://ibaotu.com/tupian/388801428.html https://ibaotu.com/tupian/388801364.html https://ibaotu.com/tupian/388801290.html https://ibaotu.com/tupian/388801240.html https://ibaotu.com/tupian/388801179.html https://ibaotu.com/tupian/388801136.html https://ibaotu.com/tupian/388801092.html https://ibaotu.com/tupian/388801043.html https://ibaotu.com/tupian/388800984.html https://ibaotu.com/tupian/388800932.html https://ibaotu.com/tupian/388800875.html https://ibaotu.com/tupian/388800777.html https://ibaotu.com/tupian/388800684.html https://ibaotu.com/tupian/388800578.html https://ibaotu.com/tupian/388800471.html https://ibaotu.com/tupian/388800361.html https://ibaotu.com/tupian/388800228.html https://ibaotu.com/tupian/388800125.html https://ibaotu.com/tupian/388800031.html https://ibaotu.com/tupian/388799920.html https://ibaotu.com/tupian/388799809.html https://ibaotu.com/tupian/388799689.html https://ibaotu.com/tupian/388799558.html https://ibaotu.com/tupian/388799464.html https://ibaotu.com/tupian/388799335.html https://ibaotu.com/tupian/388799210.html https://ibaotu.com/tupian/388799095.html https://ibaotu.com/tupian/388798972.html https://ibaotu.com/tupian/388798870.html https://ibaotu.com/tupian/388798746.html https://ibaotu.com/tupian/388798628.html https://ibaotu.com/tupian/388798507.html https://ibaotu.com/tupian/388798381.html https://ibaotu.com/tupian/388798261.html https://ibaotu.com/tupian/388798154.html https://ibaotu.com/tupian/388798029.html https://ibaotu.com/tupian/388797917.html https://ibaotu.com/tupian/388797813.html https://ibaotu.com/tupian/388797699.html https://ibaotu.com/tupian/388797575.html https://ibaotu.com/tupian/388797467.html https://ibaotu.com/tupian/388797356.html https://ibaotu.com/tupian/388797230.html https://ibaotu.com/tupian/388797106.html https://ibaotu.com/tupian/388796990.html https://ibaotu.com/tupian/388796871.html https://ibaotu.com/tupian/388796755.html https://ibaotu.com/tupian/388796629.html https://ibaotu.com/tupian/388796514.html https://ibaotu.com/tupian/388796389.html https://ibaotu.com/tupian/388796268.html https://ibaotu.com/tupian/388796139.html https://ibaotu.com/tupian/388796053.html https://ibaotu.com/tupian/388795913.html https://ibaotu.com/tupian/388795800.html https://ibaotu.com/tupian/388795689.html https://ibaotu.com/tupian/388795578.html https://ibaotu.com/tupian/388795465.html https://ibaotu.com/tupian/388795341.html https://ibaotu.com/tupian/388795179.html https://ibaotu.com/tupian/388795081.html https://ibaotu.com/tupian/388794959.html https://ibaotu.com/tupian/388794848.html https://ibaotu.com/tupian/388794717.html https://ibaotu.com/tupian/388794552.html https://ibaotu.com/tupian/388794440.html https://ibaotu.com/tupian/388794322.html https://ibaotu.com/tupian/388794197.html https://ibaotu.com/tupian/388794063.html https://ibaotu.com/tupian/388793940.html https://ibaotu.com/tupian/388793821.html https://ibaotu.com/tupian/388793678.html https://ibaotu.com/tupian/388793550.html https://ibaotu.com/tupian/388793420.html https://ibaotu.com/tupian/388793307.html https://ibaotu.com/tupian/388793187.html https://ibaotu.com/tupian/388793085.html https://ibaotu.com/tupian/388792987.html https://ibaotu.com/tupian/388792903.html https://ibaotu.com/tupian/388792841.html https://ibaotu.com/tupian/388792767.html https://ibaotu.com/tupian/388792695.html https://ibaotu.com/tupian/388792618.html https://ibaotu.com/tupian/388792520.html https://ibaotu.com/tupian/388792429.html https://ibaotu.com/tupian/388792354.html https://ibaotu.com/tupian/388792278.html https://ibaotu.com/tupian/388792201.html https://ibaotu.com/tupian/388792093.html https://ibaotu.com/tupian/388792023.html https://ibaotu.com/tupian/388791948.html https://ibaotu.com/tupian/388791872.html https://ibaotu.com/tupian/388791788.html https://ibaotu.com/tupian/388791713.html https://ibaotu.com/tupian/388791643.html https://ibaotu.com/tupian/388791552.html https://ibaotu.com/tupian/388791474.html https://ibaotu.com/tupian/388791404.html https://ibaotu.com/tupian/388791336.html https://ibaotu.com/tupian/388791246.html https://ibaotu.com/tupian/388791175.html https://ibaotu.com/tupian/388791101.html https://ibaotu.com/tupian/388791025.html https://ibaotu.com/tupian/388790951.html https://ibaotu.com/tupian/388790881.html https://ibaotu.com/tupian/388790801.html https://ibaotu.com/tupian/388790721.html https://ibaotu.com/tupian/388790647.html https://ibaotu.com/tupian/388790566.html https://ibaotu.com/tupian/388790486.html https://ibaotu.com/tupian/388790357.html https://ibaotu.com/tupian/388790289.html https://ibaotu.com/tupian/388790211.html https://ibaotu.com/tupian/388790087.html https://ibaotu.com/tupian/388789988.html https://ibaotu.com/tupian/388789898.html https://ibaotu.com/tupian/388789820.html https://ibaotu.com/tupian/388789739.html https://ibaotu.com/tupian/388789648.html https://ibaotu.com/tupian/388789572.html https://ibaotu.com/tupian/388789488.html https://ibaotu.com/tupian/388789408.html https://ibaotu.com/tupian/388789330.html https://ibaotu.com/tupian/388789257.html https://ibaotu.com/tupian/388789176.html https://ibaotu.com/tupian/388789084.html https://ibaotu.com/tupian/388789002.html https://ibaotu.com/tupian/388788911.html https://ibaotu.com/tupian/388788828.html https://ibaotu.com/tupian/388788756.html https://ibaotu.com/tupian/388788659.html https://ibaotu.com/tupian/388788564.html https://ibaotu.com/tupian/388788466.html https://ibaotu.com/tupian/388788381.html https://ibaotu.com/tupian/388788298.html https://ibaotu.com/tupian/388788204.html https://ibaotu.com/tupian/388788117.html https://ibaotu.com/tupian/388788028.html https://ibaotu.com/tupian/388787933.html https://ibaotu.com/tupian/388787837.html https://ibaotu.com/tupian/388787750.html https://ibaotu.com/tupian/388787661.html https://ibaotu.com/tupian/388787573.html https://ibaotu.com/tupian/388787481.html https://ibaotu.com/tupian/388787393.html https://ibaotu.com/tupian/388787314.html https://ibaotu.com/tupian/388787231.html https://ibaotu.com/tupian/388787143.html https://ibaotu.com/tupian/388787056.html https://ibaotu.com/tupian/388786973.html https://ibaotu.com/tupian/388786900.html https://ibaotu.com/tupian/388786816.html https://ibaotu.com/tupian/388786737.html https://ibaotu.com/tupian/388786657.html https://ibaotu.com/tupian/388786572.html https://ibaotu.com/tupian/388786487.html https://ibaotu.com/tupian/388786403.html https://ibaotu.com/tupian/388786319.html https://ibaotu.com/tupian/388786246.html https://ibaotu.com/tupian/388786171.html https://ibaotu.com/tupian/388786102.html https://ibaotu.com/tupian/388786023.html https://ibaotu.com/tupian/388785943.html https://ibaotu.com/tupian/388785850.html https://ibaotu.com/tupian/388785768.html https://ibaotu.com/tupian/388785685.html https://ibaotu.com/tupian/388785581.html https://ibaotu.com/tupian/388785499.html https://ibaotu.com/tupian/388785401.html https://ibaotu.com/tupian/388785287.html https://ibaotu.com/tupian/388785185.html https://ibaotu.com/tupian/388785092.html https://ibaotu.com/tupian/388784991.html https://ibaotu.com/tupian/388784902.html https://ibaotu.com/tupian/388784808.html https://ibaotu.com/tupian/388784706.html https://ibaotu.com/tupian/388784594.html https://ibaotu.com/tupian/388784500.html https://ibaotu.com/tupian/388784399.html https://ibaotu.com/tupian/388784298.html https://ibaotu.com/tupian/388784195.html https://ibaotu.com/tupian/388784083.html https://ibaotu.com/tupian/388783978.html https://ibaotu.com/tupian/388783870.html https://ibaotu.com/tupian/388783777.html https://ibaotu.com/tupian/388783679.html https://ibaotu.com/tupian/388783571.html https://ibaotu.com/tupian/388783469.html https://ibaotu.com/tupian/388783359.html https://ibaotu.com/tupian/388783213.html https://ibaotu.com/tupian/388783053.html https://ibaotu.com/tupian/388782906.html https://ibaotu.com/tupian/388782750.html https://ibaotu.com/tupian/388782601.html https://ibaotu.com/tupian/388782450.html https://ibaotu.com/tupian/388782288.html https://ibaotu.com/tupian/388782145.html https://ibaotu.com/tupian/388781995.html https://ibaotu.com/tupian/388781835.html https://ibaotu.com/tupian/388781687.html https://ibaotu.com/tupian/388781498.html https://ibaotu.com/tupian/388781344.html https://ibaotu.com/tupian/388781177.html https://ibaotu.com/tupian/388781030.html https://ibaotu.com/tupian/388780891.html https://ibaotu.com/tupian/388780760.html https://ibaotu.com/tupian/388780643.html https://ibaotu.com/tupian/388780535.html https://ibaotu.com/tupian/388780432.html https://ibaotu.com/tupian/388780321.html https://ibaotu.com/tupian/388780220.html https://ibaotu.com/tupian/388780104.html https://ibaotu.com/tupian/388779997.html https://ibaotu.com/tupian/388779894.html https://ibaotu.com/tupian/388779784.html https://ibaotu.com/tupian/388779684.html https://ibaotu.com/tupian/388779585.html https://ibaotu.com/tupian/388779471.html https://ibaotu.com/tupian/388779368.html https://ibaotu.com/tupian/388779264.html https://ibaotu.com/tupian/388779155.html https://ibaotu.com/tupian/388779056.html https://ibaotu.com/tupian/388778942.html https://ibaotu.com/tupian/388778854.html https://ibaotu.com/tupian/388778736.html https://ibaotu.com/tupian/388778649.html https://ibaotu.com/tupian/388778539.html https://ibaotu.com/tupian/388778438.html https://ibaotu.com/tupian/388778328.html https://ibaotu.com/tupian/388778228.html https://ibaotu.com/tupian/388778122.html https://ibaotu.com/tupian/388778019.html https://ibaotu.com/tupian/388777934.html https://ibaotu.com/tupian/388777835.html https://ibaotu.com/tupian/388777727.html https://ibaotu.com/tupian/388777634.html https://ibaotu.com/tupian/388777527.html https://ibaotu.com/tupian/388777420.html https://ibaotu.com/tupian/388777320.html https://ibaotu.com/tupian/388777215.html https://ibaotu.com/tupian/388777131.html https://ibaotu.com/tupian/388777041.html https://ibaotu.com/tupian/388776952.html https://ibaotu.com/tupian/388776865.html https://ibaotu.com/tupian/388776766.html https://ibaotu.com/tupian/388776668.html https://ibaotu.com/tupian/388776566.html https://ibaotu.com/tupian/388776461.html https://ibaotu.com/tupian/388776357.html https://ibaotu.com/tupian/388776254.html https://ibaotu.com/tupian/388776144.html https://ibaotu.com/tupian/388776045.html https://ibaotu.com/tupian/388775953.html https://ibaotu.com/tupian/388775849.html https://ibaotu.com/tupian/388775761.html https://ibaotu.com/tupian/388775659.html https://ibaotu.com/tupian/388775557.html https://ibaotu.com/tupian/388775459.html https://ibaotu.com/tupian/388775369.html https://ibaotu.com/tupian/388775259.html https://ibaotu.com/tupian/388775148.html https://ibaotu.com/tupian/388775051.html https://ibaotu.com/tupian/388774941.html https://ibaotu.com/tupian/388774846.html https://ibaotu.com/tupian/388774763.html https://ibaotu.com/tupian/388774663.html https://ibaotu.com/tupian/388774563.html https://ibaotu.com/tupian/388774469.html https://ibaotu.com/tupian/388774379.html https://ibaotu.com/tupian/388774281.html https://ibaotu.com/tupian/388774188.html https://ibaotu.com/tupian/388774092.html https://ibaotu.com/tupian/388774005.html https://ibaotu.com/tupian/388773919.html https://ibaotu.com/tupian/388773829.html https://ibaotu.com/tupian/388773754.html https://ibaotu.com/tupian/388773662.html https://ibaotu.com/tupian/388773582.html https://ibaotu.com/tupian/388773494.html https://ibaotu.com/tupian/388773398.html https://ibaotu.com/tupian/388773316.html https://ibaotu.com/tupian/388773228.html https://ibaotu.com/tupian/388773128.html https://ibaotu.com/tupian/388773051.html https://ibaotu.com/tupian/388772951.html https://ibaotu.com/tupian/388772864.html https://ibaotu.com/tupian/388772771.html https://ibaotu.com/tupian/388772669.html https://ibaotu.com/tupian/388772563.html https://ibaotu.com/tupian/388772460.html https://ibaotu.com/tupian/388772370.html https://ibaotu.com/tupian/388772278.html https://ibaotu.com/tupian/388772181.html https://ibaotu.com/tupian/388772084.html https://ibaotu.com/tupian/388771987.html https://ibaotu.com/tupian/388771884.html https://ibaotu.com/tupian/388771792.html https://ibaotu.com/tupian/388771688.html https://ibaotu.com/tupian/388771602.html https://ibaotu.com/tupian/388771513.html
实际上,JAVA中还存在另外一种基本类型 void,它也有对应的包装类 java.lang.Void,不过我们无法直接对它们进行操作。
类型默认值
下表列出了 Java 各个类型的默认值:
| int | 0 |
| long | 0L |
| short | 0 |
| char | '\\u0000' |
| byte | 0 |
| float | 0.0f |
| double | 0.0d |
| boolean | false |
| 引用类型(类、接口、数组) | null |
说明:
- int, short, long, byte 的默认值是0。
- char 的默认值是 \\u0000(空字符)。
- float 的默认值是 0.0f。
- double 的默认值是 0.0d。
- boolean 的默认值是 false。
- 引用类型(类、接口、数组)的默认值是 null。
实例
public class Test { static boolean bool; static byte by; static char ch; static double d; static float f; static int i; static long l; static short sh; static String str; public static void main(String[] args) { System.out.println("Bool :" + bool); System.out.println("Byte :" + by); System.out.println("Character:" + ch); System.out.println("Double :" + d); System.out.println("Float :" + f); System.out.println("Integer :" + i); System.out.println("Long :" + l); System.out.println("Short :" + sh); System.out.println("String :" + str); } }
实例输出结果为:
Bool :false
Byte :0
Character:
Double :0.0
Float :0.0
Integer :0
Long :0
Short :0
String :null
引用类型
- 在Java中,引用类型的变量非常类似于C/C++的指针。引用类型指向一个对象,指向对象的变量是引用变量。这些变量在声明时被指定为一个特定的类型,比如 Employee、Puppy 等。变量一旦声明后,类型就不能被改变了。
- 对象、数组都是引用数据类型。
- 所有引用类型的默认值都是null。
- 一个引用变量可以用来引用任何与之兼容的类型。
- 例子:Site site = new Site("Runoob")。
Java 常量
常量在程序运行时是不能被修改的。
在 Java 中使用 final 关键字来修饰常量,声明方式和变量类似:
final double PI = 3.1415927;
虽然常量名也可以用小写,但为了便于识别,通常使用大写字母表示常量。
字面量可以赋给任何内置类型的变量。例如:
byte a = 68;
char a = 'A'
byte、int、long、和short都可以用十进制、16进制以及8进制的方式来表示。
当使用字面量的时候,前缀 0 表示 8 进制,而前缀 0x 代表 16 进制, 例如:
int decimal = 100;
int octal = 0144;
int hexa = 0x64;
和其他语言一样,Java的字符串常量也是包含在两个引号之间的字符序列。下面是字符串型字面量的例子:
"Hello World"
"two\\nlines"
"\\"This is in quotes\\""
字符串常量和字符变量都可以包含任何 Unicode 字符。例如:
char a = '\\u0001';
String a = "\\u0001";
Java语言支持一些特殊的转义字符序列。
| \\n | 换行 (0x0a) |
| \\r | 回车 (0x0d) |
| \\f | 换页符(0x0c) |
| \\b | 退格 (0x08) |
| \\0 | 空字符 (0x0) |
| \\s | 空格 (0x20) |
| \\t | 制表符 |
| \\" | 双引号 |
| \\' | 单引号 |
| \\\\ | 反斜杠 |
| \\ddd | 八进制字符 (ddd) |
| \\uxxxx | 16进制Unicode字符 (xxxx) |
自动类型转换
整型、实型(常量)、字符型数据可以混合运算。运算中,不同类型的数据先转化为同一类型,然后进行运算。
转换从低级到高级。
低 ————————————> 高
byte,short,char—> int —> long—> float —> double
数据类型转换必须满足如下规则:
-
1. 不能对boolean类型进行类型转换。
-
2. 不能把对象类型转换成不相关类的对象。
-
3. 在把容量大的类型转换为容量小的类型时必须使用强制类型转换。
-
4. 转换过程中可能导致溢出或损失精度,例如:
int i =128;
byte b = (byte)i;因为 byte 类型是 8 位,最大值为127,所以当 int 强制转换为 byte 类型时,值 128 时候就会导致溢出。
-
5. 浮点数到整数的转换是通过舍弃小数得到,而不是四舍五入,例如:
(int)23.7 == 23;
(int)-45.89f == -45
自动类型转换
必须满足转换前的数据类型的位数要低于转换后的数据类型,例如: short数据类型的位数为16位,就可以自动转换位数为32的int类型,同样float数据类型的位数为32,可以自动转换为64位的double类型。
实例
public class ZiDongLeiZhuan{ public static void main(String[] args){ char c1='a';//定义一个char类型 int i1 = c1;//char自动类型转换为int System.out.println("char自动类型转换为int后的值等于"+i1); char c2 = 'A';//定义一个char类型 int i2 = c2+1;//char 类型和 int 类型计算 System.out.println("char类型和int计算后的值等于"+i2); } }
运行结果为:
char自动类型转换为int后的值等于97
char类型和int计算后的值等于66
解析:c1 的值为字符 a ,查 ASCII 码表可知对应的 int 类型值为 97, A 对应值为 65,所以 i2=65+1=66。
强制类型转换
-
1. 条件是转换的数据类型必须是兼容的。
-
2. 格式:(type)value type是要强制类型转换后的数据类型 实例:
实例
public class ForceTransform { public static void main(String[] args){ int i1 = 123; byte b = (byte)i1;//强制类型转换为byte System.out.println("int强制类型转换为byte后的值等于"+b); } }
运行结果:
int强制类型转换为byte后的值等于123
隐含强制类型转换
-
1、 整数的默认类型是 int。
-
2、 小数默认是 double 类型浮点型,在定义 float 类型时必须在数字后面跟上 F 或者 f。
这一节讲解了 Java 的基本数据类型。下一节将探讨不同的变量类型以及它们的用法。




