This article is a mirror article of machine translation, please click here to jump to the original article.

View: 16754|Reply: 0

[Source] Basic data types built into Java

[Copy link]
Posted on 4/7/2015 4:19:13 PM | | |
Data types are abstract expressions of memory locations. Programmers can leverage a variety of data types: some defined by programming languages, some defined by external libraries, and others defined by programmers. Many programming languages rely on specific computer types and specific compiled implementations of data type properties, such as the size of word and integer data types. On the other hand, Java's virtual machine is responsible for defining all aspects of its built-in data types. This means that no matter how low-level operating system the Java Virtual Machine (JVM) is running, the properties of the data types are exactly the same.

Simple data types
Simple data types are data types that can no longer be simplified, built-in data types, defined by programming languages, and represent real numbers, characters, and integers. Larger, more complex data types can be defined by a combination of simple data types. In most cases, simple data types have their hardware equivalents. For example, int simple types are sometimes stored in 32-bit hardware registers. Java provides several classes of simple data types representing numbers and characters.

Simple data types are typically divided into the following categories: real numbers, integers, characters, and booleans. These categories in turn include a variety of simple types. For example, Java defines two simple types: float and double, which belong to the real number category, and the other four simple types: byte, short, int, and long all belong to the integer category. There is also a simple type, char, which is attributed to the character type. There is only one simple type of boolean category: boolean. Table A details the simple data types in Java.

Table A Java simple data types
Simple type size range/precision
float 4 bytes 32-bit IEEE 754 single-precision
double 8 bytes 64-bit IEEE 754 double precision
Byte 1 byte -128 to 127
- Short 2 bytes - 32,768 to 32,767
int 4 bytes - 2,147,483,648 to 2,147,483,647
Long 8 bytes - 9,223,372,036,854,775,808 to 9,223,372,036, 854,775,807
char 2 bytes of the entire Unicode character set
boolean 1 bit True or false

Simple data types in Java
All numerical variables in Java are signed, and Java does not allow arbitrary conversion between data types. Only type conversion is possible between numeric variables. For example, a boolean cannot be converted to other data types, and other data types cannot be converted to boolean.

Because Java's simple data types are accurately defined and direct memory access is not allowed, the sizeof operator is removed in the Java language.

Java's simple data types are not objects. In order to take an object-oriented approach to Java simple data types, you need to first encapsulate them in classes.

Package
Java also provides built-in encapsulation classes such as Byte, Short, Boolean, Character, Integer, Double, Float, and Long. These wrapper classes provide a very intuitive and practical approach. For example, the Byte, Float, Integer, Long, and Double classes all have a doubleValue() method that allows you to convert values stored in instances of the class to Double types. Also, all encapsulated classes provide a static valueOf(Strings) method to convert a given String to its corresponding simple type. The code in Listing A demonstrates some of the usage of these encapsulation classes.

Simple data type initialization
In Java, simple data types are automatically initialized as defaults when declared as member variables of a class, unless explicitly declared. Simple data types are methods that declare local variables that are not automatically initialized and cause the compiler to throw an error message like the following: "Variable x may not have been initialized." Table B defines the default values for simple data types in Java.
Table B Defaults for Java Simple Data Types
type
Default
boolean
false
Byte
0
short
0
int
0
Long
0
Char
′/u0000′
Float
0.0
double
0.0

Default default values for Java simple data types
The code in Listing B shows that all Java simple data types use member variables of the Initialization class. This example also shows that an int variable is declared locally in the constructor of the Initialization class. Without modifying the above code, the compiler will throw an error when compiling the above code.
Once the line of code that caused the problem (the code that references an uninitialized variable causing the error) is deleted or commented out. After the program is successfully compiled and executed, the following results are displayed:

byte: 0
short: 0
int: 0
long: 0
float: 0.0
double: 0.0
char: 0
boolean: false
We can also explicitly initialize member variables to other values, as shown in the following code:
byte b = 5;
short s = 123;
int i = 1234;
long l = 12345;
float f = 123.45f;
double d = 12345.678;
char c = ′A′;
boolean z = true;

Summary
Java defines a full set of simple data types. In addition, Java removes hardware and compiler dependencies that become the language, allowing programmers to divert their attention to other issues




Previous:JDK, JRE, JVM differences and connections
Next:Photoshop running, please uninstall and reinstall this product, how to solve error code 16?
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com