Int, byte, and byte for java data type conversion

Source: Internet
Author: User
Tags decimal to binary

Int, byte, and byte for java data type conversion

Take a look at the following operations:
Public class TestConvert {public static void main (String [] args) {byte b1 = 67; byte b2 = 89; byte B = (byte) (b1 + b2 ); // if the System is forcibly converted to an int type operation, the System must be forcibly converted. out. println (B );}}
Analysis: the byte type of storage space is 1 byte, range:-128 ~ 127; the storage space of the int type is 4 bytes;

Byte b1 = 67 = 01000011; // The symbol bit is positive

Byte b2 = 89 = 01011001; // The symbol bit is positive

01000011

+ 01011001

----------------------------------

10011100 (if the value exceeds the upper limit, the sign bit is 1, indicating a negative number)

Return 10011100 to 01100011 and Add 1-> 01100100

Namely-(64 + 32 + 4) =-100

This mainly involves the conversion from decimal to binary, complement, and anti-code!

This is my personal idea. If you have any mistakes, please be honest!


In Java, when int type is strongly converted to byte type and strongly converted to byte type, how is the calculation of data beyond the byte expression range?

The minimum value of byte is-128, and the maximum value is 127. It seems that the capacity of a cup of water is limited. When the water in your cup is full, it will naturally overflow, 127 is like the top layer of water in the cup. if you add a drop, it will overflow and flow to the bottom of the cup. The bottom of the cup is-128. According to this logic, your I + r = 156, that is, there are 28 streams to the bottom, and the bottom is-128, which is overwritten by 28, so in the end, it would be-100. Do you understand?

Java variable data type conversion? Byte, short, char, int, long, float, double

Conversion between simple data types
In Java, integer, real, and struct types are considered as simple data types. These types are (byte, short, char) -- int -- long -- float -- double

Automatic type conversion
Low-level variables can be directly converted to advanced variables, which I call automatic type conversion. For example, the following statement can be directly passed in Java:
Byte B;
Int I = B;
Long l = B;
Float f = B;
Double d = B;
If the low-level type is char type, the conversion to the advanced type (integer) is converted to the corresponding ASCII code value, such as r
Char c = 'C ';
Int I = c;
System. out. println ("output:" I );
Output: 99;
For the byte, short, and char Types, they are of the same level, so they cannot be automatically converted to each other. The following mandatory type conversion can be used.
Short I = 99; char c = (char) I; System. out. println ("output:" c );
Public class TestBasicTypeConvert {
Static void prt (String s ){
System. out. println (s );
}
Void f1 (long x) {prt ("f1 (long )");}
Void f1 (float x) {prt ("f1 (float )");}
Void f1 (double x) {prt ("f1 (double )");}
Public static void main (String [] args ){
TestBasicTypeConvert tbConvert = new TestBasicTypeConvert ();
TbConvert. f1 (1 );
}

}
// F1 (long)

High to low, strong to turn

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.