内置数据类型
Java语言提供了八种基本类型。
- byte
/**
* A constant holding the minimum value a {@code byte} can
* have, -2<sup>7</sup>.
*/
public static final byte MIN_VALUE = -128;
/**
* A constant holding the maximum value a {@code byte} can
* have, 2<sup>7</sup>-1.
*/
public static final byte MAX_VALUE = 127;
- short
/**
* A constant holding the minimum value a {@code short} can
* have, -2<sup>15</sup>.
*/
public static final short MIN_VALUE = -32768;
/**
* A constant holding the maximum value a {@code short} can
* have, 2<sup>15</sup>-1.
*/
public static final short MAX_VALUE = 32767;
- int
/**
* A constant holding the minimum value an {@code int} can
* have, -2<sup>31</sup>.
*/
@Native public static final int MIN_VALUE = 0x80000000;
/**
* A constant holding the maximum value an {@code int} can
* have, 2<sup>31</sup>-1.
*/
@Native public static final int MAX_VALUE = 0x7fffffff;
- long
/**
* A constant holding the minimum value a {@code long} can
* have, -2<sup>63</sup>.
*/
@Native public static final long MIN_VALUE = 0x8000000000000000L;
/**
* A constant holding the maximum value a {@code long} can
* have, 2<sup>63</sup>-1.
*/
@Native public static final long MAX_VALUE = 0x7fffffffffffffffL;
- float
/**
/**
* A constant holding the positive infinity of type
* {@code float}. It is equal to the value returned by
* {@code Float.intBitsToFloat(0x7f800000)}.
*/
public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
/**
* A constant holding the negative infinity of type
* {@code float}. It is equal to the value returned by
* {@code Float.intBitsToFloat(0xff800000)}.
*/
public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
/**
* A constant holding a Not-a-Number (NaN) value of type
* {@code float}. It is equivalent to the value returned by
* {@code Float.intBitsToFloat(0x7fc00000)}.
*/
public static final float NaN = 0.0f / 0.0f;
/**
* A constant holding the largest positive finite value of type
* {@code float}, (2-2<sup>-23</sup>)·2<sup>127</sup>.
* It is equal to the hexadecimal floating-point literal
* {@code 0x1.fffffeP+127f} and also equal to
* {@code Float.intBitsToFloat(0x7f7fffff)}.
*/
public static final float MAX_VALUE = 0x1.fffffeP+127f; // 3.4028235e+38f
/**
* A constant holding the smallest positive normal value of type
* {@code float}, 2<sup>-126</sup>. It is equal to the
* hexadecimal floating-point literal {@code 0x1.0p-126f} and also
* equal to {@code Float.intBitsToFloat(0x00800000)}.
*
* @since 1.6
*/
public static final float MIN_NORMAL = 0x1.0p-126f; // 1.17549435E-38f
/**
* A constant holding the smallest positive nonzero value of type
* {@code float}, 2<sup>-149</sup>. It is equal to the
* hexadecimal floating-point literal {@code 0x0.000002P-126f}
* and also equal to {@code Float.intBitsToFloat(0x1)}.
*/
public static final float MIN_VALUE = 0x0.000002P-126f; // 1.4e-45f
/**
* Maximum exponent a finite {@code float} variable may have. It
* is equal to the value returned by {@code
* Math.getExponent(Float.MAX_VALUE)}.
*
* @since 1.6
*/
public static final int MAX_EXPONENT = 127;
/**
* Minimum exponent a normalized {@code float} variable may have.
* It is equal to the value returned by {@code
* Math.getExponent(Float.MIN_NORMAL)}.
*
* @since 1.6
*/
public static final int MIN_EXPONENT = -126;
/**
* The number of bits used to represent a {@code float} value.
*
* @since 1.5
*/
public static final int SIZE = 32;
/**
* The number of bytes used to represent a {@code float} value.
*
* @since 1.8
*/
public static final int BYTES = SIZE / Byte.SIZE;
- double
/**
* A constant holding the positive infinity of type
* {@code double}. It is equal to the value returned by
* {@code Double.longBitsToDouble(0x7ff0000000000000L)}.
*/
public static final double POSITIVE_INFINITY = 1.0 / 0.0;
/**
* A constant holding the negative infinity of type
* {@code double}. It is equal to the value returned by
* {@code Double.longBitsToDouble(0xfff0000000000000L)}.
*/
public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
/**
* A constant holding a Not-a-Number (NaN) value of type
* {@code double}. It is equivalent to the value returned by
* {@code Double.longBitsToDouble(0x7ff8000000000000L)}.
*/
public static final double NaN = 0.0d / 0.0;
/**
* A constant holding the largest positive finite value of type
* {@code double},
* (2-2<sup>-52</sup>)·2<sup>1023</sup>. It is equal to
* the hexadecimal floating-point literal
* {@code 0x1.fffffffffffffP+1023} and also equal to
* {@code Double.longBitsToDouble(0x7fefffffffffffffL)}.
*/
public static final double MAX_VALUE = 0x1.fffffffffffffP+1023; // 1.7976931348623157e+308
/**
* A constant holding the smallest positive normal value of type
* {@code double}, 2<sup>-1022</sup>. It is equal to the
* hexadecimal floating-point literal {@code 0x1.0p-1022} and also
* equal to {@code Double.longBitsToDouble(0x0010000000000000L)}.
*
* @since 1.6
*/
public static final double MIN_NORMAL = 0x1.0p-1022; // 2.2250738585072014E-308
/**
* A constant holding the smallest positive nonzero value of type
* {@code double}, 2<sup>-1074</sup>. It is equal to the
* hexadecimal floating-point literal
* {@code 0x0.0000000000001P-1022} and also equal to
* {@code Double.longBitsToDouble(0x1L)}.
*/
public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324
/**
* Maximum exponent a finite {@code double} variable may have.
* It is equal to the value returned by
* {@code Math.getExponent(Double.MAX_VALUE)}.
*
* @since 1.6
*/
public static final int MAX_EXPONENT = 1023;
/**
* Minimum exponent a normalized {@code double} variable may
* have. It is equal to the value returned by
* {@code Math.getExponent(Double.MIN_NORMAL)}.
*
* @since 1.6
*/
public static final int MIN_EXPONENT = -1022;
/**
* The number of bits used to represent a {@code double} value.
*
* @since 1.5
*/
public static final int SIZE = 64;
/**
* The number of bytes used to represent a {@code double} value.
*
* @since 1.8
*/
public static final int BYTES = SIZE / Byte.SIZE;
- char
/**
* The minimum radix available for conversion to and from strings.
* The constant value of this field is the smallest value permitted
* for the radix argument in radix-conversion methods such as the
* {@code digit} method, the {@code forDigit} method, and the
* {@code toString} method of class {@code Integer}.
*
* @see Character#digit(char, int)
* @see Character#forDigit(int, int)
* @see Integer#toString(int, int)
* @see Integer#valueOf(String)
*/
public static final int MIN_RADIX = 2;
/**
* The maximum radix available for conversion to and from strings.
* The constant value of this field is the largest value permitted
* for the radix argument in radix-conversion methods such as the
* {@code digit} method, the {@code forDigit} method, and the
* {@code toString} method of class {@code Integer}.
*
* @see Character#digit(char, int)
* @see Character#forDigit(int, int)
* @see Integer#toString(int, int)
* @see Integer#valueOf(String)
*/
public static final int MAX_RADIX = 36;
/**
* The constant value of this field is the smallest value of type
* {@code char}, {@code '\u005Cu0000'}.
*
* @since 1.0.2
*/
public static final char MIN_VALUE = '\u0000';
/**
* The constant value of this field is the largest value of type
* {@code char}, {@code '\u005CuFFFF'}.
*
* @since 1.0.2
*/
public static final char MAX_VALUE = '\uFFFF';
- boolean
/**
* The {@code Boolean} object corresponding to the primitive
* value {@code true}.
*/
public static final Boolean TRUE = new Boolean(true);
/**
* The {@code Boolean} object corresponding to the primitive
* value {@code false}.
*/
public static final Boolean FALSE = new Boolean(false);
- 上面就是java的八种基本类型,从源码copy而来,下面通过简单的例子输出八种基本类型的常用值
// 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);
详细说说这八种类型的用法
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 = 123.4。
- boolean:
boolean数据类型表示一位的信息;
只有两个取值:true 和 false;
这种类型只作为一种标志来记录 true/false 情况;
默认值是 false;
例子:boolean one = true。
- char:
char类型是一个单一的 16 位 Unicode 字符;
最小值是 \u0000(即为0);
最大值是 \uffff(即为65,535);
char 数据类型可以储存任何字符;
例子:char letter = ‘A’;。
下面介绍一些java基本类型常用的方法
- java基本类型的转换规则:从存储范围小的类型到存储范围大的类型
byte→short(char)→int→long→float→double
byte num1 =10;
System.out.println(num1);
short num2 =num1;
System.out.println(num2);
int num3 =num2;
System.out.println(num3);
long num4=num3;
System.out.println(num4);
float num5=num4;
System.out.println(num5);
double num6=num5;
System.out.println(num6);
//结果:10/10/10/10/10.0/10.0
- 强制类型转换:会损失精度
double→float→long→int→short(char)→byte
double val=12.33;
System.out.println(val);
float val1= (float) val;
System.out.println(val1);
long val2=(long)val1;
System.out.println(val2);
int val3=(int)val2;
System.out.println(val3);
short val4=(short)val3;
System.out.println(val4);
byte val5 =(byte)val4;
System.out.println(val5);
//输出结果 12.33/12.33/12/12/12/12
//char
char c=65;
System.out.println(c); //print: A
char h='A';
System.out.println(3+h); //print: 68