Java basic 02 data type conversions

Source: Internet
Author: User

First, answer the question
float f1 = 6.66f;
float F2 = (float) 6.67;

F1==f2?

/** *  * @author Sun * */public class Demo {public static void main (string[] args) {Float F1 = 6.67f;float F2 = (float) 6.66; System.out.println (F1-F2); System.out.println (F1==F2);}}

Output:

0.010000229
False

Why does this happen? Where's f1!=f2? At this point we need to know that in Java,

integers are type int by default, and floating-point numbers are double by default.
Long integers to be added L or L.
Single-precision floating-point numbers are added F or f.

Default conversion
A: From small to large
B:byte,short,char--INT--long--float--double

The latter 6.66 is a double type, after the cast value to F2, the precision will be lost, so the above situation occurs.

Second, the following procedure is a problem, if so, where?
BYTE B1 = 3;
byte b2 = 4;
BYTE B3 = b1 + b2;
byte B4 = 3 + 4;

B1+b2java is automatically converted to int type in the result of the computed value is the int type of data, cannot automatically turn to byte, so error, need to force conversion to byte.
Third, what is the result of the following operation?
byte B = (byte) 130;

/** *  * @author Sun * */public class Demo {public static void main (string[] args) {//Because the range of byte is:-128 to 127. ( -2^7--2^7-1)//We need to convert byte B = (byte) 130 using coercion type; System.out.println (b);}}

Output:

-126

Analysis Process: If we want to know what the result is, we should know how to calculate it. And we know that the computation of the data in the computer is in the complement. To get the complement, the first thing to do is to calculate the binary of the data.
A: Get 130 binary for this data. 00000000 00000000 00000000 10000010 This is the original code of 130, is also anti-code, or complement.
B: Intercept operation, cut into byte type. 10000010 The result is a complement. C: Known complement to seek the original code. Sign bit number complement: 1 0000010
Anti-code: 1 0000001
Original code: 1 1111110

Iv. String Participation Operations

/** *  * @author Sun * */public class Demo {public static void main (string[] args) {System.out.println ("Hello" + ' a ' + 1); S Ystem.out.println (' A ' +1+ "Hello"); System.out.println ("5+5=" +5+5); System.out.println (5+5+ "=5+5");}}

Output:

Helloa1
98hello
5+5=55
10=5+5

Concatenation of strings

 

Java basic 02 data type conversions

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.