JAVA SE Basics Review-Basic program design (1)

Source: Internet
Author: User
Tags java se

1. Java Basic Data type

int 4 max 0x7fffffff 2147483647=2 31 square-1 first bit symbol bit minimum -2147483648 0x80000000 complement store first constant other bit reverse plus 1

Short 2 max 2^15-1 32767 min -2^15-32768

Long 8 maximum 2^63-1 minimum value -2^63

BYTE 1 max 2^7-1 127 min -2^7-128

Float 4 byte format saved in memory seee eeee emmm MMMM MMMM MMMM MMMM MMMM

S represents the sign bit, 1 is negative, 0 is positive
The power of E offset 127, binary order = (EEEEEEEE)-127.
The mantissa of the M 24 bits is stored in 23 bits, with only 23 bits, and the highest bit fixed at 1. This method is implemented with the least number of bits
High number of significant digits, improving the accuracy.

Double 8, similar to above, can be more precise in a larger number of ranges, such as double.max_value=1.7976931348623157e308 1.7976931348623157*10^308

Char 2

Boolean 2

Note: float and double can only be used for scientific calculations or engineering calculations; we use java.math.BigDecimal in business calculations.

2. Java Type Conversion

1) Basic Data type conversion

The Java language is a strongly typed language. There are several requirements for strongly typed languages:

-a variable or constant must have a type: The type must be declared when a variable or constant is declared, and can only be used after a declaration.

-the type must be identical when assigning values: the type of the value must be exactly the same as the type of the variable or constant.

-the type of the operation must be the same: the data type of the participating operation must be consistent to operate.

However, in practical use, it is often necessary to operate between different types of values, which requires a new syntax to accommodate this need, which is the conversion of data types.

In numerical processing this part, the logic of the computer and the reality is not quite the same, for the reality, 1 and 1.0 is no different, but for the computer, 1 is an integer type, and 1.0 is a decimal type, its in-memory storage

And the space occupied are different, so type conversions are necessary inside the computer.

There are two types of data type conversions in the Java language:

-Automatic type conversion: The compiler automatically completes the type conversion and does not need to write code in the program.

-Coercion of type conversions: Forces the compiler to make type conversions and must write code in the program.

Because the Boolean type in the base data type is not a numeric type, the conversion of the base data type is a conversion between 7 types other than the Boolean type. Here's a detailed description of the rules for two types of conversions,

Applicable occasions and the need to pay attention to the issue.

1> Automatic data type conversion

Conversion rules: From a type with a small storage scope to a type with a large storage range.

The specific rules are:byte→short (char) →int→long→float→double

When the operation is performed, integer and floating-point operations are converted to floating-point types, and long and floating-point operations are also converted to floating-point types.

2> Forcing data type conversions

This class type conversion is likely to have a loss of precision, so you must write the appropriate code and be able to tolerate that type of conversion.

The specific rules are: Double→float→long→int→short (char) →byte

The syntax format is: (the type converted to) the value that needs to be converted

Attention:

        Decimal to an integer, the use is to go to 1 method, unconditionally shed the fractional part.

Direct conversion rules for integers (byte, char, short, int, long)

            A, long and short direct interception

as             int i=0xffffff01; byte b= (byte) i; System.out.println (b); output is 1, direct intercept after eight bit 0x01.

            B, short variable length if it is a signed bit, the execution symbol extension except char;

                  No sign bits, such as Char, are directly preceded by the symbol 0.

Such as(int)(char)(byte)-1    输出是65535.

First step: int->byte 0xffffffffffffffffffffffffffffffff intercept eight bit 0xFFFFFFFF

Step two: Byte->char 0xFFFFFFFF signed execution symbol expansion negative number in front of 1 positive complement 0 0XFFFFFFFFFFFFFFFF

Step three: Char->int 0xfffffffffffffff char to int direct complement 0 0X0000000000000000FFFFFFFFFFFFFFFF

And finally became 65535.

2) Reference data type conversions

This section is reviewed in object-oriented.

JAVA SE Basics Review-Basic program design (1)

Related Article

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.