java 原始数据类型_Java编程中的原始数据类型

java 原始数据类型

In almost every Java program you will find primitive data types being used. They provide a way to store the simple values the program is dealing with. For example, consider a calculator program that allows the user to perform mathematical calculations. In order for the program to achieve its goal, it has to be capable of storing the values the user enters. This can be done using variables. A variable is a container for a specific kind of value that is known as a data type.

在几乎每个Java程序中,您都会发现正在使用的原始数据类型。 它们提供了一种存储程序要处理的简单值的方法。 例如,考虑一个允许用户执行数学计算的计算器程序。 为了使程序实现其目标,它必须能够存储用户输入的值。 这可以使用变量来完成。 变量是用于特定类型值的容器,该值称为数据类型

原始数据类型 ( Primitive Data Types )

Java comes with eight primitive data types to handle simple data values. They can be split into four categories by the kind of value they hold:

Java带有八种原始数据类型来处理简单数据值。 根据其所拥有的价值,它们可以分为四类:

  • Integers: these are positive and negative whole numbers.

    整数:这些是正整数和负整数。

  • Floating Point Numbers: any number that has a fractional part.

    浮点数 具有小数部分的任何数字。

  • Characters: a single character.

    字符:单个字符。

  • Truth Values: either true or false.

    真相值:对或错。

整数 ( Integers )

Integers hold number values that cannot have a fractional part. There are four different types:

整数包含不能有小数部分的数字值。 有四种不同的类型:

  • byte: uses one byte to store values from -128 to 127

    字节:使用一个字节存储从-128到127的值

  • short: uses two bytes to store values from -32,768 to 32,767

    short:使用两个字节存储从-32,768到32,767的值

  • int: uses four bytes to store values from -2,147,483,648 to 2,147,483,647

    int:使用四个字节存储从-2,147,483,648到2,147,483,647的值

  • long: uses eight bytes to store values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

    long:使用八个字节存储从-9,223,372,036,854,775,808到9,223,372,036,854,775,807的值

As you can see from above the only difference between the types are the range of values they can hold. Their ranges directly correlate to the amount of space the data type needs to store its values.

从上面可以看到,类型之间的唯一区别是它们可以容纳的值的范围。 它们的范围与数据类型存储其值所需的空间量直接相关。

In most cases when you want to represent a whole number use the int data type. Its ability to hold numbers from just under -2 billion to a little over 2 billion will be suitable for most integer values. However, if for some reason you need to write a program that uses as little memory as possible, consider the values you need to represent and see if the byte or short is a better choice. Likewise, if you know the numbers you need to store are higher than 2 billion then use the long data type.

在大多数情况下,当您要表示整数时,请使用int数据类型。 它可以容纳从-20亿到不到20亿的数字的能力将适用于大多数整数值。 但是,如果由于某种原因需要编写使用尽可能少的内存的程序,请考虑需要代表的值,然后查看字节或短字节是否是更好的选择。 同样,如果您知道需要存储的数字大于20亿,则使用long数据类型。

浮点数字 ( Floating Point Numbers )

Unlike integers, floating point numbers like fractional parts. There are two different types:

与整数不同,浮点数就像小数部分。 有两种不同的类型:

  • float: uses four bytes to store values from -3.4028235E+38 to 3.4028235E+38

    float:使用四个字节存储-3.4028235E + 38到3.4028235E + 38的值

  • double: uses eight bytes to store values from -1.7976931348623157E+308 to 1.7976931348623157E+308

    double:使用八个字节存储从-1.7976931348623157E + 308到1.7976931348623157E + 308的值

The difference between the two is simply the range of fractional numbers they can hold. Like integers the range directly correlates to the amount of space they need to store the number. Unless you have memory concerns it's best to use the double data type in your programs. It will handle fractional numbers to the precision needed in most applications. The main exception will be in financial software where rounding errors cannot be tolerated.

两者之间的区别只是它们可以保留的小数范围。 像整数一样,范围与存储数字所需的空间量直接相关。 除非您有内存问题,否则最好在程序中使用double数据类型。 它将处理小数到大多数应用程序所需的精度。 主要例外是在财务软件中,不能容忍四舍五入错误。

性格 ( Characters )

There is only one primitive data type that deals with individual characters – the char. The char can hold the value of one character and is based on 16-bit Unicode encoding. The character might be a letter, digit, punctuation, a symbol or a control character (e.g., a character value that represents a newline or a tab).

只有一种处理单个字符的原始数据类型-char 。 char可以保留一个字符的值,并且基于16位Unicode编码 。 该字符可以是字母,数字,标点符号,符号或控制字符(例如,代表换行符或制表符的字符值)。

真理价值 ( Truth Values )

As Java programs deal in logic there needs to be a way to determine when a condition is true and when it is false. The boolean data type can hold those two values; it can only be true or false.

随着Java程序处理逻辑,需要一种确定条件何时为真以及何时为假的方法。 布尔数据类型可以容纳这两个值。 它只能是对或错。

翻译自: https://www.thoughtco.com/primitive-data-types-2034320

java 原始数据类型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值