Java Basics Self-study note finishing (ii) posture of certain data types

Source: Internet
Author: User

Ah hahaha haha ~ just hit the Three Kingdoms killed, good cool, so have a look at the video.

There are two types of conversions: automatic and hard-to-anus (mandatory).

1. Automatic type conversion

A data type with a small capacity can automatically be converted to a large data type.

e.g int i = 3;

Double d = i;

float F = i;

Double d2 = f; You don't have to be a hard-on-the-anus pose.


You can directly assign an int constant to a type variable such as Byte,short,char without forcing the type to go beyond the range of its number of tables . 650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7E/D3/wKioL1cJ5YfDkIANAADMZUMJ_FE295.png "style=" float: right; "title=" solid line can be converted directly, dashed lines can not "alt=" Wkiol1cj5yfdkianaadmzumj_fe295.png "/>

e.g Byte ( -128~~127)

char (0~~65535)


int i = 200;

byte C = i;

WRONG!!!!!

int i = 126;

byte C = i;

to!!!!!


byte B = 123;

byte B2 = 300;// WRONG!!!!!

char c = -3;// error!!!!! Negative! Super range.


char c2 = ' a ';

int i = c2;//(direct)

Long do1 = 123213;//(direct)

float F = do1;//may lose precision


Capacity size is the size of the range, regardless of the byte


2. Hard-to-type conversion, so ashamed of the posture point, I still in the example described in.

e.g int i2 = 100;

char C3 = (char) i2;

System.out.println (C3);

Output is character D


However, when more than one type of representation is cast, it is transformed into a completely different meaningless value.

e.g int i2 =-100;

char C3 = (char) i2;

System.out.println (C3);

Output is not recognized by characters


e.g//type promotion issues in expressions

int a = 3;

Long B = 4;

int c = (int) (a+b);//Do all the two-dollar operators (+-*/%) have type-raising problems such as if you write int c = a+b; Loss of precision.

float f = (float) (a+b);


Common errors:

int money = 1 billion;//too much 0 labor only writes Chinese.

int years = 20;

int all = money * years;

System.out.println (all); //negative, because out of range


In the face of this situation, it is necessary to use type lifting to fill the pot.

For example: long all = (long) money * years;

Note that only one int money integer is promoted here, if (long) (Money*years)

Also negative because the result of multiplying by two int is already over-range.



So!!! When a multi-digit operation is encountered when the result overflows, only one of the elements is raised immediately.

e.g long time = 70L * * * 365 *;

System.out.println (time);


Yes, I'm so tired, I'm going to play again.

This article is from the "11433657" blog, please be sure to keep this source http://11443657.blog.51cto.com/11433657/1762298

Java Basics Self-study note finishing (ii) posture of certain data types

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.