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

因此,通过定义不同类型的变量,可以在内存中储存整数、小数或者字符。
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/477836449.html https://ibaotu.com/tupian/477836447.html https://ibaotu.com/tupian/477836444.html https://ibaotu.com/tupian/477836442.html https://ibaotu.com/tupian/477836441.html https://ibaotu.com/tupian/477836443.html https://ibaotu.com/tupian/477836440.html https://ibaotu.com/tupian/477836412.html https://ibaotu.com/tupian/477836411.html https://ibaotu.com/tupian/477836407.html https://ibaotu.com/tupian/477836404.html https://ibaotu.com/tupian/477836405.html https://ibaotu.com/tupian/477836398.html https://ibaotu.com/tupian/477836396.html https://ibaotu.com/tupian/477836392.html https://ibaotu.com/tupian/477836387.html https://ibaotu.com/tupian/477836353.html https://ibaotu.com/tupian/477836340.html https://ibaotu.com/tupian/477836337.html https://ibaotu.com/tupian/477836341.html https://ibaotu.com/tupian/477836339.html https://ibaotu.com/tupian/477836352.html https://ibaotu.com/tupian/477836336.html https://ibaotu.com/tupian/477836348.html https://ibaotu.com/tupian/477836347.html https://ibaotu.com/tupian/477836346.html https://ibaotu.com/tupian/477836345.html https://ibaotu.com/tupian/477836329.html https://ibaotu.com/tupian/477836328.html https://ibaotu.com/tupian/477836325.html https://ibaotu.com/tupian/477836323.html https://ibaotu.com/tupian/477836322.html https://ibaotu.com/tupian/477836318.html https://ibaotu.com/tupian/477836316.html https://ibaotu.com/tupian/477836315.html https://ibaotu.com/tupian/477836292.html https://ibaotu.com/tupian/477836280.html https://ibaotu.com/tupian/477836279.html https://ibaotu.com/tupian/477836267.html https://ibaotu.com/tupian/477836266.html https://ibaotu.com/tupian/477836265.html https://ibaotu.com/tupian/477836264.html https://ibaotu.com/tupian/477836262.html https://ibaotu.com/tupian/477836263.html https://ibaotu.com/tupian/477836261.html https://ibaotu.com/tupian/477836260.html https://ibaotu.com/tupian/477836258.html https://ibaotu.com/tupian/477836256.html https://ibaotu.com/tupian/477836254.html https://ibaotu.com/tupian/477836253.html https://ibaotu.com/tupian/477836250.html https://ibaotu.com/tupian/477836249.html https://ibaotu.com/tupian/477836247.html https://ibaotu.com/tupian/477836246.html https://ibaotu.com/tupian/477836245.html https://ibaotu.com/tupian/477836239.html https://ibaotu.com/tupian/477836219.html https://ibaotu.com/tupian/477836218.html https://ibaotu.com/tupian/477836217.html https://ibaotu.com/tupian/477836215.html https://ibaotu.com/tupian/477836216.html https://ibaotu.com/tupian/477836214.html https://ibaotu.com/tupian/477836213.html https://ibaotu.com/tupian/477836212.html https://ibaotu.com/tupian/477836211.html https://ibaotu.com/tupian/477836210.html https://ibaotu.com/tupian/477836209.html https://ibaotu.com/tupian/477836206.html https://ibaotu.com/tupian/477836205.html https://ibaotu.com/tupian/477836204.html https://ibaotu.com/tupian/477836200.html https://ibaotu.com/tupian/477836170.html https://ibaotu.com/tupian/477836161.html https://ibaotu.com/tupian/477836160.html https://ibaotu.com/tupian/477836159.html https://ibaotu.com/tupian/477836153.html https://ibaotu.com/tupian/477836144.html https://ibaotu.com/tupian/477836143.html https://ibaotu.com/tupian/477836141.html https://ibaotu.com/tupian/477836140.html https://ibaotu.com/tupian/477836139.html https://ibaotu.com/tupian/477836137.html https://ibaotu.com/tupian/477836132.html https://ibaotu.com/tupian/477836131.html https://ibaotu.com/tupian/477836130.html https://ibaotu.com/tupian/477836129.html https://ibaotu.com/tupian/477836125.html https://ibaotu.com/tupian/477836123.html https://ibaotu.com/tupian/477836121.html https://ibaotu.com/tupian/477836120.html https://ibaotu.com/tupian/477836117.html https://ibaotu.com/tupian/477836116.html https://ibaotu.com/tupian/477836113.html https://ibaotu.com/tupian/477836112.html https://ibaotu.com/tupian/477836110.html https://ibaotu.com/tupian/477836092.html https://ibaotu.com/tupian/477836075.html https://ibaotu.com/tupian/477836074.html https://ibaotu.com/tupian/477836073.html https://ibaotu.com/tupian/477836062.html https://ibaotu.com/tupian/477836060.html https://ibaotu.com/tupian/477836058.html https://ibaotu.com/tupian/477836056.html https://ibaotu.com/tupian/477836057.html https://ibaotu.com/tupian/477836053.html https://ibaotu.com/tupian/477836049.html https://ibaotu.com/tupian/477836050.html https://ibaotu.com/tupian/477836048.html https://ibaotu.com/tupian/477836047.html https://ibaotu.com/tupian/477836044.html https://ibaotu.com/tupian/477836043.html https://ibaotu.com/tupian/477836041.html https://ibaotu.com/tupian/477836039.html https://ibaotu.com/tupian/477836038.html https://ibaotu.com/tupian/477836037.html https://ibaotu.com/tupian/477836032.html https://ibaotu.com/tupian/477836006.html https://ibaotu.com/tupian/477836005.html https://ibaotu.com/tupian/477836004.html https://ibaotu.com/tupian/477836002.html https://ibaotu.com/tupian/477836000.html https://ibaotu.com/tupian/477835998.html https://ibaotu.com/tupian/477835999.html https://ibaotu.com/tupian/477835995.html https://ibaotu.com/tupian/477835992.html https://ibaotu.com/tupian/477835991.html https://ibaotu.com/tupian/477835990.html https://ibaotu.com/tupian/477835988.html https://ibaotu.com/tupian/477835987.html https://ibaotu.com/tupian/477835963.html https://ibaotu.com/tupian/477835960.html https://ibaotu.com/tupian/477835951.html https://ibaotu.com/tupian/477835946.html https://ibaotu.com/tupian/477835948.html https://ibaotu.com/tupian/477835949.html https://ibaotu.com/tupian/477835941.html https://ibaotu.com/tupian/477835926.html https://ibaotu.com/tupian/477835924.html https://ibaotu.com/tupian/477835925.html https://ibaotu.com/tupian/477835923.html https://ibaotu.com/tupian/477835921.html https://ibaotu.com/tupian/477835920.html https://ibaotu.com/tupian/477835919.html https://ibaotu.com/tupian/477835909.html https://ibaotu.com/tupian/477835908.html https://ibaotu.com/tupian/477835907.html https://ibaotu.com/tupian/477835906.html https://ibaotu.com/tupian/477835904.html https://ibaotu.com/tupian/477835903.html https://ibaotu.com/tupian/477835900.html https://ibaotu.com/tupian/477835898.html https://ibaotu.com/tupian/477835897.html https://ibaotu.com/tupian/477835896.html https://ibaotu.com/tupian/477835895.html https://ibaotu.com/tupian/477835894.html https://ibaotu.com/tupian/477835868.html https://ibaotu.com/tupian/477835862.html https://ibaotu.com/tupian/477835861.html https://ibaotu.com/tupian/477835860.html https://ibaotu.com/tupian/477835859.html https://ibaotu.com/tupian/477835856.html https://ibaotu.com/tupian/477835855.html https://ibaotu.com/tupian/477835853.html https://ibaotu.com/tupian/477835839.html https://ibaotu.com/tupian/477835838.html https://ibaotu.com/tupian/477835837.html https://ibaotu.com/tupian/477835836.html https://ibaotu.com/tupian/477835833.html https://ibaotu.com/tupian/477835834.html https://ibaotu.com/tupian/477835832.html https://ibaotu.com/tupian/477835826.html https://ibaotu.com/tupian/477835825.html https://ibaotu.com/tupian/477835822.html https://ibaotu.com/tupian/477835821.html https://ibaotu.com/tupian/477835820.html https://ibaotu.com/tupian/477835815.html https://ibaotu.com/tupian/477835798.html https://ibaotu.com/tupian/477835796.html https://ibaotu.com/tupian/477835797.html https://ibaotu.com/tupian/477835795.html https://ibaotu.com/tupian/477835793.html https://ibaotu.com/tupian/477835792.html https://ibaotu.com/tupian/477835790.html https://ibaotu.com/tupian/477835787.html https://ibaotu.com/tupian/477835786.html https://ibaotu.com/tupian/477835785.html https://ibaotu.com/tupian/477835782.html https://ibaotu.com/tupian/477835779.html https://ibaotu.com/tupian/477835780.html https://ibaotu.com/tupian/477835778.html https://ibaotu.com/tupian/477835775.html https://ibaotu.com/tupian/477835776.html https://ibaotu.com/tupian/477835774.html https://ibaotu.com/tupian/477835772.html https://ibaotu.com/tupian/477835773.html https://ibaotu.com/tupian/477835740.html https://ibaotu.com/tupian/477835724.html https://ibaotu.com/tupian/477835723.html https://ibaotu.com/tupian/477835722.html https://ibaotu.com/tupian/477835721.html https://ibaotu.com/tupian/477835720.html https://ibaotu.com/tupian/477835718.html https://ibaotu.com/tupian/477835716.html https://ibaotu.com/tupian/477835712.html https://ibaotu.com/tupian/477835711.html https://ibaotu.com/tupian/477835714.html https://ibaotu.com/tupian/477835707.html https://ibaotu.com/tupian/477835706.html https://ibaotu.com/tupian/477835705.html https://ibaotu.com/tupian/477835702.html https://ibaotu.com/tupian/477835700.html https://ibaotu.com/tupian/477835697.html https://ibaotu.com/tupian/477835698.html https://ibaotu.com/tupian/477835696.html https://ibaotu.com/tupian/477835694.html https://ibaotu.com/tupian/477835689.html https://ibaotu.com/tupian/477835665.html https://ibaotu.com/tupian/477835663.html https://ibaotu.com/tupian/477835661.html https://ibaotu.com/tupian/477835660.html https://ibaotu.com/tupian/477835657.html https://ibaotu.com/tupian/477835656.html https://ibaotu.com/tupian/477835653.html https://ibaotu.com/tupian/477835637.html https://ibaotu.com/tupian/477835636.html https://ibaotu.com/tupian/477835635.html https://ibaotu.com/tupian/477835633.html https://ibaotu.com/tupian/477835632.html https://ibaotu.com/tupian/477835629.html https://ibaotu.com/tupian/477835630.html https://ibaotu.com/tupian/477835627.html https://ibaotu.com/tupian/477835615.html https://ibaotu.com/tupian/477835625.html https://ibaotu.com/tupian/477835614.html https://ibaotu.com/tupian/477835623.html https://ibaotu.com/tupian/477835620.html https://ibaotu.com/tupian/477835611.html https://ibaotu.com/tupian/477835605.html https://ibaotu.com/tupian/477835602.html https://ibaotu.com/tupian/477835601.html https://ibaotu.com/tupian/477835599.html https://ibaotu.com/tupian/477835598.html https://ibaotu.com/tupian/477835596.html https://ibaotu.com/tupian/477835594.html https://ibaotu.com/tupian/477835593.html https://ibaotu.com/tupian/477835592.html https://ibaotu.com/tupian/477835590.html https://ibaotu.com/tupian/477835589.html https://ibaotu.com/tupian/477835586.html https://ibaotu.com/tupian/477835585.html https://ibaotu.com/tupian/477835584.html https://ibaotu.com/tupian/477835573.html https://ibaotu.com/tupian/477835540.html https://ibaotu.com/tupian/477835533.html https://ibaotu.com/tupian/477835532.html https://ibaotu.com/tupian/477835531.html https://ibaotu.com/tupian/477835530.html https://ibaotu.com/tupian/477835529.html https://ibaotu.com/tupian/477835527.html https://ibaotu.com/tupian/477835526.html https://ibaotu.com/tupian/477835524.html https://ibaotu.com/tupian/477835523.html https://ibaotu.com/tupian/477835522.html https://ibaotu.com/tupian/477835518.html https://ibaotu.com/tupian/477835519.html https://ibaotu.com/tupian/477835517.html https://ibaotu.com/tupian/477835513.html https://ibaotu.com/tupian/477835512.html https://ibaotu.com/tupian/477835510.html https://ibaotu.com/tupian/477835509.html https://ibaotu.com/tupian/477835508.html https://ibaotu.com/tupian/477835506.html https://ibaotu.com/tupian/477835473.html https://ibaotu.com/tupian/477835470.html https://ibaotu.com/tupian/477835467.html https://ibaotu.com/tupian/477835466.html https://ibaotu.com/tupian/477835464.html https://ibaotu.com/tupian/477835462.html https://ibaotu.com/tupian/477835460.html https://ibaotu.com/tupian/477835459.html https://ibaotu.com/tupian/477835458.html https://ibaotu.com/tupian/477835457.html https://ibaotu.com/tupian/477835456.html https://ibaotu.com/tupian/477835451.html https://ibaotu.com/tupian/477835450.html https://ibaotu.com/tupian/477835448.html https://ibaotu.com/tupian/477835447.html https://ibaotu.com/tupian/477835445.html https://ibaotu.com/tupian/477835444.html https://ibaotu.com/tupian/477835440.html https://ibaotu.com/tupian/477835439.html https://ibaotu.com/tupian/477835434.html https://ibaotu.com/tupian/477835429.html https://ibaotu.com/tupian/477835427.html https://ibaotu.com/tupian/477835426.html https://ibaotu.com/tupian/477835425.html https://ibaotu.com/tupian/477835421.html https://ibaotu.com/tupian/477835418.html https://ibaotu.com/tupian/477835417.html https://ibaotu.com/tupian/477835416.html https://ibaotu.com/tupian/477835415.html https://ibaotu.com/tupian/477835413.html https://ibaotu.com/tupian/477835381.html https://ibaotu.com/tupian/477835377.html https://ibaotu.com/tupian/477835376.html https://ibaotu.com/tupian/477835375.html https://ibaotu.com/tupian/477835373.html https://ibaotu.com/tupian/477835371.html https://ibaotu.com/tupian/477835372.html https://ibaotu.com/tupian/477835364.html https://ibaotu.com/tupian/477835362.html https://ibaotu.com/tupian/477835360.html https://ibaotu.com/tupian/477835356.html https://ibaotu.com/tupian/477835351.html https://ibaotu.com/tupian/477835350.html https://ibaotu.com/tupian/477835344.html https://ibaotu.com/tupian/477835341.html https://ibaotu.com/tupian/477835330.html https://ibaotu.com/tupian/477835328.html https://ibaotu.com/tupian/477835327.html https://ibaotu.com/tupian/477835326.html https://ibaotu.com/tupian/477835321.html https://ibaotu.com/tupian/477835319.html https://ibaotu.com/tupian/477835316.html https://ibaotu.com/tupian/477835315.html https://ibaotu.com/tupian/477835305.html https://ibaotu.com/tupian/477835304.html https://ibaotu.com/tupian/477835303.html https://ibaotu.com/tupian/477835294.html https://ibaotu.com/tupian/477835283.html https://ibaotu.com/tupian/477835280.html https://ibaotu.com/tupian/477835274.html https://ibaotu.com/tupian/477835272.html https://ibaotu.com/tupian/477835271.html https://ibaotu.com/tupian/477835270.html https://ibaotu.com/tupian/477835268.html https://ibaotu.com/tupian/477835267.html https://ibaotu.com/tupian/477835264.html https://ibaotu.com/tupian/477835265.html https://ibaotu.com/tupian/477835266.html https://ibaotu.com/tupian/477835262.html https://ibaotu.com/tupian/477835259.html https://ibaotu.com/tupian/477835243.html https://ibaotu.com/tupian/477835242.html https://ibaotu.com/tupian/477835241.html https://ibaotu.com/tupian/477835238.html https://ibaotu.com/tupian/477835237.html https://ibaotu.com/tupian/477835236.html https://ibaotu.com/tupian/477835235.html https://ibaotu.com/tupian/477835233.html https://ibaotu.com/tupian/477835231.html https://ibaotu.com/tupian/477835227.html https://ibaotu.com/tupian/477835223.html https://ibaotu.com/tupian/477835221.html https://ibaotu.com/tupian/477835204.html https://ibaotu.com/tupian/477835203.html https://ibaotu.com/tupian/477835191.html https://ibaotu.com/tupian/477835201.html https://ibaotu.com/tupian/477835200.html https://ibaotu.com/tupian/477835198.html https://ibaotu.com/tupian/477835196.html https://ibaotu.com/tupian/477835194.html https://ibaotu.com/tupian/477835190.html https://ibaotu.com/tupian/477835189.html https://ibaotu.com/tupian/477835188.html https://ibaotu.com/tupian/477835187.html https://ibaotu.com/tupian/477835186.html https://ibaotu.com/tupian/477835185.html https://ibaotu.com/tupian/477835163.html https://ibaotu.com/tupian/477835161.html https://ibaotu.com/tupian/477835160.html https://ibaotu.com/tupian/477835154.html https://ibaotu.com/tupian/477835148.html https://ibaotu.com/tupian/477835138.html https://ibaotu.com/tupian/477835135.html https://ibaotu.com/tupian/477835130.html https://ibaotu.com/tupian/477835128.html https://ibaotu.com/tupian/477835127.html https://ibaotu.com/tupian/477835123.html https://ibaotu.com/tupian/477835122.html https://ibaotu.com/tupian/477835119.html https://ibaotu.com/tupian/477835117.html https://ibaotu.com/tupian/477835115.html https://ibaotu.com/tupian/477835114.html https://ibaotu.com/tupian/477835113.html https://ibaotu.com/tupian/477835110.html https://ibaotu.com/tupian/477835108.html https://ibaotu.com/tupian/477835107.html https://ibaotu.com/tupian/477835102.html https://ibaotu.com/tupian/477835100.html https://ibaotu.com/tupian/477835098.html https://ibaotu.com/tupian/477835095.html https://ibaotu.com/tupian/477835090.html https://ibaotu.com/tupian/477835089.html https://ibaotu.com/tupian/477835084.html https://ibaotu.com/tupian/477835078.html https://ibaotu.com/tupian/477835075.html https://ibaotu.com/tupian/477835073.html https://ibaotu.com/tupian/477835072.html https://ibaotu.com/tupian/477835047.html https://ibaotu.com/tupian/477835024.html https://ibaotu.com/tupian/477835019.html https://ibaotu.com/tupian/477835017.html https://ibaotu.com/tupian/477835015.html https://ibaotu.com/tupian/477835014.html https://ibaotu.com/tupian/477835013.html https://ibaotu.com/tupian/477835012.html https://ibaotu.com/tupian/477835011.html https://ibaotu.com/tupian/477835007.html https://ibaotu.com/tupian/477835005.html https://ibaotu.com/tupian/477835002.html https://ibaotu.com/tupian/477834999.html https://ibaotu.com/tupian/477834994.html https://ibaotu.com/tupian/477834993.html https://ibaotu.com/tupian/477834992.html https://ibaotu.com/tupian/477834990.html https://ibaotu.com/tupian/477834989.html https://ibaotu.com/tupian/477834988.html https://ibaotu.com/tupian/477834973.html https://ibaotu.com/tupian/477834973.html https://ibaotu.com/tupian/477834973.html https://ibaotu.com/tupian/477834973.html https://ibaotu.com/tupian/477834973.html https://ibaotu.com/tupian/477834970.html https://ibaotu.com/tupian/477834967.html https://ibaotu.com/tupian/477834966.html https://ibaotu.com/tupian/477834964.html https://ibaotu.com/tupian/477834963.html https://ibaotu.com/tupian/477834962.html https://ibaotu.com/tupian/477834956.html https://ibaotu.com/tupian/477834954.html https://ibaotu.com/tupian/477834953.html https://ibaotu.com/tupian/477834952.html https://ibaotu.com/tupian/477834950.html https://ibaotu.com/tupian/477834943.html https://ibaotu.com/tupian/477834942.html https://ibaotu.com/tupian/477834941.html https://ibaotu.com/tupian/477834939.html https://ibaotu.com/tupian/477834938.html https://ibaotu.com/tupian/477834908.html https://ibaotu.com/tupian/477834907.html https://ibaotu.com/tupian/477834906.html https://ibaotu.com/tupian/477834905.html https://ibaotu.com/tupian/477834904.html https://ibaotu.com/tupian/477834902.html https://ibaotu.com/tupian/477834901.html https://ibaotu.com/tupian/477834899.html https://ibaotu.com/tupian/477834898.html https://ibaotu.com/tupian/477834895.html https://ibaotu.com/tupian/477834889.html https://ibaotu.com/tupian/477834887.html https://ibaotu.com/tupian/477834884.html https://ibaotu.com/tupian/477834882.html https://ibaotu.com/tupian/477834881.html https://ibaotu.com/tupian/477834879.html https://ibaotu.com/tupian/477834877.html https://ibaotu.com/tupian/477834876.html https://ibaotu.com/tupian/477834871.html https://ibaotu.com/tupian/477834870.html https://ibaotu.com/tupian/477834868.html https://ibaotu.com/tupian/477834865.html https://ibaotu.com/tupian/477834851.html https://ibaotu.com/tupian/477834850.html https://ibaotu.com/tupian/477834848.html https://ibaotu.com/tupian/477834842.html https://ibaotu.com/tupian/477834841.html https://ibaotu.com/tupian/477834840.html https://ibaotu.com/tupian/477834836.html https://ibaotu.com/tupian/477834832.html https://ibaotu.com/tupian/477834830.html https://ibaotu.com/tupian/477834829.html https://ibaotu.com/tupian/477834828.html https://ibaotu.com/tupian/477834826.html https://ibaotu.com/tupian/477834824.html https://ibaotu.com/tupian/477834823.html https://ibaotu.com/tupian/477834822.html https://ibaotu.com/tupian/477834818.html https://ibaotu.com/tupian/477834817.html https://ibaotu.com/tupian/477834811.html https://ibaotu.com/tupian/477834806.html https://ibaotu.com/tupian/477834805.html https://ibaotu.com/tupian/477834771.html https://ibaotu.com/tupian/477834776.html https://ibaotu.com/tupian/477834777.html https://ibaotu.com/tupian/477834768.html https://ibaotu.com/tupian/477834766.html https://ibaotu.com/tupian/477834767.html https://ibaotu.com/tupian/477834764.html https://ibaotu.com/tupian/477834760.html https://ibaotu.com/tupian/477834758.html https://ibaotu.com/tupian/477834753.html https://ibaotu.com/tupian/477834752.html https://ibaotu.com/tupian/477834750.html https://ibaotu.com/tupian/477834747.html https://ibaotu.com/tupian/477834744.html https://ibaotu.com/tupian/477834741.html https://ibaotu.com/tupian/477834740.html https://ibaotu.com/tupian/477834737.html https://ibaotu.com/tupian/477834734.html https://ibaotu.com/tupian/477834731.html https://ibaotu.com/tupian/477834730.html https://ibaotu.com/tupian/477834724.html https://ibaotu.com/tupian/477834722.html https://ibaotu.com/tupian/477834723.html https://ibaotu.com/tupian/477834720.html https://ibaotu.com/tupian/477834711.html https://ibaotu.com/tupian/477834709.html https://ibaotu.com/tupian/477834678.html https://ibaotu.com/tupian/477834677.html https://ibaotu.com/tupian/477834674.html https://ibaotu.com/tupian/477834672.html https://ibaotu.com/tupian/477834668.html https://ibaotu.com/tupian/477834665.html https://ibaotu.com/tupian/477834663.html https://ibaotu.com/tupian/477834657.html https://ibaotu.com/tupian/477834654.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); } }


