Home>Article>Java> What are the eight major data types in Java

What are the eight major data types in Java

青灯夜游
青灯夜游 Original
2023-02-02 10:17:23 48481browse

Eight major data types: 1. byte (bit), the maximum data storage capacity is 255; 2. short (short integer), the maximum data storage capacity is 65536; 3. int (integer), the maximum data storage capacity It is 2 to the 32nd power minus 1; 4. long (long integer), the maximum data storage capacity is 2 to the 64th power minus 1; 5. float (single-precision floating number), f must be added after the number when assigning a value directly. or F; 6. double (double precision); 7. boolean (Boolean type); 8. char (character).

What are the eight major data types in Java

The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.

1. Java’s eight basic data types

There are eight basic Java types. Basic types can be divided into three categories: character type char, boolean type boolean and numerical value Types byte, short, int, long, float, double. Numeric types can be divided into integer types byte, short, int, long and floating point types float and double. There are no unsigned numerical types in JAVA. Their value range is fixed and will not change with changes in the machine hardware environment oroperating system. In fact, there is another basic type void in JAVA, which also has a corresponding packaging class java.lang.Void, but we cannot directly operate on them. 8 Medium type representation range is as follows:

byte: 8 bits, the maximum amount of stored data is 255, and the stored data range is between -128~127.

short: 16 bits, the maximum storage capacity is 65536, and the data range is -32768~32767.

#int: 32 bits, the maximum data storage capacity is 2 to the 32nd power minus 1, the data range is negative 2 to the 31st power to positive 2 to the 31st power minus 1 .

long: 64 bits, the maximum data storage capacity is 2 to the power of 64 minus 1, the data range is negative 2 to the power of 63 to positive 2 to the power of 63 minus 1 .

float: 32 bits, data range is 3.4e-45~1.4e38, f or F must be added after the number when assigning directly.

double: 64 bits, data range is 4.9e-324~1.8e308, d or D can be added or not added when assigning.

boolean: There are only two values: true and false.

char: 16 bits, stores Unicode code, assign value with single quotes.

Java determines the size of each simple type. These sizes do not change with changes in machine architecture. This immutability of size is one of the reasons why Java programs are highly portable. The following table lists the simple types defined in Java, the number of binary digits they occupy, and the corresponding wrapper classes.

For the value range of the basic types of numerical types, we do not need to be forced to remember, because their values have been defined in the corresponding packaging class in the form of constants. For example:

Basic type byte Binary digits: Byte.SIZE Minimum value: Byte.MIN_VALUE Maximum value: Byte.MAX_VALUE

Basic type short Binary digits: Short.SIZE Minimum value: Short .MIN_VALUE Maximum value: Short.MAX_VALUE

Basic type char Binary digits: Character.SIZE Minimum value: Character.MIN_VALUE Maximum value: Character.MAX_VALUE

Basic type double Binary digits: Double .SIZE minimum value: Double.MIN_VALUE maximum value: Double.MAX_VALUE

Note: The minimum values of float and double types are not the same as the values of Float.MIN_VALUE and Double.MIN_VALUE. In fact, Float.MIN_VALUE and Double.MIN_VALUE refer to the smallest positive number that can be represented by float and double types respectively. That is to say, there is a situation where the float type cannot represent the value between 0 and ±Float.MIN_VALUE, and the double type cannot represent the value between 0 and ±Double.MIN_VALUE. This is not surprising since values in these ranges are outside their range of precision.

The minimum and maximum values of Float and Double are output in scientific notation. The "E number" at the end indicates how many times the number before E should be multiplied by 10. For example, 3.14E3 is 3.14×1000=3140, and 3.14E-3 is 3.14/1000=0.00314.

Java basic types are stored on the stack, so their access speeds are faster than instance objects of corresponding wrapper classes stored in the heap. Starting from Java5.0 (1.5), the JAVA virtual machine (JavaVirtual Machine) can complete automatic conversion between basic types and their corresponding wrapper classes. Therefore, we use their wrapper classes just like basic types when doing assignments, parameter transfers, and mathematical operations, but this does not mean that you can call methods that are only available in their wrapper classes through basic types. In addition, the wrapper classes of all basic types (including void) use final modification, so we cannot inherit them to extend new classes, nor can we override any of their methods.

