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

Source: Internet
Author: User
Tags wrapper

There are four kinds of integer types in Java, which are byte short int long, where byte is only one byte 0 or 1, which is not explained in detail here.

The other three types are as follows:

1,
Basic type: Short bits number: 16
Packing class: Java.lang.Short
Minimum value: short.min_value=-32768 (-2 of 15 of this side)
Maximum value: short.max_value=32767 (2 of 15 times-1)

2,
Basic type: int bits number: 32
Packing class: Java.lang.Integer
Minimum value: integer.min_value=-2147483648 (-2 of 31 sides)
Maximum value: integer.max_value= 2147483647 (2 of 31 times-1)

3,
Basic type: Long bits number: 64
Packing class: Java.lang.Long
Minimum value: long.min_value=-9223372036854775808 (-2 of 63 sides)
Maximum value: long.max_value=9223372036854775807 (2 of 63 times-1)

Take the short type for example:

First you need to understand that the highest level in the computer is a sign bit, 0 is a positive number, and 1 is a negative number. In the computer, the data is expressed by its complement, but the positive complement is its own, the negative complement is a negative source code to take the inverse plus a get the complement.

First, positive number of original code, inverse code, complement are equal

Example: 0000 1010 (In decimal notation +10, left first digit symbol bit)
Its original code, the inverse code, the complement is 0000 1010

Second, the original code of negative numbers is its own, negative inverse code is the symbol will not change, the number behind the symbol to reverse, for the complement of negative numbers is the original code after the reverse plus 1

Example: 1000 1010 (in decimal notation-10)
Its original code is 1000 1010.
Its counter code is 1111 0101.
Its complement is 1111 0110.

For example, the short type:-12 binary indicator: 10000000 00000001 (Maximum negative integer-1)

Take counter: 11111111 11111110

Plus 1 Complement: 11111111 11111111 (Maximum negative integer-1 in the computer marking method)

Understand all of these again short binary markings:

First look at this: the smallest negative integer-32768 binary indicator in the computer: 10000000 00000000

Max minus integer-1 binary in computer: 11111111 11111111

0 binary indicator in computer: 00000000 0000000

Smallest positive integer 1 binary mark in computer: 00000000 00000001

Maximum positive integer: 32767 binary indicator in computer: 01111111 11111111

The smallest negative number-32768 plus 1-After the computer, the binary is labeled: 10000000 11 straight plus 1 until to 11111111 11111111 (-1) to reach the largest negative integer, and then add 1 to: 1 00000000 0000000 Note the length of bytes here is 17-bit, and the short type takes only 16 bits, that is, 00000000 0000000, so -1+1 becomes 0 then 00000000 00000000 then adds 1 until 01111111 11111111 reaches the maximum positive integer (32767), and 32767 adds 1 changes to: 10000000 00000000 is the smallest negative integer-32768 (2^15)

PS: Conversion between data types

1. There are two ways to convert between simple types of data: automatic conversions and casts, which usually occur in an expression or when a parameter of a method is passed.

Automatic conversion

Specifically, when a "small" data with a more "large" data operation, the system will automatically convert "small" data into "large" data, and then operation. In the method call, the actual parameter is more "small", and the method is called when the formal parameter data is more "big" (if there is a match, of course will directly call the matching method), the system will automatically convert "small" data into "large" data, and then the method of invocation, naturally, for multiple overloaded methods with the same name, will be converted to the most " Close to the "big" data and make the call. These types are from "small" to "large" respectively (Byte,short,char)--int--long--float-double. What we call "big" and "small" here is not how much bytes are occupied, but the size of the range that represents the value.

① The following statements can be passed directly in Java:

byte B;int i=b; Long l=b; float f=b; Double d=b;

② If the low-level type is char, the conversion to the Advanced Type (integer) is converted to the corresponding ASCII code value, for example

Char c= ' C '; int i=c;

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

③ for Byte,short,char three types, they are lateral and therefore cannot be automatically converted to each other, and you can use the following coercion type conversions.

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

Forced conversions

When you convert large data to small data, you can use coercion type conversions. That is, you have to use the following statement format: int n= (int) 3.14159/2; it can be imagined that this conversion is certainly likely to result in an overflow or a drop in precision.

