Java内置数据类型

在Java种共有8种内置数据类型。内置数据类型是在Java中预定义好的。通过下面的图片看一下每一个基本数据类型。
这里写图片描述

整型数据类型

整型数据类型是数值数据类型, 其值为整数。Java 提供了五个完整的整数数据类型: 字节、短、int、长和 char。让我们简单做个了解。

  • int

整型int是一个32位有符号的数据类型,即在内存中一个整型数据占用32位。数据域为-2,147,483,648 to 2,147,483,647 (-2^31 to 2^31– 1)。此范围内的所有整数都称为整型文本 (或整数常量)。整数型数据可以赋给整型变量,如:

int num = 21;

Java 有一个名为 Integer 的类, 它定义了两个常量来表示 int 数据类型的最大值和最小数值。

int max = Integer.MAX_VALUE; // Assigns maximum int value to max
int min = Integer.MIN_VALUE; // Assigns minimum int value to min
  • long

长整型是一个64位有符号内置数据类型。当整数计算的结果可能超过 int 数据类型的范围时, 将使用该类型。数据范围是-2^63 to 2^63 – 1。long 范围内的所有整数称为长类型的整型文本。一个长整型数据常以字母L结尾,如:

long num1 = 0L;
long num2 = 401L;
long mum3 = -3556L;

如果需要把一个long型数据赋给一个int类型变量,需要在代码中显示的表明,这样Java会确定你是知道会发生数据溢出的。在Java 中使用 “强制转换” 来实现。

num1 = (int) num2; // Now it is fine because of the "(int)" cast

(int) num2, 表示将 num2 中存储的值视为 int。在运行时, Java 将最低的32位中的值分配给 num1。如果 num2 的值超出了 int 数据类型的范围, 则在 num1 中不会获得相同的值。

Java 有一个名为 Long 的类, 它定义了两个常量来表示 long 数据类型的最大值和最小数值。

long max = Long.MAX_VALUE;
long min = Long.MIN_VALUE;
  • byte

字节型byte是一个8位有符号的数据类型。数据域为-128 to 127。byte类型是Java整数型类型中数据最小的。
和int,long型不同,byte没有字节文本。但是可以将有效范围内的整数赋值给字节类型,如:

byte b1 = 125;
byte b2 = -11;

如果将一个范围外的整数赋值给byte类型变量,会产生编译错误如下。

// An error. 150 is an int literal outside -128 to 127
byte b3 = 150;

Java不允许将数据域大的类型赋值给低范围的数据类型,这样会丢失精度。对于这种操作,必须使用强制类型转换。

b1 = (byte)num1; // Ok

Java 有一个名为Byte的类, 它定义了两个常量来表示 byte数据类型的最大值和最小数值。

byte max = Byte.MAX_VALUE;
byte min = Byte.MIN_VALUE;
  • short

short类型是16位有符号的内置数据类型。数据范围为-32768到32767.可以将范围内的int数据赋值给short类型,如:

short s1 = 12905;   // ok
short s2 = -11890;  // ok

byte类型的数据总是可以赋值给short类型,因为short的数据域包含了byte的数据域。将int和long型赋给short型和赋给byte类型是一样的,需要强制类型转换。

Java 有一个名为 Short 的类, 它定义了两个常量来表示 short 数据类型的最大值和最小数值。

short max = Short.MAX_VALUE;
short min = Short.MIN_VALUE;
  • char

char是一个16位无符号类型。表示一个Unicode字符。由于是无符号类型,因此没有负值。数据域为0-65536,和Unicode字符集一样的范围。一个字符表示一个char类型的值。

char c1 = 'A';
char c2 = 'L';
char c3 = '5';
char c4 = '/';

如下为转义字符:

‘\n’ A linefeed
‘\r’ A carriage return
‘\f’ A form feed
‘\b’ A backspace
‘\t’ A tab
‘\’ A backslash
‘\”‘ A double quote
‘\” A single quote

转义字符共有8个,不可以自定义。
一个字母也可以用Unicode的转义序列表示,如 ‘\uxxxx’。其中,\u 表示转义字符序列的开始,xxxx是一个4位实际的16位数字

