Introduction to basic data types

Source: Internet
Author: User

Java Basic data types

A variable is a request for memory to store a value. In other words, when creating a variable, you need to request space in memory. The memory management system allocates storage space for variables based on the type of the variable, and the allocated space can only be used to store that type of data. Therefore, you can store integers, decimals, or characters in memory by defining variables of different types. Two big data types for Java, built-in data types, and reference data types.

1. Built-in data type: The Java language provides eight basic types. Six types of numbers (four integers, two floating-point types), one character type, and one Boolean type.

BYTE (this data type is not commonly used in Java):

The byte data type is a 8-bit, signed, integer in binary complement notation;

The default value is 0;

The byte type is used to save space in large arrays, mainly in place of integers, because the byte variable occupies only one-fourth of the int type;

Example: Byte a = 100,byte B =-50.

Short (this data type is not commonly used in Java):

The short data type is a 16-bit, signed integer with a binary complement representation

The short data type can also save space as Byte. A short variable is one-second of the space occupied by the int variable;

The default value is 0;

Example: Short s = 1000,short r =-20000.

Int:

The int data type is a 32-bit, signed integer with a binary complement representation;

The generic integer variable defaults to the int type;

The default value is 0;

Example: int a = 100000, int b =-200000.

Long

The Long data type is a 64-bit, signed integer with a binary complement representation;

This type is mainly used on systems that need to compare large integers;

The default value is 0;

Example: Long a = 100000l,long B = -200000l.

Float

The float data type is a single-precision, 32-bit, IEEE 754 compliant floating-point number;

Float saves memory space when storing large floating-point groups;

The default value is 0.0f;

Floating-point numbers cannot be used to denote precise values, such as currency;

Example: float f1 = 234.5f.

Double

The double data type is a dual-precision, 64-bit, IEEE 754-compliant floating-point number;

The default type of floating-point number is double type;

A double type cannot also represent an exact value, such as a currency;

The default value is 0.0d;

Example: Double D1 = 123.4.

Boolean

The Boolean data type represents one bit of information;

Only two values: true and false;

This type is only used as a sign to record the true/false situation;

The default value is false;

Example: Boolean one = True.

Char

The char type is a single 16-bit Unicode character;

Char data type can store any character;

Example: char letter = ' A '.

String:

String is a more special type of string;

Example: String a = ' hello '.

Steps to use a variable

First step: Declare variables, both "In memory request space based on data type"

Data type variable name; int money

Step Two: Assign values to "store data in the corresponding memory space"

Variable name = value; money=1000;

The first and third steps merge

Data type variable name = value; int money=1000;

Step three: Use variables to both remove data using

Variable name Specification

1. The first letter of the variable name must be a letter, underscore, dollar sign, the rest of the numbers, letters, underscores, dollar signs

2. Short can clearly indicate the role of the variable, usually the first word of the initial capital letter

3. Variables are declared and assigned before they can be used

4. Declaring two variables of the same name cannot be duplicated

Scope of the variable

From defining the variable place to the end of the current "}", the variable will be recycled if it is out of scope

VI: Basic data type conversions

1. Automatic type conversion (implicit type conversion): From small to large types can be automatically completed

2. Coercion of type conversions: large-to-small types require a cast character, resulting in loss of precision or overflow

Byte-->short-->int-->long-->float-->double

From left to right small type to large type, can be automatic type conversion

To the left large type to small type, you need to force type conversion

The minimum and maximum values for float and double are output in the form of scientific notation, and the trailing "e+ number" indicates how many times the number before E is multiplied by 10. For example, 3.14E3 is 3.14x1000=3140,3.14e-3 is 3.14/1000=0.00314. 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.

2. Reference data Types

Reference type

Reference type variables are created by the constructors of the classes and can be used to access the referenced objects. These variables are specified at the time of declaration as a specific type, such as employee, Pubby, and so on. Once a variable is declared, the type cannot be changed.

objects, arrays are reference data types.

The default value for all reference types is null.

A reference variable can be used to refer to any type that is compatible with it.

Example: Animal Animal = new Animal ("Giraffe").

Java Constants

A constant is a fixed value. They do not need to be computed and represent the corresponding values directly.

Constant refers to the amount that cannot be changed. In Java, the final flag is declared in the same way as a variable:

Final double PI = 3.1415927;

Although constant names can also be lowercase, uppercase letters are usually used to denote constants for easy identification.

Literals can be assigned to variables of any built-in type. For example:

byte a = 68;

char a = ' a '

byte, int, long, and short can all be represented in decimal, 16-binary, and 8-binary ways.

When a constant is used, the prefix 0 represents 8, and the prefix 0x represents 16 binary. For example:

int decimal = 100;

int octal = 0144;

int hexa = 0x64;

Like other languages, a string constant in Java is also a sequence of characters that is contained between two quotation marks. The following is an example of a string literal:

"Hello World"

"Two\nlines"

"" This was in quotes ""

Both string constants and character constants can contain any Unicode characters. For example:

char a = ' \u0001 ';

String a = "\u0001";

The Java language supports some special sequences of escape characters.

Symbolic character meaning

\ n line break (0x0a)

\ r Enter (0x0d)

\f page Break (0x0c)

\b Backspace (0x08)

\s Space (0x20)

\ t tab

"Double quotation marks

' Single quotation mark

\ back Slash

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