Java Data types

Source: Internet
Author: User
Tags java se

First, the data type

There are basically 8 basic data types in Java, byte,short,int,long,char,boolean,float,double, respectively, as follows:

1. Boolean: The data value is only true or FALSE for logical calculations.

2. Char:char type (character type) data occupies 2 bytes in memory. Char data is used to represent characters in the usual sense, each character occupies 2 bytes, the Java character is Unicode encoded, and its first 128 bytes of encoding and ASCII-compatible characters are stored in \u0000~\uffff, When defining character-type data, be aware of the addition of ', ' 1 ' for the character ' 1 ' instead of the value 1,

3. Byte:byte type (byte type) data occupies 1 bytes in memory, representing the storage data range: -128~127.

4. Short:short type (short integer) data occupies 2 bytes in memory.

5. Int:int type (integer) data occupies 4 bytes in memory.

6. Long:long type (Long integer) data occupies 8 bytes in memory.

7. Float:float (single-precision floating-point type) data occupies 4 bytes in memory. (float precision is 7-8 bits)

8. Double:double (double-precision floating-point) data occupies 8 bytes in memory.

all basic data types in Java have a fixed storage range and the size of the occupied memory space, and are not affected by the specific operating system to ensure the portability of Java programs. The Shaping data defaults to the int data type, the float type defaults to the double data type, and if you want to represent long data or float type data, you will have a compilation problem if you want to add L, l, or F or F to the corresponding value .

In other languages, such as the C language, character data and integer data can be converted to each other, are stored in ASCII code, you can use the character data as Integer data to view.

Ii. conversions between basic data types

1. The Boolean type cannot be converted to any other data type.

2, automatic type conversion : Small capacity of the data type can be automatically converted to large-capacity data types, such as byte-short-int-long-float-double. BYTE, short, int do not convert to each other, and the three of them are converted to the int type when they are calculated.

Example: int a=257;byte b= (byte) A; in Java, the int type is 4 bytes and byte is one byte, so the conversion of int to byte type will cause bit loss, and the lower 4 bits of int are the values of byte type. The value of the int variable is 257, the corresponding binary is 100000001, the second 8 bits are 00000001, the first 0 represents the sign bit, and the positive number, so the value of variable B is 1.

Note: In a computer, where data is stored by memory addresses to differentiate between different data, memory addresses are represented in hexadecimal numbers. In the computer storage unit, an ASCII value occupies one byte (8 bits), where the highest sign bit. In fact, the binary values in the computer are expressed in complementary form , the complement of positive numbers and the same form of the original code, the complement of negative numbers is the absolute value of the number of binary form, bitwise negation plus one. 】

int a=128;byte b= (byte) A; At this point the value of B is-128. Because the variable a corresponds to the binary is 10000000, corresponding to the first bit of the sign is 1, then A is a negative number, the complement of the negative is still 10000000, so the value of B is-128.

3. coercion type conversion: When a large data type is converted to a small capacity data type, it is necessary to add a cast character, but this may result in reduced precision or data overflow, be careful.

Three, Object wrapper

Java, in order to be able to treat basic types (primitive type) as objects, and to connect related methods, Java provides the appropriate wrapper class for each abridged type, so that the basic type can be converted to an object for processing . The corresponding relational table is as follows:

Basic data types Packing class
Byte (bytes) Java.lang.Byte
char (character) Java.lang.Character
Short (shorter integer type) Java.lang.Short
int (integral type) Java.lang.Integer
Long (integer) Java.lang.Long
Float (float type) Java.lang.Float
Double (dual precision) Java.lang.Double
Boolean (Boolean) Java.lang.Boolean


Use of wrapper classes

The class called wrapper class , also called wrapper, such as Long,integer, is designed to provide an object instance as a shell, package its basic type into this object, and then provide many ways to manipulate the data. Making object-oriented programming easier.

Conversions between strings, wrapper classes, raw data classes

Iv. Packing and unpacking

Before Java SE 5.0, an int could be wrapped to an integer class before it could be manipulated. JDK 5.0 provides automatic boxing (boxing) and unpacking (unboxing) functionality for basic data types.

Boxing: Wrapper class object that wraps the base data type to the scale.

unboxing: Converts the wrapper class object to the corresponding base data type.

At compile time, the Java compiler determines whether boxing and unpacking is done based on the syntax of the source code. Automatic boxing and unpacking can also be done during operations.

Generic generics are just compile-time concepts that are used by the compiler for syntax checking (because programmers need a type-safe collection, this requirement produces generics). The purpose of using generics is to have the following two main aspects:
    1. Attempts to convert runtime exceptions to compile-time errors, reducing the number of runtime exceptions (which improves the compiler's ability).
    2. Solve the problem of template programming.
// No generics   New ArrayList ();  List0.add ("Hello");   = (String) list0.get (0);   // with generics, so that you don't use type conversions  when you get objects New Arraylist<string>();  List.add ("Hello");   = List.get (0);  

    1. No generics
    2. List list0 = new ArrayList ();
    3. List0.add ("Hello");
    4. String str0 = (string) list0.get (0);
    5. With generics, so that you don't use type conversions when you get objects
    6. list<string> list = new arraylist<string> ();
    7. List.add ("Hello");
    8. String str = list.get (0);
Attention: generics are not arrays. compiler does not recognize hashmap<k,v>[]This form: Cannot create a generic array of hashmap<k,v>
The reason is the array must determine the type of each of his elements。 However, generics have a type erasure problem in the compilation phase, meaning that the compiler understands that both hashmap<integer,integer> and hashmap<string,string> are the same type HashMap. If a generic array exists, then both hashmap<integer,integer> and hashmap<string,string> objects can be added to the same hashmap[] array, which can result in catastrophic results.

Detailed reasons can see this blog http://hxraid.iteye.com/blog/549509

If you must define a HASHMAP array, you can use hashmap[] atweightlist = new HASHMAP[10]; form, i.e. HashMap the general type.

Java Data types

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.