Talk about the storage mode of integer type (short int long) in Java

There are four integer types in Java, namely byte short int long, where byte has only one byte 0 or 1, which will not be explained in detail here.

The other three types are as follows:

1. Basic type: short binary digits: 16 wrapper class: Java Lang.short minimum: short MIN_ Value = - 32768 (- the 15th power of 2) maximum value: short. Max_value = 32767 (the 15th power of 2 - 1)

2. Basic type: int binary digits: 32 wrapper class: Java Lang.integer minimum: integer MIN_ Value = - 2147483648 (- 31st power of 2) maximum value: integer. Max_value = 2147483647 (31st power of 2 - 1)

3. Basic type: long binary digits: 64 wrapper class: Java Lang.long Min: MIN_ Value = - 9223372036854775808 (- 63rd power of 2) maximum value: long. Max_value = 9223372036854775807 (63rd power of 2 - 1)

Take the short type for example:

First of all, we need to understand that the highest bit in the computer is the sign bit, 0 represents a positive number and 1 represents a negative number. In the computer, the data is represented by its complement, but the positive complement is itself, and the negative complement is the complement obtained by adding one to the negative source code.

1、 The original code, inverse code and complement of positive numbers are equal

Example: 0000 1010 (represents + 10 in the decimal system, and the first digit of the left number is the sign bit) its original code, inverse code and complement are 0000 1010

2、 The original code of a negative number is itself, the inverse code of a negative number is to keep the symbol unchanged and reverse the number behind the symbol, and the complement code of a negative number is to reverse the original code and add 1

Example: the original code of 1000 1010 (representing - 10 in decimal system) is 1000 1010, its inverse code is 1111 0101, and its complement is 1111 0110

For example, short type: - 1 binary indication: 10000000 00000001 (maximum negative integer - 1)

Reverse: 11111111 11111110

Add 1 Complement: 11111111 11111111 (marking method of the largest negative integer-1 in the computer)

Understand the binary mark of short:

Look at this first: the smallest negative integer - 32768 binary mark in the computer: 1000000000000

The largest negative integer - 1 binary mark in the computer: 11111111 11111111

0 binary mark in computer: 00000000 0000000

The smallest positive integer 1 is binary in the computer: 00000000 00000001

Maximum positive integer: 32767 binary mark in computer: 01111111

After the minimum negative number - 32768 plus 1, the binary in the computer is marked as: 10000000 00000001, and continue to add 1 until 11111111111 (- 1) reach the maximum negative integer, and then add 1 to become: 1 00000000 0000000. Note that the byte length here is 17 bits, while the short type takes only 16 bits, that is: 00000000 0000000, so it becomes 0 after - 1 + 1, and then 00000000 0000000 continues to add 1 until 01111111 reaches the maximum positive integer (32767), 32767 plus 1 becomes: 10000000 000000 is the smallest negative integer - 32768 (2 ^ 15)

PS: conversion between data types @ h_ 502_ 72@

1). There are two ways to convert simple type data: automatic conversion and cast, which usually occur in expressions or when parameters of methods are passed.

Automatic conversion

Specifically, when a "small" data is calculated with a "large" data, the system will automatically convert the "small" data into "large" data for operation. When calling a method, the actual parameter is "small", and the formal parameter data of the called method is "large" (if there is a match, of course, the matching method will be called directly), the system will automatically convert the "small" data into "big" data, and then call the method. Naturally, multiple overloaded methods with the same name will be converted into the "closest" big data and called. From "small" to "large", these types are (byte, short, char) -- int -- long -- float -- double. What we call "big" and "small" here does not mean the number of bytes occupied, but the size of the range representing the value.

① The following statements can be used directly in Java:

byte b; int i=b; long l=b; float f=b; double d=b;

② If the low-level type is char type, it will be converted to the corresponding ASCII code value when converting to high-level type (integer), such as

char c='c'; int i=c;

System. out. println("output:"+i); Output: output: 99;

