Data types and usage of Java

Source: Internet
Author: User

I. Basic data types

    1. Classification of basic data types
    2. Range of values for the base data type

data type

value range

byte       

8

-128~127

short      

16

-32768~32767

int          

32

-2 Span style= "Text-decoration:underline" >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

    1. Type conversions for basic data types

In a program, you often encounter situations where you want to convert a data type (for example, when there are different types of operands in an expression)

The data transformation is divided into two modes:

1. Automatic type conversion

An automatic conversion is also called an implicit conversion, and an automatic type conversion occurs as long as the following conditions are met

A) two types of compatibility with each other

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

char ch = ' b ' ;

int a = ch;

float f = a ;

Double d = f;

Type conversions occur automatically, without explicit instructions

In the case of type promotion, the compiler will automatically convert the type, otherwise coercion type conversion is necessary; when casting, it is necessary to be clear whether the result of the cast is what you need

Type promotion should follow these rules:

Both types must be compatible, and the target type is greater than the source type, for example:

Byte, char, and short types can all be promoted to long.

For an expression, the final result will be the same as the highest data type in the expression.

2. coercion type conversion (before casting, it is necessary to know whether the result after the cast is the one you want is the available result)

Also called an explicit type conversion

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

Such as:

int x = - ;

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 = - ;

Char CH = (char) x;

Data types fall into two categories: basic types and reference types

A, basic data type: 8 class {Int,char,double,float,long,shortbyte , boolean }

B, reference type:

Reference type except for the base data type

include Classes in the API, such as String, File

also includes custom classes, such as Schoolname,classroom

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

The difference between a data type and a reference type:

& nbsp;

basic data type  

reference data type  

concept  

variable (pure value)  

 

storage location  

save value in stack  

save references in stacks, save property values for specific objects in the heap  

How to assign a value  

Direct Assignment  

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

Iv. Reference data types

Definition of reference data type: Except for variables of the underlying data type above, all of the rest belong to the reference data type

Classes in the API, such as String, file, and so on

Array []

custom classes, such as Person,room

Objects are all reference data types

Objects are reference data types

reference types are assigned using the new call construction method: Assignment of reference data types

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

The assignment of a reference type is to call the constructor by new, construct two variables and assign the initial value, the person is a String type

Person p = new person ("red");

person P1 = new Person ("blue");

Age A = New Age (1);//age is an int type.

Exceptions: The string class can use the = assignment directly, without using the new

Data types and usage of 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.