专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java学习 Java八大数据类型的详细介绍

Java八大数据类型的详细介绍

更新时间:2022-06-02 10:20:51 来源:动力节点 浏览1403次

Java 是一种强类型语言。这意味着,在 Java 中,每种数据类型都有自己的严格定义。当数据类型之间发生任何冲突时,不存在隐式数据类型转换。数据类型的任何更改都应由程序员明确声明。

Java 定义了8 种原始数据类型:byte、short、int、long、char、float和.doubleboolean

它们分为以下几类:

整数

浮点数字

人物

布尔类型

每种数据类型的详细信息如下:

整数:

它们有四种类型:byte, short, int, long。重要的是要注意这些是有符号的正值和负值。有符号整数使用2 的补码存储在计算机中。它包含负值和正值,但格式不同,例如(-1 to -128)or (0 to +127)。无符号整数可以容纳更大的正值,而没有像(0 to 255). 与 C++ 不同,Java 中没有无符号整数。

字节:

字节数据类型是一个 8 位有符号二进制补码整数。

Wrapper Class: Byte
Minimum value: -128 (-2^7)
Maximum value: 127 (2^7 -1)
Default value: 0
Example: byte a = 10 , byte b = -50;

短的:

短数据类型是 16 位有符号二进制补码整数。

Wrapper Class: Short
Minimum value: -32,768 (-2^15)
Maximum value: 32,767 (2^15 -1)
Default value: 0.
Example: short s = 10, short r = -1000;

诠释:

int 数据类型是一个 32 位有符号二进制补码整数。它通常用作整数值的默认数据类型,除非存在内存问题。

Wrapper Class: Integer
Minimum value: (-2^31)
Maximum value: (2^31 -1)
The default value: 0.
Example: int a = 50000, int b = -20

长:

Long 数据类型是一个 64 位有符号二进制补码整数。

Wrapper Class: Long
Minimum value: (-2^63)
Maximum value: (2^63 -1)
Default value: 0L.
Example: long a = 100000L, long b = -600000L; 
By default all integer type variable is "int". So long num=600851475143  will give an error.
But it can be specified as long by appending the suffix L (or l)

浮点数​:

这些也称为实数,用于涉及小数精度的表达式。它们有两种类型:float, double。在货币或研究数据等精确数据的情况下,实际上避免了浮动。

漂浮:

float 数据类型是单精度 32 位IEEE 754 浮点数。

Wrapper Class: Float
Float is mainly used to save memory in large arrays of floating point numbers.
Default value: 0.0f.
Example: float f1 = 24.5f;
The default data type of floating-point number is double. So float f = 24.5 will introduce an error.
However, we can append the suffix F (or f) to designate the data type as float.

双倍的:

double 数据类型是双精度 64 位IEEE 754 浮点。此数据类型通常是默认选择。此数据类型绝不应用于精确值,例如货币。

Wrapper Class: Double
This data type is generally used as the default data type for decimal values.
Default value: 0.0d.
Example: double d1 = 123.400778;

特点:

我们使用这种数据类型来存储字符。这与 C/C++ 中的 char 不同。Java 使用UNICODE国际公认的字符集。Java 中的字符为 16 位,而 C/C++ 中的字符为 8 位。

Wrapper Class: Character
Minimum value: '\u0000' (or 0).
Maximum value: '\uffff' (or 65,535).
Default value: null ('\u0000').
Example: char letterA ='a';

布尔值:

这用于存储逻辑值。布尔类型的值可以是真或假。这种类型通常由关系运算符返回。

There are only two possible values: true and false.
Wrapper Class: Boolean
This data type is used for simple flags that track true/false conditions.
Default value is false.
Example: boolean b = true, boolean b1 = 1(this is not possible in java and give incompatible type error), boolean b2;

参考数据类型:

除了原始数据类型之外,还有使用不同类的构造函数创建的引用变量。引用变量可用于任何类以及数组、字符串、扫描仪、随机数、模具等。使用新关键字初始化引用变量。

例子 :

public class Box{
    int length, breadth, height;
    public Box(){
        length=5;
        breadth=3;
        height=2;
    }
}
class demo{
    public static void main(String args[]) {
        Box box1 = new Box();                //box1 is the reference variable  
        char[] arr = new char[10];           //arr is the reference variable
    }
}

细绳:

String 不是原始数据类型,但它允许您将多个字符数据类型存储在一个数组中,并且有许多可以使用的方法。当用户输入数据并且您必须对其进行操作时,它非常常用。

在下面的示例中,我们尝试从字符串中删除所有字母并将其输出:

String input = "My birthday is 10 January 1984 and my favorite number is 42";
String output = "";
for(int i=0;i<input.length();i++){
//if the character at index i on the string is a letter or a space, move on to the next index
if(Character.isLetter(input.charAt(i)) || input.charAt(i)==' '){     
    continue;
}
output = output + input.charAt(i); //the number is added onto the output
}
System.out.println(output);

输出:

10198442

 

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>