官术网_书友最值得收藏!

Types

Each variable has one type. In Java, there are two major groups of type: primitive and reference types. The primitive types are predefined, and you cannot define or create a new primitive type. There are eight primitive types: byte, short, int, long, float, double, boolean, and char.

The first four types, byte, short, int, and long, are signed numeric integer types, capable of storing positive and negative numbers on 8, 16, 32, and 64 bits.

The float and double types store floating point numbers on 32 and 64 bits in the IEEE 754 floating-point format.

The boolean type is a primitive type that can only be true or false.

The char type is a character data type that stores a single 16-bit Unicode character.

For each primitive type, there is a class that can store the same type of value. When a primitive type has to be converted to the matching class type it is done automatically. It is called auto boxing. These types are Byte, Short, Integer, Long, Float, Double, Boolean, and Character. Take, for example, the following variable declaration:

Integer a = 113;

This converts the value 113, which is an int number, to an Integer object.

These types are part of the runtime, and also part of the language. Although there is no primitive counterpart of it, there is a very important and ubiquitous class that we have already used: String. A string contains characters.

The major differences between primitive types and objects are that primitive types cannot be used to invoke methods, but they consume less memory. The difference between the memory consumption and its consequences for speed is important in the case of arrays.

主站蜘蛛池模板: 北碚区| 乌兰浩特市| 南京市| 龙川县| 红原县| 贵州省| 花垣县| 娄底市| 清新县| 平舆县| 绥宁县| 苍山县| 怀远县| 莫力| 蒲江县| 红安县| 盐山县| 石泉县| 宜章县| 台中县| 雷州市| 调兵山市| 石家庄市| 嵩明县| 嵊州市| 集安市| 济宁市| 秀山| 西乌珠穆沁旗| 武穴市| 沅江市| 河南省| 台江县| 贡嘎县| 旬邑县| 景泰县| 勃利县| 株洲县| 曲松县| 盈江县| 南陵县|