2 automatic elevation of the data type of the expression, the automatic elevation of the type, notice the following rules.

① the value of all Byte,short,char types will be promoted to int type;

② if one of the operands is a long, the result is a long type;

③ if one of the operands is float, the result is a float type;

④ if one of the operands is a double, the result is a double type;

example, byte B; b=3; B= (Byte) (b*3);//must declare byte.

3) wrapper class transition type conversion

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

① when you want to convert a float to a double type:

float f1=100.00f;
Float f1=new Float (F1);
Double D1=f1.doublevalue ();//f1.doublevalue () method for returning a double value of float class

② when you want to convert a double to an int type:

Double d1=100.00;
Double D1=new double (D1);
int I1=d1.intvalue ();

A variable of a simple type is converted to the appropriate wrapper class, which can take advantage of the constructor of the wrapper class. That is: Boolean (boolean value), Character (char value), Integer (int value), long (Long value), float (float value), double (double Value

and in each wrapper class, the total visible as Xxvalue () method, to get its corresponding simple type of data. By using this method, we can also realize the transformation between different numerical variables, 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 strings and other types

Conversion of other types to strings

① invoke the string conversion method of the class: X.tostring ();

② Automatic conversion: x+ "";

③ method using String: String.volueof (X);

string as a value, to other types of conversions

① first to the corresponding wrapper instance, then calls the corresponding method to convert to other types

For example, the format for the value of the character "32.1" to convert a double is: New Float ("32.1"). Doublevalue (). can also be used: double.valueof ("32.1"). Doublevalue ()

② static Parsexxx method

String s = "1";
byte B = byte.parsebyte (s);
Short T = Short.parseshort (s);
int i = Integer.parseint (s);
Long L = Long.parselong (s);
Float f = float.parsefloat (s);
Double d = double.parsedouble (s);

③character getnumericvalue (char ch) method

5 conversion between date class and other data types

There is no direct correspondence between the integral type and the date class. You can just use the int type to represent the year, month, day, time, minute, and second, so that you have a corresponding relationship between the two, and you can use the three forms of the date class constructor when making this transition:

①date (int year, int month, int Date): INT-type for years, months, days
②date (int year, int month, int Date, int hrs, int min): int-type for years, months, days, hours, minutes
③date (int year, int month, int Date, int hrs, int min, int sec): INT-type for years, months, days, hours, minutes, seconds

An interesting correspondence between the long and the date classes is to represent a time as the number of milliseconds from 0:0 Greenwich Mean Time of January 1, 1970 to 0 seconds. For this correspondence, the date class also has its corresponding constructor: date (long date).

Gets the year, month, day, time, minute, second, and week in the date class you can use the Date class getyear (), getmonth (), GetDate (), getHours (), getminutes (), getseconds (), Getday ( method, you can also interpret it as converting the date class to int.

The GetTime () method of the date class can get the long integer corresponding to the time we mentioned earlier, and like the wrapper class, the date class also has a ToString () method that can convert it to a string class.

Sometimes we want to get a specific format for date, such as 20020324, and we can use the following methods, starting with the introduction of the file,

Import Java.text.SimpleDateFormat;
Import java.util.*;
Java.util.Date Date = new Java.util.Date ();
If you want to get the YyyyMMDD format
simpledateformat sy1=new simpledateformat ("YyyyMMDD");
String Dateformat=sy1.format (date);
If want to separate get year, month, day
SimpleDateFormat sy=new simpledateformat ("yyyy");
SimpleDateFormat sm=new SimpleDateFormat ("MM");
SimpleDateFormat sd=new SimpleDateFormat ("DD");
String Syear=sy.format (date);
String Smon=sm.format (date);
String Sday=sd.format (date);

Summary: Only Boolean does not participate in data type conversions

(1). Automatic Type conversions:

A. Constants are capable of automatic type conversions within a range of tables

B. Small data-type conversions with large automatic data types (attention to special cases)

int to Float,long to Float,long to double is not automatically converted, otherwise it will lose precision

C. Reference types can be automatically converted to the parent class

D. Basic types and their packaging types are interchangeable

(2). Force type conversion: Enclose the target type in parentheses, before the variable

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.