概述
非基元类型数据结构
Java is a strongly typed language, that means all the variables must first be declared before we can use it. Declaring a variable in java includes type and name with optional value assignment. If no value is assigned, the variable holds the default value. For primitive types, there are different default values but it’s always NULL for Object data types.
Java是一种强类型语言,这意味着在使用它之前必须先声明所有变量。 在Java中声明变量包括类型和名称以及可选的值分配。 如果未分配任何值,则该变量将保留默认值。 对于基本类型,有不同的默认值,但对于Object数据类型,它始终为NULL。
Java原始数据类型 (Java Primitive Data Types)
Java programming language contains eight primitive data types. Four primitive data types are for integer values – byte, short, int and long. Two primitive data types are for floating type decimal values – float and double. One is for characters – char and one is for the condition – boolean. Java programming language also comes with Wrapper classes for all these primitive data types.
Java编程语言包含八种原始数据类型。 四种原始数据类型用于整数值–字节,短,整数和长整数。 浮点型十进制值有两种原始数据类型-浮点和双精度。 一种是字符(char),另一种是条件(boolean)。 包装器类还为所有这些原始数据类型提供了Java编程语言。
Below table shows all these primitive data types with size, range, default value and different ways to assign them.
下表显示了所有这些原始数据类型,包括大小,范围,默认值和不同的分配方式。
Type | Size | Range | Default Value | Examples |
---|---|---|---|---|
boolean | 1 bit | NA | false | boolean bool = true; |
char | 16 bits | Unicode Characters | ‘u0000’ or 0, which is nothing but a white space | char c = ‘A’; char c = ‘u0041’; char c = 65; char c = ‘t’; |
byte | 8 bits | [-128,127] or [-2^7 to 2^7-1] | 0 | byte b = 10; byte b = 0b010; |
short | 16 bits | [-32768,32767] | 0 | short s = 32; short s = ‘A’; |
int | 32 bits | [-2147483648,2147483647] | 0 | int i = 10; int i = ‘A’; |
long | 64 bits | [-2^63,2^63-1] | 0 | long l = 3200L; long l = 3200; |
float | 32 bits | [-3.4E38, 3.4E38] | 0.0f | float f = (float) 12.34; float f = 12.34f; |
double | 64 bits | [-1.7E308, 1.7E308] | 0.0 | double d = 12.34; |
类型 | 尺寸 | 范围 | 默认值 | 例子 |
---|---|---|---|---|
布尔值 | 1位 | 不适用 | 假 | 布尔布尔= true; |
烧焦 | 16位 | Unicode字符 | ' u0000'或0,即 只是一个 空格 | char c ='A'; char c =' u0041'; 字符c = 65; char c =' t'; |
字节 | 8位 | [-128,127]或 [-2 ^ 7至2 ^ 7-1] | 0 | 字节b = 10; 字节b = 0b010; |
短 | 16位 | [-32768,32767] | 0 | 短s = 32; short s ='A'; |
整型 | 32位 | [-2147483648,2147483647] | 0 | 整数i = 10; int i ='A'; |
长 | 64位 | [-2 ^ 63,2 ^ 63-1] | 0 | 长l = 3200L; 长l = 3200; |
浮动 | 32位 | [-3.4E38、3.4E38] | 0.0分 | 浮点数f =(float)12.34; 浮点f = 12.34f; |
双 | 64位 | [-1.7E308、1.7E308] | 0.0 | 双d = 12.34; |
Below is a simple java program showing different ways to declare primitive data types – look closely for char and what happens when an int is converted to byte through explicit casting.
下面是一个简单的Java程序,显示了声明原始数据类型的不同方法-仔细查看char以及将int通过显式转换转换为字节时发生的情况。
package com.journaldev.collections;
public class DataTypes {
public static void main(String[] args) {
char c = 'A';
System.out.println(c); //prints A
char c1 = 'u0041';
System.out.println(c1); //prints A
char c2 = 0;
System.out.println("Default Value:"+c2+":"); // prints Default Value: :
char c3 = 65;
System.out.println(c3); //prints A
char c4 = 't';
System.out.println("Tab Start:"+c4+":End"); //prints Tab Start: :End
byte b = 10;
System.out.println(b); //prints 10
byte b1 = (byte) 200;
System.out.println(b1); // prints -56
//<0...>_11001000 (int), converted to 11001000 (byte) by stripping leading 24 bits
// since left most bit is 1, we need to find the value
// Ones complement 11001000 -1 = 11000111
//invert digits 00111000 i.e 56, hence printing -56
b1 = (byte) 0b11001000;
System.out.println(b1); //prints -56
byte b2 = (byte) 320; //256+64 i.e 00000000_00000000_00000001_01000000, byte 01000000
//since leading bit is 0, nothing is required to determine value
System.out.println(b2); //prints 64
short s = 32;
short s1 = 'A'; //implicit char to short conversion
System.out.println(s1); //prints 65
int i = 'A';
System.out.println(i); //prints 65
long l = 3200L;
long l1 = 3200;
float f = 12.34f;
//Examples
byte x, y = 1, z = 2;
x = (byte) (y + z);
}
}
在数字文字中使用下划线 (Using Underscore in Numeric Literals)
From Java 7 onwards, we can use underscore in numeric literals such as long ccNum = 1234_5678_9101_1121L;
. You can read more about them at Underscores in Numeric Literals.
从Java 7开始,我们可以在数字文字中使用下划线,例如long ccNum = 1234_5678_9101_1121L;
。 您可以在“数字文字”中的“ 下划线”中阅读有关它们的更多信息。
二进制文字 (Binary Literals)
From Java 7 onwards, the integral types (byte, short, int, and long) can also be expressed using the binary number system. We need to prefix the number with 0b or 0B. Some of the examples are;
从Java 7开始,整数类型(字节,短型,整型和长型)也可以使用二进制数字系统表示。 我们需要给数字加上0b或0B前缀。 一些例子是;
// An 8-bit 'byte' value:
byte aByte = (byte)0b00100001;
// A 16-bit 'short' value:
short aShort = (short)0b1010000101000101;
That’s all for primitive data types in java. Please look into the program carefully to understand what happens when we perform casting.
这就是Java中原始数据类型的全部内容。 请仔细查看程序,以了解执行铸造时会发生什么。
翻译自: https://www.journaldev.com/6431/java-data-types-primitives-and-binary-literals
非基元类型数据结构
最后
以上就是激情纸飞机为你收集整理的非基元类型数据结构_Java数据类型–基元和二进制文字的全部内容,希望文章能够帮你解决非基元类型数据结构_Java数据类型–基元和二进制文字所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复