Object-Oriented Programming: simple Java Data Types

Source: Internet
Author: User
Object-Oriented Programming: Java's simple data type-general Linux technology-Linux programming and kernel information. For more information, see the following. Data types are abstract expressions of memory locations. Programmers can use multiple data types: Some defined by programming languages, some defined by external libraries, and some defined by programmers. Many programming languages depend on the specific computer type and the specific compilation Implementation of Data Type attributes, such as the size of word and integer data types. On the other hand, Java virtual machines are responsible for defining various aspects of their built-in data types. This means that no matter what low-level operating system the Java Virtual Machine (JVM) runs on, the data type attributes are identical.


Simple data type


A simple data type cannot be simplified. It is a built-in data type defined by a programming language that represents real numbers, characters, and integers. Larger and 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, the int type is sometimes stored in 32-bit hardware registers. Java provides several types of simple data types to indicate numbers and characters.

Simple data types are generally divided into the following types: real numbers, integers, characters, and boolean values. These categories contain multiple simple types. For example, Java defines two simple types: float and double, both of which belong to the real number category, and the other four simple types: byte, short, int, and long all belong to the integer category. In addition, there is a simple type of char that is attributed to the character type. Boolean has only one simple type: boolean. Table A lists Java's simple data types in detail.


Table A Java simple data type

Simple type size range/precision
Float 4-byte 32-bit IEEE 754 single precision
Double 8-byte 64-bit IEEE 754 dual precision
Byte 1 byte-128 to 127
Short 2 bytes-32,768 to 32,767
Int 4-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-byte full Unicode Character Set
Boolean 1-bit True or false



Java simple data type

All numeric variables in Java are signed. Java does not allow arbitrary conversion between data types. Only numeric variables can be converted. For example, boolean cannot be converted to another data type, 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 canceled in Java.

Java's simple data type is not an object. To use object-oriented methods to treat Java simple data types, You Need To encapsulate them with classes first.


Encapsulation class

Java also provides built-in encapsulation classes such as Byte, Short, Boolean, Character, Integer, Double, Float, and Long. These wrapper Classes provide intuitive and practical methods. For example, the Byte, Float, Integer, Long, and Double classes all have the doubleValue () method, through which the values stored in the instance of the class can be converted to the Double type. In addition, all encapsulation classes provide static valueOf (String s) Methods to convert a given String to a corresponding simple type. The code in listing A demonstrates some usage of these encapsulation classes.


Simple data type Initialization

In Java, a simple data type is automatically initialized as the default value when declared as a member variable of the class, unless explicitly declared. When a simple data type is set to a method, local variables are not automatically initialized and the compiler throws the following error message "Variable x may not have been initialized. (The x variable is not initialized) "table B defines the default value of the Java simple data type.


Table B default values of Java simple data types

Type
Default Value

Boolean
False

Byte
0

Short
0

Int
0

Long
0

Char
'\ U000000'

Float
0.0

Double
0.0




Default values of Java simple data types

The code in listing B shows that all Java simple data types Use the Initialization class member variables. In this example, an int variable is declared 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 problematic code line (the code that references the uninitialized variable and causes the error) is deleted or commented out. After the program is successfully compiled and executed, the following results are displayed:



Byte: 0 short: 0int: 0 long: 0 float: 0.0 double: 0.0 char: 0 boolean: false


You can also explicitly initialize a member variable to another value, 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 from other languages, allowing programmers to focus on other issues. In the next article, we will continue to discuss some frequently used complex data types defined in Java and learn how to use them to handle typical programming problems.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.