Java Fundamentals-Implicit conversions vs casts

Source: Internet
Author: User

There are many issues to be aware of when defining variables, such as loss accuracy or incompatible types.

For example:

1. When defining long-integer data, you must add a suffix L or L

Long L =123456789012345l

2. When defining a single-precision type ( 7-8 digits), you must add a suffix F or F

float F = 12.5F

3. The Boolean type cannot be converted to another data type.

In this, we often encounter data type conversion problem, the most common is implicit conversion and casting, let us analyze.

Implicit conversions

Characteristics:

From small to large, can be implicitly converted, the data type will be automatically promoted.

byte , Short , Char -->int-->long-->float-->double

Note: Long is a 8 bytes, float is a 4 a byte.

Long is an integer, float is floating point, the storage rules for integers and floating-point numbers are different, remember a little Long the range is less than float the.

Example :

byte a= Ten;

int B=a ;

When compiling intb=a, a implicitly converts to an int type.

Forced conversions

features :

From large to small (if you know for sure that data can be represented by that data type, you can use casts)

format:(the converted data type) variable or value.

Note: Forced type conversions are not generally recommended at all.

Example 1:

int a=10;

byte b= (byte) A;

When compiling byte b= (byte) a when the a is cast to byte type.

Example 2 :

Class Qiangzhidemo{public static void Main (string[] args) {byte b= (byte) 130; System.out.println (b);//Print result-126}}


Analytical:

Data the the default is int type of decimal data ,

The first step : decimal the converted into binary data.

10000010

Step Two: the The representation in memory is as follows

Original code: 00000000 00000000 00000000 10000010

Step three : Ask int130 the complement

because the is positive, so the anti-code and the complement are consistent with the original code.

Complement: 00000000 00000000 00000000 10000010

Fourth step: to intercept the complement, leaving only the last 8 bit.

(Byte) the complement is: 10000010

Fifth step: Convert the complement to the original code.

because the sign bit (first bit) is 1 , so the number is negative,

Anti-code: 10000001 ( complement -1)

Original code: 11111110 ( symbol bit invariant, data bit reversed )

Convert to decimal -126 , so the final print -126 .

Example 3 :

Shorts = 1;

s= s +1;

And

Shorts = 1;

S+=1;

Is there a problem?

parsing :

The first program will error: Errors: Incompatible types: conversion from int to short can be a loss

Cause: S=s+1;s+1 is implicitly converted to an int type, which can be lost when assigning an int type to the short type.

A second program can be compiled and run.

Cause: s+=1, although can be regarded as s=s+1, but there is a difference, s+=1 there is a cast, that s = (short) (s+1) , The value of s+1 will be cast to the short type, so no error will be made.

Summary:

Problems with data type conversion if it happens in Some small programs, we may be able to see it at a glance, but when you write a huge system with huge amounts of data, these small problems can cause system errors or even crashes , so the rigor of the early code writing depends on our own grasp.




Java Fundamentals-Implicit conversions vs casts

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.