Java Foundation implicit conversion vs cast _java

Source: Internet
Author: User

In Java, you can often encounter types of conversion scenarios, from the definition of variables to replication, the calculation of numerical variables to the method of parameter transfer, base classes and derived classes between the shape, and so on, everywhere type conversion figure. Type conversions in Java play an important role in Java coding.

When defining variables, there are a number of issues to be aware of, inadvertently will appear loss accuracy or incompatible types and so on.

For example:

1. When defining long integer data, you must add the suffix L or l

Long L =123456789012345l

2. When defining a single precision type (7-8-digit valid number), you must add a suffix f or f

float F = 12.5F

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

Among these, we often encounter the conversion of data types, the most common is implicit conversion and forced conversion, let's analyze.

Implicit conversions

Characteristics:

From small to large, you can implicitly convert the data type will automatically ascend.

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

Note: Long is 8 bytes and float is 4 bytes.

A long is an integer, float is a floating-point type, and an integer is not stored with a floating-point number, and remember that a long range is less than float.

Cases:

BYTE a=10;
int b=a;

When Intb=a is compiled, a is implicitly converted to type int.

Forced conversions

Characteristics:

From big to small (if you know explicitly that data can be represented by that data type, you can use a cast)

Format:

(converted data type) variable or value.

Note: In general, it is not recommended to use coercion type conversions at all.

Example 1:

int a=10;
byte b= (byte) A;

When a byte b= (byte) is compiled, A is cast to the 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 130 defaults to the decimal data of type int,

The first step: decimal 130 is converted to binary data.

10000010

Step two: 130 the representation in memory is as follows

Original code: 0000000000000000 00000000 10000010

The third step: to find the complement of int130

Since 130 is a positive number, the inverse code and complement are consistent with the original code.

Complement: 0000000000000000 00000000 10000010

The fourth step: the complement to intercept, only the last 8 digits left.

The complement of (byte) 130 is: 10000010

The fifth step: Converts the complement to the original code.

Because the sign bit (first digit) is 1, the number is negative,

Anti-code: 10000001 (Complement-1)

Original code: 11111110 (symbol bit unchanged, data bit counter)

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

Example 3:

Shorts = 1;
s= s +1;   

And

Shorts = 1;
S+=1;   

Do you have a problem?

Analytical:

The first program will complain: Error: Incompatible type: conversion from int to short can be a loss

Reason: S=s+1;s+1 is implicitly converted to type int, and when an int type is assigned to the short type, it may be lost.

The second program can be compiled and run.

Reason: S+=1, although can be regarded as s=s+1, but there is a difference, S+=1 has a cast, that is, s= (short) (s+1), will be forced to convert the value of s+1 to the short type, it does not error.

Summary:

Problems with data type conversions if they occur on some small program, we may be able to see at a glance, but when writing a large system, with a large amount of data, these small problems may lead to system errors or even crashes, so the rigor of the early code writing depends on our own grasp.

The above describes the Java foundation of the implicit conversion vs forced conversion, I hope you like.

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.