Type conversion in Java

Source: Internet
Author: User
Tags float double


This work is licensed using the "knowledge sharing signature"-"non-commercial use"-2.5 mainland China License Agreement in the same way.

 

 

Whether in C/C ++ or Java, forced type conversion is no stranger. However, to fully master the key points of type conversion in Java, this article will take you to understand all the key points of type conversion in Java.

Data Type Conversion

Java has two types of data: Basic Data Type and reference data type. Java has two basic types of conversion principles: widening conversion) and narrowing conversion ).
Widening conversion and downgrading conversion are only applicable to numeric types in basic data types. Data value types are listed in the descending order of the range:

 

Byte short int long float double

 

When the range of the target type is larger than the range of the source type, the conversion principle is extended without force conversion. For example:

Byte B = 10; <br/> short S = B;

 

Otherwise, follow the principle of narrowing down the conversion, that is
When the range of the target type is smaller than the range of the source type, the conversion must be forced. For example:

Short S = 10; <br/> byte B = (byte) S;

 

It should be noted that the principle of widening conversion does not lose data. For example:

Long L1 = long. max_value; <br/> float F = L1; <br/> long L2 = (long) F; <br/> system. out. println ("before conversion:" + L1); <br/> system. out. println ("converted:" + l2 );

 

The output result of the preceding example is:

Before switch: 9223372036854775807
After the conversion: 9223372036854775807

Narrowing down the conversion principle will cause data loss when the value of the target type is exceeded. For example:

Int I1 = 257; <br/> byte B = (byte) i1; <br/> int I2 = B; <br/> system. out. println ("before conversion:" + I1); <br/> system. out. println ("converted:" + I2 );

 

The output result of the preceding example is:

Before switch: 257
After conversion: 1

Conversion between character type and numeric type

The basic data type char is equivalent to an unsigned short integer, ranging from 0x0000 to 0 xFFFF.

When converting from other numeric types to Char Types, you must perform forced conversion. For example:

Byte B = 10; <br/> short S = 10; <br/> int I = 10; <br/> long l = 10l; <br/> float F = 10.0f; <br/> double D = 10.0; </P> <p> char c = (char) B; <br/> C = (char) s; <br/> C = (char) I; <br/> C = (char) L; <br/> C = (char) F; <br/> C = (char) D;

 

If the char type is converted to another numeric type, the int long float double type does not have to be converted unless the byte and short values must be forcibly converted. For example:

Char c = 'a'; <br/> byte B = (byte) C; <br/> short S = (byte) C; <br/> int I = C; <br/> long l = C; <br/> float F = C; <br/> double D = C;

 

Conversion of reference types

The assignment of reference types is common in the conversion of two types with inheritance relationships. Example:

Public class person {<br/> Public void sleep () {<br/>}< br/> public class student extends person {<br/> Public void Study () {<br/>}< br/> public class teacher extends person {<br/> Public void teach () {<br/>}< br/>}

 

If a parent-child relationship exists between the target type and the source type, you do not need to convert it forcibly. For example:

Person = new student (); <br/> person. Sleep ();

 

The preceding example is a typical polymorphism. However, if you want to convert from a parent class reference to a subclass reference, that is
If the source type and target type have a parent-child relationship, the conversion must be performed forcibly. For example:

Person = new student (); <br/> person. sleep (); <br/> If (person instanceof student) {<br/> Student = (student) person; <br/> student. study (); <br/>}

 

Of course, if the subclass references a parent class object, a classcastexception exception will be thrown during conversion. For example:

Person = new person (); <br/> person. sleep (); <br/> Student = (student) person; // exception thrown during runtime <br/> student. study ();

 

Further, if you want to convert from one reference type to another unrelated reference type, that is
If there is no parent-child relationship between the source type and the target type, the conversion fails and an error occurs during compilation. For example:

Teacher Person = new teacher (); <br/> person. sleep (); <br/> Student = (student) person; // report errors during compilation <br/> student. study ();

 

Conversion between numeric type and packaging class

After jdk1.5, the concept of automatic packing was introduced. That is to say
When converting a basic data type to a corresponding reference type, you do not need to convert it forcibly. For example:

Int I = 10; <br/> integer iobj = I;

 

And vice versa. That is
You do not need to forcibly convert a reference type of a basic data type to a basic data type. For example:

Integer iobj = new INTEGER (10); <br/> int I = iobj;

 

Type escalation principle in expressions

In arithmetic and bitwise operations, data of the numeric type is upgraded according to the following principles:

Byte, short, and char types are automatically converted to int type. For example:

Byte b1 = 10; <br/> byte b2 = 20; <br/> int value = b1 + B2;

 

If one of the operands is of the long type, the result of the entire expression is represented by the long type. For example:

Int I = 10; <br/> long l = 20; <br/> long value = I + L;

 

Bitwise operations cannot operate the float and double types. Therefore, in arithmetic operations
If one of the operands is of the float type, the results of the entire expression are represented by the float type. For example:

Long L = 10; <br/> float F = 20366f; <br/> float value = L + F;

 

If one of the operands is 'double', the result of the entire expression is expressed as 'double. For example:

Float F = 10.0f; <br/> double D = 20.0; <br/> double value = F + D;

 

Special Type upgrade

Java expressions have a special expression,? Expression.? An expression is a special value assignment expression that promotes the operands at both ends to a uniform type based on the Type escalation principle in arithmetic operations. For example:

Byte B = 10; <br/> short S = 20; <br/> int I = (B = 10 )? B: S;

 

It should be noted that
If one of the two operands is of the reference type, the results of the entire expression are represented in the object type.

Int I = 10; <br/> string STR = "hello"; <br/> Object OBJ = (I = 10 )? I: STR; // here the variable I performs the automatic packing operation

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.