Advantages of basic types: data storage is relatively simple and operation efficiency is relatively high

Advantages of packaging classes: some are easy, for example, the elements of a collection must be object types, which satisfies everything in Java The idea of objects

2. Constants in Java

Hexadecimal integer constants: When expressed in hexadecimal, they need to start with 0x or 0X. Such as 0xff,0X9A.

Octal integer constant: Octal must start with 0, such as 0123, 034.

Long integer type: Long integer type must end with L, such as 9L, 342L.

Floating point constants: Since the default type of decimal constants is double, f (F) must be added after the float type. Variables with decimals also default to double type.

For example:

float f; f=1.3f;//必须声明f。

Character constants: Character constants need to be enclosed in two single quotes (note that string constants are enclosed in two double quotes). Characters in Java take up two bytes. Some commonly used escape characters:

①\r means accepting keyboard input, which is equivalent to pressing the Enter key;

②\n表示换行;

③\t表示制表符,相当于Table键;

④\b表示退格键,相当于Back Space键;

⑤\'表示单引号;

⑥\''表示双引号;

⑦\\表示一个斜杠\。

3. 数据类型之间的转换

1).简单类型数据间的转换,有两种方式:自动转换和强制转换,通常发生在表达式中或方法的参数传递时。

自动转换

具体地讲,当一个较"小"数据与一个较"大"的数据一起运算时,系统将自动将"小"数据转换成"大"数据,再进行运算。而在方法调用时,实际参数较"小",而被调用的方法的形式参数数据又较"大"时(若有匹配的,当然会直接调用匹配的方法),系统也将自动将"小"数据转换成"大"数据,再进行方法的调用,自然,对于多个同名的重载方法,会转换成最"接近"的"大"数据并进行调用。这些类型由"小"到"大"分别为 (byte,short,char)--int--long--float—double。这里我们所说的"大"与"小",并不是指占用字节的多少,而是指表示值的范围的大小。

①下面的语句可以在Java中直接通过:

byte b; int i=b; long l=b; float f=b; double d=b;

②如果低级类型为char型,向高级类型(整型)转换时,会转换为对应ASCII码值,例如

char c='c'; int i=c; System.out.println("output:"+i); 输出:output:99;

③对于byte,short,char三种类型而言,他们是平级的,因此不能相互自动转换,可以使用下述的强制类型转换。

short i=99 ; char c=(char)i; System.out.println("output:"+c); 输出:output:c;

强制转换

将"大"数据转换为"小"数据时,你可以使用强制类型转换。即你必须采用下面这种语句格式: int n=(int)3.14159/2;可以想象,这种转换肯定可能会导致溢出或精度的下降。

2)表达式的数据类型自动提升, 关于类型的自动提升,注意下面的规则。

①所有的byte,short,char型的值将被提升为int型;

②如果有一个操作数是long型,计算结果是long型;

③如果有一个操作数是float型,计算结果是float型;

④如果有一个操作数是double型,计算结果是double型;

例, byte b; b=3; b=(byte)(b*3);//必须声明byte。

3)包装类过渡类型转换

一般情况下,我们首先声明一个变量,然后生成一个对应的包装类,就可以利用包装类的各种方法进行类型转换了。例如:

①当希望把float型转换为double型时:

float f1=100.00f; Float F1=new Float(f1); double d1=F1.doubleValue();//F1.doubleValue()为Float类的返回double值型的方法

②当希望把double型转换为int型时:

double d1=100.00; Double D1=new Double(d1); int i1=D1.intValue();

简单类型的变量转换为相应的包装类,可以利用包装类的构造函数。即:Boolean(boolean value)、Character(char value)、Integer(int value)、Long(long value)、Float(float value)、Double(double value)

而在各个包装类中,总有形为××Value()的方法,来得到其对应的简单类型数据。利用这种方法,也可以实现不同数值型变量间的转换,例如,对于一个双精度实型类,intValue()可以得到其对应的整型变量,而doubleValue()可以得到其对应的双精度实型变量。

