java数据类型

Java is a completely object-oriented programming language which provides a lot of different type of features. Primitive data types are some of them. Java provides basic or primitive data types like integer, string, logical true and false, etc. In this tutorial, we will learn the Java Primitive Datatypes.

Java是一种完全面向对象的编程语言,它提供了许多不同类型的功能。 原始数据类型就是其中一些。 Java提供了基本或原始数据类型,例如整数,字符串,逻辑对与错等。在本教程中,我们将学习Java原始数据类型。

(Primitive Data Types)

Most of the programming languages provide the primitive data type. Java also provides a primitive data type as a complete and popular programming language. Primitive means basic or non-complex data type simply storing a single type of data. Integers, floating-point numbers, strings, characters are named as primitive because they hold a single type of data. Class, Structs, Objects are referred to as nonprimitive data types.

大多数编程语言都提供原始数据类型。 Java还提供一种原始数据类型,作为一种完整且流行的编程语言。 原始表示简单存储单一数据类型的基本或非复杂数据类型。 整数,浮点数,字符串,字符被称为原始,因为它们保存一种类型的数据。 类,结构,对象称为非原始数据类型。


JavaTypeResolverDefaultImpl 不生效 java primitive type_java

Primitive Data Types 原始数据类型

复合或非原始类型(Composite or Non-Primitive Types)

Composite or Non-primitive types are used to store complex data which is generally multiple types in a single type like Class, Struct, Objects, etc. For example, a class which has members like name, age, etc is a non-primitive type. But in some cases, non-primitive types can contain other non-primitive types.

复合或非原始类型用于存储复杂数据,这些数据通常是单个类型(例如Class,Struct,Objects等)中的多种类型。例如,具有诸如name,age等成员的类是非原始类型。 但是在某些情况下,非原始类型可以包含其他非原始类型。

(Number Types)

Java provides 6 different number types to represent a different type of numbers. As general there is two main number type which is integer and floating-point but there are also derivatives.

Java提供了6种不同的数字类型来表示不同类型的数字。 通常,有两种主要的数字类型,它们是整数和浮点数,但也有导数。

(Integer Type)

An integer is used to store integers and requires 4 bytes in memory even the number do not require it. An integer can be between  -2147483648 to 2147483647 which is generally enough for most of the scenarios and cases. int keyword is used to define the integer type in Java. Here are some examples of Java integer data type.

整数用于存储整数,并且在内存中需要4个字节,即使该数字不需要它也是如此。 整数可以在-2147483648至2147483647之间,对于大多数情况和情况而言,通常就足够了。 int关键字用于在Java中定义整数类型。 这是Java整数数据类型的一些示例。

int myNum = 100000;

System.out.println(myNum);

(Byte Type)

If we want to store very little numbers like 50, 7, we can use the byte data type. The byte data type can store numbers between 128 and -127 . Byte data type uses 1 byte in memory. byte keyword is used to define the byte data type.

如果我们要存储很少的数字(例如50、7),则可以使用字节数据类型。 字节数据类型可以存储128到-127之间的数字。 字节数据类型在内存中使用1个字节。 byte关键字用于定义字节数据类型。

int myByte= 126; 

System.out.println(myByte);

(Short Type)

As integer can store a range of number but in some cases, we may need to store some little numbers like 598, 15290, etc. Short type can store numbers between -32768 to 32767. In order to define the short type short keyword is used. The short data type can be converted into other number types like integer, long, floating-point, double without a problem.

由于整数可以存储一定范围的数字,但是在某些情况下,我们可能需要存储一些小数字,例如598、15290等。short类型可以存储-32768到32767之间的数字。为了定义short类型,使用了short关键字。 短数据类型可以转换为其他数字类型,例如整数,长整数,浮点数,双精度数而没有问题。

short myNum = 5000;

System.out.println(myNum);

(Long Type)

