- Java 9 Programming By Example
- Peter Verhas
- 280字
- 2021-07-02 23:37:33
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.
- Mastering Entity Framework Core 2.0
- Beginning C++ Game Programming
- 工程軟件開發(fā)技術(shù)基礎(chǔ)
- Rust編程:入門、實戰(zhàn)與進階
- Vue.js快跑:構(gòu)建觸手可及的高性能Web應(yīng)用
- C語言程序設(shè)計教程(第2版)
- Instant Typeahead.js
- JavaScript 程序設(shè)計案例教程
- 青少年P(guān)ython編程入門
- Mastering Apache Spark 2.x(Second Edition)
- JavaCAPS基礎(chǔ)、應(yīng)用與案例
- Azure Serverless Computing Cookbook
- PHP 7從零基礎(chǔ)到項目實戰(zhàn)
- Angular Design Patterns
- Java高并發(fā)編程詳解:深入理解并發(fā)核心庫