③ For byte, short and char types, they are level, so they cannot be automatically converted to each other. The following forced type conversion can be used.

short i=99 ; char c=(char)i; System. out. println("output:"+c); Output: output: C;

Force conversion

You can use cast when converting "big" data to "small" data. That is, you must use the following statement format: int n = (int) 3.14159/2; It is conceivable that this conversion may certainly lead to overflow or decline in accuracy.

2) The data type of an expression is automatically promoted. For the automatic promotion of types, pay attention to the following rules.

① All byte and char type values will be promoted to int type;

② If one operand is long, the calculation result is long;

③ If one operand is of float type, the calculation result is of float type;

④ If one operand is of double type, the calculation result is of double type;

Example, byte B; b=3; b=(byte)(b*3);// Byte must be declared.

3) Packaging class transition type conversion

In general, we first declare a variable and then generate a corresponding wrapper class, so we can use various methods of the wrapper class for type conversion. For example:

① When you want to convert float type to double type:

float f1=100.00f; Float F1=new Float(f1); double d1=F1. doubleValue();// F1. Doublevalue() is a method of the float class that returns a double value type

② When you want to convert double type to int type:

double d1=100.00; Double D1=new Double(d1); int i1=D1. intValue();

Simple type variables are converted to the corresponding wrapper class, and the constructor of the wrapper class can be used. Namely: Boolean (Boolean value), character (char value), integer (int value), long (long value), float (float value), double (double value)

In each packaging category, the total tangible is ×× Value() to get its corresponding simple type data. Using this method, the conversion between different numerical variables can also be realized. For example, for a double precision real class, intvalue() can get its corresponding integer variable, and doublevalue() can get its corresponding double precision real variable.

4) Conversion between string and other types

Conversion of other types to strings

① Call the string conversion method of the class: x.tostring();

② Automatic conversion: x + "";

③ Method of using string: string volueOf(X);

String as a value to other types of conversion

① First convert to the corresponding wrapper instance, and then call the corresponding method to convert to other types

For example, the format of "32.1" converted to double value in character is: new float ("32.1") doubleValue()。 You can also use: double valueOf("32.1"). doubleValue()

② Static parsexxx method

③ Getnumericvalue (char CH) method of character

5) Conversion between date class and other data types

There is no direct correspondence between integer type and date class, but you can use int type to represent year, month, day, hour, minute and second respectively, so as to establish a correspondence between them. In this conversion, you can use three forms of date class constructor:

① Date (int year, int month, int date): the year, month and day are represented by int type ② date (int year, int date, int hrs, int min): the year, month, day, hour and minute are represented by int type ③ date (int year, int min, int SEC): the year, month, day, hour, minute and second are represented by int type

There is an interesting correspondence between the long integer and the date class, that is, a time is expressed as the number of milliseconds from 0:0:0, Greenwich mean time on January 1, 1970. For this correspondence, the date class also has its corresponding constructor: date (long date).

Get the year, month, day, hour, minute, second and week in the date class. You can use the getyear(), getmonth(), getdate(), gethours(), getminutes(), getseconds(), getday() methods of the date class. You can also understand it as converting the date class into int.

The gettime () method of the date class can get the long integer number corresponding to a time as we mentioned earlier. Like the wrapper class, the date class also has a toString () method to convert it into a string class.

Sometimes we want to get a specific format of date, such as 20020324. We can use the following methods to introduce it at the beginning of the file,

Summary: only Boolean does not participate in data type conversion @ H_ 502_ 72@

(1) . automatic type conversion:

a. Constants can be automatically typed within the range of tables

b. Small data range can automatically convert large data types (pay attention to special cases)

Int to float, long to float, long to double will not be automatically converted, otherwise precision will be lost

c. The reference type can be automatically converted to the parent class

d. Basic types and their wrapper types can be converted to each other

(2) . cast type: enclose the target type in parentheses and put it in front of the variable

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>