Well, integer type provides a long-range to define numbers. But in some cases, this may not enough for our usage and calculations. Long type can store numbers between -9223372036854775808 to 9223372036854775807. This may be very useful scientific or economy use cases. Long type can be defined with the long keyword. Long type can be converted to integer and short if it is in the range of these types if not we will see some error

好吧,整数类型提供了一个远程定义数字的方法。 但是在某些情况下,这可能不足以用于我们的使用和计算。 长型可以存储在-9223372036854775808至9223372036854775807之间的数字。这可能是非常有用的科学或经济用例。 可以使用long关键字定义long类型。 长整型可以转换为整数,短整型(如果不在这些类型的范围内),我们将看到一些错误

long myNum = 15000000000L;

System.out.println(myNum);

(Floating Number Types)

Integers are useful but some calculations require some floating-point numbers. There are two types of floating-point numbers in the Java programming language. The only difference is the precision of the types.

整数很有用,但某些计算需要一些浮点数。 Java编程语言中有两种类型的浮点数。 唯一的区别是类型的精度。


LEARN MORE  Php - Boolean Variable Types , True, False


了解更多信息-布尔变量类型,True,False


(Floating Point Type)

Floating point type is the first floating-point type. We can use float in order to define the floating-point type. Floating type store floating numbers between 3.4e−038 to 3.4e+038. We can also use f postfix at the end of the float value in order to specify it is a floating-point number.

浮点类型是第一个浮点类型。 我们可以使用float来定义浮点类型。 浮点类型存储浮点数在3.4e-038到3.4e + 038之间。 我们还可以在浮点值的末尾使用f后缀,以指定它是浮点数。

float myNum = 5.75f;

float percentage = 10.8;

System.out.println(myNum);

(Double Type)

In some cases, the floating-point type may not be enough to store given floating-point number. Double can store numbers between 1.7e−308 to 1.7e+308 . We will use double keyword to define a double data type. A double type variable requires 8-byte memory which is expensive and should be used carefully. We can postfix or end d to the end of the floating data to explicitly specify it is double.

在某些情况下,浮点类型可能不足以存储给定的浮点数。 Double可以存储1.7e−308到1.7e + 308之间的数字。 我们将使用double关键字定义double数据类型。 双精度型变量需要8字节的内存,这很昂贵,应谨慎使用。 我们可以将fix或end d附加到浮动数据的末尾,以明确指定其为double。

double myNum = 19.99d;

System.out.println(myNum);

(Boolean Type)

Java can store logical values false and true. Boolean type is used to store logical values. We will use boolean keyword to define a logical value and true and false keyword to set value.

Java可以存储逻辑值falsetrue 。 布尔类型用于存储逻辑值。 我们将使用boolean关键字定义逻辑值,并使用truefalse关键字设置值。

boolean isPoftutFun = true;

boolean isSaladTasty = false;

System.out.println(isPoftutFun );     // Outputs true

System.out.println(isSaladTasty );   // Outputs false

角色类型(Character Type)

The character type is used to store a single character. char keyword is used to define the character type. Character values generally surrounded by the single quotes but double quotes can be also used. Char variable can be created by providing the character directly like 'A' or with its ASCII value like 65.

字符类型用于存储单个字符。 char关键字用于定义字符类型。 字符值通常用单引号引起来,但也可以使用双引号。 可以通过直接提供字符(例如'A'或提供其ASCII值(例如65来创建Char变量。

char grade = 'A';

System.out.println(myGrade);

char d = 68, e = 69, f = 70;

System.out.println(d);

System.out.println(e);

System.out.println(f);

(Non-Primitive Type String)

The string is not a primitive type and one of the examples to non-primitive data types. The string data type is used to store multiple characters or a text in a single variable. We can also define a single array of characters. The string variable data can be defined inside double quotes or single quotes.

该字符串不是原始类型,并且是非原始数据类型的示例之一。 字符串数据类型用于在单个变量中存储多个字符或文本。 我们还可以定义单个字符数组。 可以在双引号或单引号内定义字符串变量数据。

String greeting = "Hello Poftut";

System.out.println(greeting);