Java Fundamentals-Basic data type conversion cases

Source: Internet
Author: User

Java Basic Data Type VIII

byte = byte short = shortchar = Characterint =  IntegerLong = longfloat = floatDouble = double Boolean = Boolean

This article connects the sequence through a few simple transition relationships

Run an instance to view the results of the output

        byteb = ' a ';  Shorts = ' a '; Charc = ' a '; inti = 5; LongL = 5l; floatf = 5.5f; DoubleD = 5.5; BooleanBo =true;        System.out.println (b);        System.out.println (s);        System.out.println (c);        System.out.println (i);        System.out.println (l);        System.out.println (f);        System.out.println (d); System.out.println (bo);

Output

9797a555.55.5true

You can see that byte and short convert the characters to the AC code output. About char essentially char type is a subset of int

The following is a comparison transformation to understand the size problem

First try to convert to another type using byte

You can see that byte cannot be converted to another type, and you can see that byte is the smallest storage unit

Keep looking at short.

From which you can see that short can receive byte for direct conversion

Int

Long

Float

Double

Boolean (itself True/false only two values, so no other type can be converted)

Char (Char's particularity makes char unable to use other conversions)

One can see the difference

1. In addition to the other types of char can be converted from small to large, the view period source code is inherited, implemented the following ways

?  extendsimplements

Have inherited the number class. Which is the digital type.

2.char different from other data types The reason is also here

 Public Final class implements java.io.Serializable, comparable<character>

Using the Unicode character set to represent characters, Unicode is a fully internationalized character set that can represent characters in all human languages. Unicode requires a 16-bit width, so the char type in Java is also represented by a-bit. The assignment might be something like this:

char ch1 =N; char CH2 = ' A ';

The ASCII character set occupies the first 127 values of Unicode. Char is attributed to Integer because Java provides arithmetic operations support for Char, such as ch2++; then CH2 becomes Y. When Char is subtraction, it is also converted to an int type, which must be explicitly converted back.

Java Fundamentals-Basic data type conversion cases

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.