Data types in Java

Source: Internet
Author: User

data types in Java

I. BASIC data Types

1. Basic data types

That is, int,char,float,double,byte, Short , Long , Boolean There are 8 types of such numeric values .

Data type

Number of digits

Range of values

Description

Byte

8

-128~127

The most primitive form of data stored in memory

Short

16

-32768~32767

Int

32

-2^31~2^31-1

Long

64

-2^63~2^63-1

Float

32

Slightly

Double

64

Slightly

Char

16

'\u0000'~'\uffff'

Boolean

1

True/false

2. Conversion of basic data types

(1) automatic type conversion is also called implicit type conversion;

When you assign a variable of one type to a variable of another type, an automatic type conversion occurs whenever the following conditions are true:

Mutual compatibility between the two types;

The target type is greater than the source type (that is, type promotion).

Such as:

char ch = ' A ';

int num = ch;

float f = num;

Double d = f;

type conversions occur automatically, without explicit instructions

(2) cast type

Coercion type conversions are also known as explicit type conversions;

If the data type of the converted value is greater than its target type, some information will be lost;

Such as:

int x = 65;

char ch = x;

Such statements will cause an error because the char type is less accurate than the int type, and the compiler cannot convert it automatically, only cast:

int x = 65;

char ch = (char) x;

Ii. type of reference

1. reference type data definition

Reference type except for the base data type

include classes in the API, such as String,File

Also includes custom classes, such as PersonalAccount,creditaccount

include arrays , such as int[],string[].

2. Scenarios used by reference types

Used as a type of attribute;

Used as the return value type of the method;

Used as the form parameter type of the method;

3. assignment of reference data types

Reference types are objects, so assignments are assigned using the new call construction method.

Exception: The String class can use the = assignment directly, without using new.

Three, the difference between the basic type and the reference type

Basic data types

Reference data type

Concept

Variable (simple value)

Object (Reference)

Storage location

To store values in the stack

Save references in stacks, store property values for specific objects in the heap

How to assign a value

Direct Assignment

New ,String class and wrapper class can be directly assigned values

Data types in 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.