4)字符串与其它类型间的转换

其它类型向字符串的转换

①调用类的串转换方法:X.toString();

②自动转换:X+"";

③使用String的方法:String.volueOf(X);

字符串作为值,向其它类型的转换

①先转换成相应的封装器实例,再调用对应的方法转换成其它类型

例如,字符中"32.1"转换double型的值的格式为:new Float("32.1").doubleValue()。也可以用:Double.valueOf("32.1").doubleValue()

②静态parseXXX方法

String s = "1"; byte b = Byte.parseByte( s ); short t = Short.parseShort( s ); int i = Integer.parseInt( s ); long l = Long.parseLong( s ); Float f = Float.parseFloat( s ); Double d = Double.parseDouble( s );

③Character的getNumericValue(char ch)方法

5)Date类与其它数据类型的相互转换

整型和Date类之间并不存在直接的对应关系,只是你可以使用int型为分别表示年、月、日、时、分、秒,这样就在两者之间建立了一个对应关系,在作这种转换时,你可以使用Date类构造函数的三种形式:

①Date(int year, int month, int date):以int型表示年、月、日

②Date(int year, int month, int date, int hrs, int min):以int型表示年、月、日、时、分

③Date(int year, int month, int date, int hrs, int min, int sec):以int型表示年、月、日、时、分、秒

在长整型和Date类之间有一个很有趣的对应关系,就是将一个时间表示为距离格林尼治标准时间1970年1月1日0时0分0秒的毫秒数。对于这种对应关系,Date类也有其相应的构造函数:Date(long date)。

获取Date类中的年、月、日、时、分、秒以及星期你可以使用Date类的getYear()、getMonth()、getDate()、getHours()、getMinutes()、getSeconds()、getDay()方法,你也可以将其理解为将Date类转换成int。

而Date类的getTime()方法可以得到我们前面所说的一个时间对应的长整型数,与包装类一样,Date类也有一个toString()方法可以将其转换为String类。

有时我们希望得到Date的特定格式,例如20020324,我们可以使用以下方法,首先在文件开始引入,

import java.text.SimpleDateFormat; import java.util.*; java.util.Date date = new java.util.Date(); //如果希望得到YYYYMMDD的格式 SimpleDateFormat sy1=new SimpleDateFormat("yyyyMMDD"); String dateFormat=sy1.format(date); //如果希望分开得到年,月,日 SimpleDateFormat sy=new SimpleDateFormat("yyyy"); SimpleDateFormat sm=new SimpleDateFormat("MM"); SimpleDateFormat sd=new SimpleDateFormat("dd"); String syear=sy.format(date); String smon=sm.format(date); String sday=sd.format(date);

总结:只有boolean不参与数据类型的转换

(1).自动类型的转换:a.常数在表数范围内是能够自动类型转换的

b.数据范围小的能够自动数据类型大的转换(注意特例)

int到float,long到float,long到double 是不会自动转换的,不然将会丢失精度

c.引用类型能够自动转换为父类的

d.基本类型和它们包装类型是能够互相转换的

(2).强制类型转换:用圆括号括起来目标类型,置于变量前

4.Java引用类型

Java有 5种引用类型(对象类型):类 接口 数组 枚举 标注

引用类型:底层结构和基本类型差别较大

JVM的内存空间:(1). Heap 堆空间:分配对象 new Student()

(2). Stack 栈空间:临时变量 Student stu

(3).Code 代码区 :类的定义,静态资源 Student.class

eg:Student stu = new Student(); //new 在内存的堆空间创建对象

stu.study(); //把对象的地址赋给stu引用变量

上例实现步骤:a.JVM加载Student.class 到Code区

b.new Student()在堆空间分配空间并创建一个Student实例;

c.将此实例的地址赋值给引用stu, 栈空间;

更多编程相关知识,请访问:编程教学!!

##Binary digits 1 8 16 16 32 64 32 64 ##Double Void
#Simple type

boolean

byte

char

short

Int

long

float

double

void

--

Wrapper class

Boolean

Byte

Character

Short

Integer

Long

##Float

The above is the detailed content of What are the eight major data types in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn