Basic data types for Java

Source: Internet
Author: User

The following is referenced from http://wiki.jikexueyuan.com/project/java/basic-datatypes.html:

Variables are memory locations that are reserved for storing values. This means that when a variable is created, it takes up a certain amount of space in memory.

Based on the variable data type, the operating system allocates memory and decides what will be stored in the reserved memory. Therefore, 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

First, the original data type

Java supports 8 types of raw data. The original data type is pre-defined by the language and named with the keyword.

1, 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 intended to save space in large arrays, since byte types are 4 times times smaller than integers, so they are mostly substituted integers.
    • Example: byte a = +, byte B =-50

2, 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

3, 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

4, Long integer type (long)

    • 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

5. 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

6. Double type

    • 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

7. 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

8, 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 '

II. Reference data types

    • The reference data type is defined by the editor of the class. is 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");

Third, Java constants

Constants are source code that represents a fixed value.

Constants can be assigned to arbitrary primitive variable types. For example:

byte a =; char a = ' a '

Byte, Integer, long, and short integers can also be represented by decimal, hexadecimal, and octal count systems.

When using these technical systems to represent the direct volume, the prefix 0 is to indicate octal, and the prefix 0x is to indicate hexadecimal. For example:

int decimal = +; int octal = 0144; int hexa = 0x64;

The rules for string constants in Java, like most other languages, are written in the middle of a double quotation mark. Examples of string direct quantities are as follows:

"Hello World" "Two\nlines" "\" This is in Quotes\ ""

Character and string constants can contain arbitrary Unicode letters. For example:

char a = ' \u0001 '= "\u0001";

The Java language also supports some special escape sequences for character and string direct quantities. They are:

Escape Character meaning
\ n Line Break (0x0a)
\ r Enter (0x0d)
\f Page Change (0x0c)
\b BACKSPACE (0x08)
\s Space (0x20)
\ t tab
\" Double quotes
\‘ Single quotation marks
\ Back slash
\ddd octal character (DDD)
\uxxxx Hexadecimal UNICODE character (xxxx)

Basic data types for Java

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.