char c1 = 'A';
char c2 = '\u0041';  // Same as c2 = 'A',A的Unicode是65,ASCII也是65.

浮点数据类型

包含小数部分的浮点数字称为实数,如3.25,0.49,-9.19等。当一个实数转换为二进制时,计算机同时需要存储小数点的位置。在计算机中,使用如下两种方法保存实数。

a) Store only the binary representation of the number and assume that there are always a fixed number of digits before and after the point. A point is called a decimal point in the decimal representation of a number and a binary point in the binary representation. The type of representation in which the position of the point is always fixed in a number is known as “fixed-point” number format.

b) Store the binary representation of the real number and the position of the point in the real number. Since the number of digits before and after the point can vary in this kind of representation of the real number, we say that the point can float. This kind of representation is called a “floating-point” format.

Floating-point representations are slower and less accurate compared to fixed-point representations. However, floating-point representations can handle a larger range of numbers with the same computer memory as compared to that of fixed-point representations.

Java supports the floating-point number format. Java has two floating-point Numeric data types: float and double.
Java支持浮点数字格式。共有两种浮点数据类型:float(浮点)和double(双精度)

  • 浮点

The float data type uses 32 bits to store a floating-point number in the IEEE 754 standard format (single-precision floating-point number). It can represent a real number as small as 1.4 x 10-45 and as big as 3.4 x 1038 (approx.) in magnitude. The range includes only the magnitude. It could be positive or negative.

All real numbers that end with f or F are called float literals.

float f1 = 8F;
float f2 = 8.F;
float f3 = 8.0F;

The float data type defines two infinities: positive infinity and negative infinity. For example, the result of the dividing 2.5F by 0.0F is a float positive infinity whereas the result of dividing 2.5F by -0.0F is a float negative infinity.

Results of some of the operations on float are not defined. These results are represented by a special value of the float data type called NaN (Not-a-Number). Java has a Float class (Note the upper case F in Float), which defines three constants that represent positive infinity, negative infinity, and NaN of the float data type. There are two more constants, which represent the maximum and minimum (greater than zero) float values that can be stored in a float variable.

Float.POSITIVE_INFINITY - Positive infinity of type float.
Float.NEGATIVE_INFINITY - Negative infinity of type float.
Float.NaN - Not a Number of type float.
Float.MAX_VALUE - The largest positive value that can be represented in a float variable.
Float.MIN_VALUE - The smallest positive value greater than zero that can be represented in a float variable.
  • double

The double data type uses 64 bits to store a floating-point number in the IEEE 754 standard format. A floating-point number represented in 64 bits according to IEEE 754 standard is also known as a double-precision floating-point number.

All real numbers are called double literals. A double literal may optionally end with d or D, for example, 19.27d. However, the suffix d or D is optional in double literals. That is, both 19.27 and 19.27d represent the same double literal.

double d1 = 8D
double d2 = 8.;
double d3 = 8.0;
double d4 = 8.D;

Like the float data type, the double data type defines two zeros, two infinities, and a NaN.

Double.POSITIVE_INFINITY - Positive infinity of type double.
Double.NEGATIVE_INFINITY - Negative infinity of type double.
Double.NaN - Not a Number of type double.
Double.MAX_VALUE - The largest positive value that can be represented in a double variable.
Double.MIN_VALUE - The smallest positive value greater than zero that can be represented in a double variable.

布尔类型

  • boolean

布尔类型只有两个有效值:true和false。这两个值称为布尔型文本。具体使用方法如下:

boolean done; // Declares a boolean variable named done
done = true;  // Assigns true to done

需要注意的是,布尔类型不能显示转换到其他类型,反之亦然。Java里没有指定boolean的大小,它的大小留给 JVM 实现。通常, 布尔数据类型的值存储在一个字节内。

That’s all for 8 primitive data types available in java.
以上为这8个内置数据类型的全部内容。

------------这章写着真的累,太多了,粘了点原文过来。留个坑,慢慢填吧--------

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值