Java Basic data types

Source: Internet
Author: User

Based on the variable data type, the operating system allocates memory and decides what will be stored in the reserved memory. So, by assigning different data types to variables, you can store integers, decimals, or letters in these variables.

There are two valid types of data in Java:

    • Raw data type
    • Reference data type
Raw data type

Java supports 8 types of raw data. The original data type is pre-defined by the language and named with the keyword. Let's take a closer look at these 8 types of data.

byte type (byte)
    • The byte type is a 8-bit positive binary integer
    • Minimum value is-128 ( -2^7)
    • Maximum value is 127 (2^7-1)
    • The default value is 0
    • The byte type data type is primarily designed to save space in large arrays, mainly replacing integers because byte types are 4 times times smaller than integers.
    • Example: byte a = +, byte B =-50
    • Packing type: java.lang.Byte
Short integer
    • A short integer is a 16-bit positive binary integer
    • Minimum value is-32768 ( -2^15)
    • Maximum value is 32767 (2^15-1)
    • Data of a short integer type can also be used to save space as a byte type. Short integers twice times smaller than integers
    • The default value is 0
    • Example: short S = 10000, short r =-20000
    • Packing type: Java.lang.Short
Integer type (int)
    • Integer type is a 32-bit positive binary integer
    • Minimum value is-2,147,483,648 ( -2^31)
    • Maximum value is 2,147,483,647 (2^31-1)
    • Integer types are typically applied to integer values by default unless you are concerned about insufficient memory.
    • The default value is 0
    • For example: int a = 100000, int b =-200000
    • Packing type: Java.lang.Integer
Long integer type
    • A long integer is a 64-bit positive binary integer
    • Minimum value is-9,223,372,036,854,775,808 ( -2^63)
    • Maximum value is 9,223,372,036,854,775,807 (2^63-1)
    • This data type is typically applied when it needs to be larger than the integer range.
    • Default value is 0L
    • For example: Long a = 100000L, int b = -200000l
    • Packing type: Java.lang.Long
Float type (float)
    • Floating-point data is a single-precision floating-point data under the 32-bit IEEE 754 standard.
    • Floating-point data is primarily intended to conserve memory in large floating-point digital arrays.
    • The default value is 0.0f.
    • Floating-point data cannot be used for precise data such as currencies.
    • Example: float f1 = 234.5f
    • Packing type: java.lang.Float
Dual Type (double)
    • Double-precision data is a double-precision floating-point data under the 64-bit IEEE 754 standard.
    • This data type is primarily used by default to represent decimal values, and is generally the default choice.
    • Double data cannot be used for precise data such as currencies.
    • The default value is 0.0d
    • Example: double D1 = 123.4
    • Packing type: java.lang.Double
Boolean Type (Boolean)
    • Boolean data represents a bit of information.
    • It has only two possible values: True (True) and False (false)
    • This data type is used for simple tokens under true and false conditions.
    • Default value is False (false)
    • For example: boolean one = True
    • Packing type: Java.lang.Boolean
Character type (char)
    • Character data is a simple character under the 16-bit Unicode standard.
    • The minimum value is: ' \u0000 ' (or 0).
    • The maximum value is: ' \uffff ' (or 65,535).
    • Character data can be used to store any letter.
    • For example: Char letter A (character type a) = ' a '
    • Packing type: java.lang.Character
Reference data type
    • The reference data type is defined by the editor of the class. They are used to access the object. These variables are defined as a specific type that cannot be changed. For example: Employee, Puppy and so on.
    • class objects and array variables are the reference data types.
    • The default value for any reference data type is empty.
    • A reference data type can be used for any declared type and object of a compatible type.
    • For example: Animal Animal = new Animal ("giraffe");


In fact, there is another primitive type Void in Java, which also has a corresponding wrapper class java.lang.Void, but we cannot manipulate them directly.

The base types are stored in the stack, so they are accessed faster than the instance object of the corresponding wrapper class stored in the heap. Starting with Java5.0 (1.5), the Java Virtual machine (Java VM) can perform the basic types and the automatic conversion between their corresponding wrapper classes. So we use their wrapper classes in terms of assignment, parameter passing, and math, like using basic types, but that doesn't mean that you can call their wrapper class with a basic type to have a method. In addition, the wrapper classes for all basic types (including void) use final adornments, so we cannot inherit them to extend new classes or to override any of their methods. Reference


References:

http://wiki.jikexueyuan.com/project/java/basic-datatypes.html

http://www.cnblogs.com/kelin1314/archive/2010/09/11/1824145.html

Java Basic data types

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.