Name Haishu - Data Type1.0

xiaoxiao2021-03-06  49

Data type of 眀海 棠

Draw a little, step by step;

Growing is in place, mature in invisible

Types

1.1. About the reference

The Java language first assumes that we only want to perform object-oriented programming, that is to say, you must first transfer your ideas into a pure object-oriented world before using it. Everything in Java is in an object. A Java program is also an object itself.

Anything can be seen as an object, but pay attention to although everything will be regarded as an object, the identifier of the operation is actually a reference to an object (Reference). Just because there is a reference that doesn't mean that there must be an object to connect to it, if you want to accommodate a word or sentence, you can create a string reference:

String S;

But here is only the reference is not an object. If you send a message to S, you will get a runtime error, which is because s does not connect to anything; therefore a more secure approach is to create a reference I remember that initialization is performed anyway:

String s = "ASDF";

When you create a reference, we hope that it is connected to the same new object, usually with the New keyword to achieve this. The meaning of New is an object that turns me into this type, so the example above can also be reached:

String s = new string ("ASDF");

1.2. Java type

There are several concepts in the Java language: class (ie, our own type of type), basic type (system originally included), object, reference

We use the Class ID in Java that is called class, from class production objects, objects belong to class; objects are specified by their classes, and the basic type is declared and initialized by procedures. The basic types have corresponding packages, and there is a corresponding method in the package, thereby achieving the transformation of basic types to class objects;

For objects, by reference to operation objects, reference is not object itself; for basic types, reference is both type value itself;

1.2.1. Basic type

There is a series of types in Java to take us special treatments, you can imagine them into basic primary or primary primitive types, which will be used frequently when programming; it is specifically treated because of NEW to create certain objects, especially It is a small simple variable, efficiency is not particularly high, because New will put the object in the stack; for these types, Java provides a similar approach to C and C , that is, do not create these variables with NEW, and Yes, create an automatic variable, this variable is not a reference, why not reference? Because of the specific value, it is placed in the stack, not the memory heap, thereby achieving higher efficiency;

Java decided in advance, the size of each primary type, just like in most languages; this size does not change as you change the hardware platform operating system, and the size cannot be changed is that the Java program has strong portability. one of the reasons;

Java data types are divided into three categories, namely,,, Char; integer, short, int, long; floating-point float, double. Four of four integer variables and two floating-point variables correspond to different precision and ranges, respectively. Main type

size

Minimum

Maximum

Packaging type

Boolean

---

--------

----

Boolean

Charr

16-bit

Unicode 0

Unicode 216- 1

Character

Byte

8-bit

-128

127

Byte

Short

16-bit

-215

215-1

Short

Int

32-bit

-231

231-1

Integer

Long

64-bit

-263

263-1

Long

Float

32-bit

IEEE754

IEEE754

Float

Double

64-bit

IEEE754

IEEE754

Double

Void

-

-

-

Void

note:

1, all numerical types are symbolically negative;

2, the size of the Boolean Boolean type does not clearly regulate, as long as you can accommodate TRUE or FALSE, it is OK;

The original data type also has its own packaging Wrapper class, which means that if you want to make a non-main object in the stack indicate that the main type, you should use the corresponding package; for example:

CHAR C = 'x';

Character C = New Character (C);

Can also be used directly:

Character C = New Character ('x');

1.2.2. Java packaging class

The so-called packaging class is that we can directly represent the simple type of variables as a class. When performing mutual conversion of variable types, we will use these packaging classes.

Java has six packaging classes, namely Boolean, Character, Integer, Long, Float and Double, which we can see them from the literal, which correspond to Boolean, CHAR, INT, LONG, FLOAT, and DOUBLE. And String itself is class. So there is no concept of packaging classes.

1.2.3. Basic type, package and its interchange

1.2.3.1. Mutual conversion in integer, real, character variable

In Java integer, real, character type is considered the same class data, these types are directly (Byte, short, char), respectively, and low variables can be directly Convert to advanced variables, for example, the following statement can pass directly in Java:

BYTE B;

INT i = B;

When the advanced variable is converted to a low-level variable, the situation will be complex, you can use forced type conversion. That is, you have to use the following statement format:

INT I;

BYTE B = (Byte) i;

Imagine that this conversion may definitely lead to a decline in overflow or accuracy, so we do not recommend this conversion.

The assignment of floating point is DUBLE type, if f or f is added after floating point numbers, then D or D is Double, the floating point number of scientific counting is also Double type And the Double's precision is higher than Float, and a high-precision Double assignment is required to enforce type conversion when a low-precision Float is required, and it is not necessary. Float f = 11.1; (is wrong)

1.2.3.2. Simple type variables and mutual conversion between packaging classes

Simple type variables are converted to the corresponding packaging class, which can utilize the constructor of the package class. which is:

Boolean (Boolean Value);

Character (char value);

INTEGER (INT VALUE);

Long (long value);

Float value;

Double (Double Value)

In each package class, a method of total tab is ×× value () to obtain its corresponding simple type data. With this method, the conversion between different numerical variables can be achieved, for example, for a double-precision real type, intValue () can obtain its corresponding integer variable, while DoubleValue () can get its corresponding double precision Type variable.

1.2.3.3. String class and other data types of mutual conversion

For these packaging classes above, in addition to Character, there are configuration functions that can directly use string parameters, which makes us convert String class to these data types, which is simpler, namely:

Boolean (String S);

Integer (String S);

Long (string s);

FLOAT (String S);

Double (String S);

Now we have a characteristic variable, in fact, the String class can be understood as a CHAR type array, so we can find such a method in the String class to implement this conversion: Charat (int index) can get a string class The characters in a location, TocharaRray () can convert the entire String class into an array of char.

For all packaging classes, there is a method called toString () to convert it into a corresponding String class, and for integer and long integrings, you can also use TobinaryString (Int i), tohexString (INT i) TOOCTALSTRING (INT i) is converted to the String class in binary, hexadecimal and octal form, respectively.

1.2.3.4. Character type Directly converted to other data types

Converting the character variable to the numerical variable actually has two corresponding relationships, in the conversion of our first part, actually converts it to the corresponding ASCII code, but we sometimes need another Conversion relationship, for example, '

1'

That is, the value of 1, not its ASCII code, for this conversion, we can use the Character's GetNumericValue (CHAR CH) method.

Package com.minght.sys.util;

Public class objconvert {

Private objconvert () {

}

/ *********************************************************************************************************************************************************

/ **

* Translate the Integer package to int base type

* @Param I INTEGER

* @Return Int

* /

Public Static Int IntegertOint (Integer i) {

IF (i == null)

Return 0;

INT i = I.intValue ();

Return I;

}

/ **

* Translate int base type into an Integer packn object

* @Param I Int

* @Return Integer

* /

Public Static Integer INTTOINTEGER (INT i) {

Integer i = new integer (i);

Return I;

}

/ **

* Transform the Charater packaging object to a char basic type

* @Param C Character

* @Return Char

* /

Public static char charattlertochar (java.lang.character C) {

IF (c == null)

Return '0';

CHAR C = C.CHARVALUE ();

Return C;

}

/ **

* Translate the char Basic type into a CHARATER wrapper object

* @Param C Char

* @Return Character

* /

Public Static Character Chartochar (CHAR C) {

Character C = New Character ('c');

Return C;

}

/ **

* Translate the LONG wrapper object to long basic type

* @Param l Long

* @Return Long

* /

Public Static Long Longtolong (long L) {

IF (l == NULL) {

Return

0L

;

}

Long L = L.LongValue ();

Return L;

}

/ **

* Translate the LONG basic type to long wrapper object

* @Param l Long

* @Return Long

* /

Public Static Long Longtolong (long L) {

Long l = new long (l);

Return L;

}

/ **

* Translate FLOAT wrapper object to float basic type

* @Param f float

* @Return Float

* /

Public Static Float floattofloat (float f) {

IF (f == null) {

Return

-1f

;

}

Float f = f.floatvalue ();

Return F;

}

/ **

* Translate FLOAT basic type to FLOAT wrapper object

* @Param f float

* @Return Float

* /

Public Static Float floattofloat (float f) {

Float f = new float (f);

Return F;

}

/ **

* Translate the Double wrapper object to double basic type

* @PARAM D DOUBLE

* @Return Double

* /

Public Static Double DouBletoDouble (Double D) {

IF (D == NULL)

Return 0D;

Double D = D. DoubleValue ();

Return D;

}

/ **

* Translate Double Basic Type to Double Package Object

* @PARAM D DOUBLE

* @Return Double

* /

Public Static Double DoubletoDouble (Double D) {Double D = New Double (D);

Return D;

}

/ **

* Translate Boolean packaging object to Boolean basic type

* @Param B Boolean

* @Return Boolean

* /

Public Static Boolean Booleantoboolean (Boolean B) {

IF (b == null)

Return False;

Boolean B = B.BOOLEANVALUE ();

Return B;

}

/ **

* Transform Boolean basic type to Boolean object type

* @Param B Boolean

* @Return Boolean

* /

Public Static Boolean Booleantoboolean (Boolean B) {

Boolean b = new boolean (b);

Return B;

}

/ ************************** The mutual conversion of various types and string types ************* ********* /

/ **

* Translate String object to int base type

* @Param s string

* @Return Int

* /

Public static int stoi (string s) {

INT I = -1;

IF (s == null || s.trim (). Length () == 0) {

Return I;

}

Try {

i = integer.parseint (s);

} catch (exception e) {

Debug.print ("Stirng Convet To Int Error!");

Return I;

}

Return I;

}

/ **

* Translate the int type to String

* @Param I Int

* @Return String

* /

Public Static String ITOS (INT i) {

String s = INTTOINTEGER (i) .tostring ();

Return S;

}

/ **

* Translate String Type to Long Type

* @Param s string

* @Return Long

* /

Public Static Long Stol (String S) {

Long L =

-1L

;

IF (s == null || s.trim (). Length () == 0) {

Return L;

}

Try {

L = long.parselong (s);

} catch (exception e) {

Debug.println ("String Convert to Long Error!");

Return L;

}

Return L;

}

/ **

* Translate the long type to String Type

* @Param l Long

* @Return String

* /

Public Static String LTOS (long L) {

String s = longtolong (l) .tostring ();

Return S;

}

/ **

* Translate String Type to FLOAT Type

* @Param s string

* @Return Float

* /

Public static float stof (string s) {

Float f =

-1f

;

IF (s == null || s.LENGTH () == 0) {RETURN F;

}

Try {

f = float.parsefloat (s);

} catch (exception e) {

Debug.println ("String Convert to Float Error);

Return F;

}

Return F;

}

/ **

* Translate the Float type to string type

* @Param f float

* @Return String

* /

Public Static String Ftos (Float F) {

String s = floattofloat (f) .tostring ();

Return S;

}

/ **

* Translate String Type to Double Type

* @Param s string

* @Return Double

* /

Public static double stod (string s) {

Double D = -1d;

IF (s == null || s.Length () == 0) {

Return D;

}

Try {

D = double.parsedouble (s);

} catch (exception e) {

Debug.println ("String Convert to Double Error");

Return D;

}

Return D;

}

/ **

* Translate the Double type to String Type

* @PARAM D DOUBLE

* @Return String

* /

Public Static String Dtos (Double D) {

String s = doubletodouble (d) .tostring ();

Return S;

}

/ **

* Test

* @Param args string []

* /

Public static void main (String [] args) {

Integer i = new integer (10);

INT I = IntegertOint (i);

System.out.println ("Convert Integer to Int: i);

}

}

1.2.4. High precision

Java offers two classes that specifically despite high-precision operations Biginteger and BigDecimal, although they can be roughly divided into the same categories as the packaging, but there is no corresponding primary type; these two classes have their own one A series of methods, similar to the operation of our main type, that is to say, can do it like Biginteger and BigDecimal with Biginteger and BigDecimal, rather than using operators. In addition, due to more, the calculation speed will slow down, and we sacrifice the speed, but in exchange for accuracy.

1.2.4.1. High precision integer Biginteger

Biginteger supports an integer of any precision, that is, we can accurately represent an integer value of any size; it will not lose any information during the operation;

There are all basic arithmetic operation methods in the Biginteger class, such as add, minus, multiply, division, and bit operations that may be used, different or non-left, right movement, etc. Here are some examples of methods: Of course, if you need to have more methods, you can check the Java API.

Package com.minght.test;

Import java.math. *;

/ **

*

*

title: Open source, open *

Description: OPeansource

*

Copyright: Copyright (C) 2004

*

Company: 眀 海

* @Author Haitang Ming

* @version 1.0

* /

Public class bigintegertest {

Public Bigintegertest () {

}

/ **

* Test Biginteger

* /

Public static void testbiginteger () {

Biginteger bi = New Biginteger ("888");

// Multiply: Multiplication

Biginteger Result = bi.multiply (New Biginteger ("2"));

System.out.println (Result);

// Divide: division

Result = bi.divide (New Biginteger ("2"));

System.out.println (Result);

// Add: add

Result = bi.add (New Biginteger ("232"));

System.out.println (Result);

// Subtract: subtraction

Result = bi.subtract (New Biginteger ("23122"));

System.out.println (Result);

Result = bi.shiftright (2);

System.out.println (Result);

}

Public static void main (String [] args) {

Testbiginteger ();

}

}

1.2.4.2. High-precision floating point BigDecimal

Some non-integer values ​​(such as a few dollars and a few decisions such as a few decisions) need to be precise. The floating point is not a precise value, so use them will result in rounding errors. Therefore, using floating point numbers to try to indicate that the quantity of the amount of money is not a good idea. The use of floating point is used for dollar and coding calculations to get catastrophic consequences. The floating point number is best used to represent the value of the measured value, which is not accurate from the beginning.

From JDK 1.3, Java developers have another numerical representation to express non-integers: BigDecimal. BigDecimal is a standard class that does not require special support in the compiler, which can represent the decimal of any precision and calculate them. Internally, the value of any precision can be used to represent the value of any range and a conversion factor, and the conversion factor indicates how many bits of the left-shifter decimal point, thereby obtaining the value within the desired range. Therefore, in the form of a number represented by BigDecimal is UnscaledValue * 10-Scale.

Method for adding, minus, multiplication, and division provides arithmetic operations for BigDecimal values. Since the BigDecimal object is not variable, each of these methods produces a new BigDecimal object. Therefore, because the cost of creating an object, BigDecimal is not suitable for a large number of mathematical calculations, but the purpose of designing it is to accurately represent the decimal. If you are looking for a value that can accurately indicate such a currency, BigDecimal can be better enough to do this task.

As the floating point type, BigDecimal also has some strange behavior. Especially if you use the equals () method to detect whether the values ​​are equal, be careful. The equals () method believes that both BigDecimal values ​​indicating the same number but different (for example, 100.00 and 100.000) are unequal. However, the Compareto () method will think that both numbers are equal, so compare () instead of equals () should be used when comparing two BigDecimal values ​​from the value. There are also some cases, and the decimal operation of any precision is still unable to indicate precise results. For example, 1 is divided into decimal decimal in which an infinite cycle is generated. 111111 ... For this reason, BigDecimal allows you to explicitly control. The MovePointLeft () method supports the precise division of the power of 10.

For BigDecimal, there are several available constructor. One of the constructor is used as input, and the other is an integer and conversion factor as an input, and there is an input as an input in a decimal String. Be careful with the BigDecimal (Double) constructor, because if it does not understand it, a rounding error is generated during the calculation process. Use constructor based on integer or string.

If you use the BigDecimal (Double) constructor, it is not appropriate to pass to the JDBC setBigDecimal () method, it will cause an exception in the JDBC driver. For example, consider the following JDBC code, the code wants to store the number 0.01 to the decimal field:

PreparedStatement PS =

Connection.PrepareStatement ("INSERT INTO FOO SET NAME = ?, value =?");

Ps.setstring (1, "penny");

Ps.SetBigDecimal (2, New BigDecimal (0.01));

ps.executeUpdate ();

When performing this seemingly harmless code, it will throw some confusing anomalies (depending on the specific JDBC driver), because the two precision approximation of 0.01 will result in a large conversion value, which may make JDBC The driver or database is confused. The JDBC driver will produce an exception, but may not indicate where the code is actually wrong, unless the limitations of binary floating point numbers. Instead, use BigDecimal ("0.01") or BigDecimal (1, 2) to construct BigDecimal to avoid such problems, because both methods can accurately represent decimals.

Package com.minght.sys.util;

Import java.math.bigdecimal;

/ **

*

*

Title: Open source, open

*

Description: OPeansource

*

Copyright: Copyright (C) 2004

*

Company: 眀 海

* @Author Haitang Ming

* @version 1.0

* /

Public class bigdecimalutil {

/ / Default division operation accuracy, and how much is the decimal point

Private static final int dev_div_scale = 2;

// This class cannot be instantiated

Private bigdecimalutil () {

}

/ **

* Provide precise addition operations. * @Param V1 is added

* @Param V2 plus number

* @return two parameters and

* /

Public Static Double Add (Double V1, Double V2) {

BigDecimal B1 = New BigDecimal (Double.ToString (V1));

BigDecimal B2 = New BigDecimal (Double.toString (V2));

Return (b1.add (b2)). DoubleValue ();

}

/ **

* Provide precise subtraction operations.

* @Param V1 is reduced

* @PARAM V2 reduction

* @return two parameters

* /

Public Static Double Sub (Double V1, Double V2) {

BigDecimal B1 = New BigDecimal (Double.ToString (V1));

BigDecimal B2 = New BigDecimal (Double.toString (V2));

Return (b1.subtract (b2)). DoubleValue ();

}

/ **

* Provide precise multiplication.

* @Param V1 is multiplied

* @Param V2 multiplier

* @Return's product

* /

Public Static Double Mul (Double V1, Double V2) {

BigDecimal B1 = New BigDecimal (Double.ToString (V1));

BigDecimal B2 = New BigDecimal (Double.toString (V2));

Return (b1.multiply (b2)). DoubleValue ();

}

/ **

* Provide (relatively) accurate division operation, accurate to

* How many bits after the decimal point, the numbers in the future are rounded.

* @PARAM V1 is divided

* @Param V2 divisor

* @Return two parameters

* /

Public Static Double Div (Double V1, Double V2) {

Return Div (V1, V2, DEF_DIV_SCALE);

}

/ **

* Provide (relatively) precise division operations. When an endless case occurs, the scale parameter refers to

* Define accuracy, the number of numbers in the future.

* @PARAM V1 is divided

* @Param V2 divisor

* @Param Scale means that several digits are required to be accurate to the decimal point.

* @Return two parameters

* /

Public Static Double Div (Double V1, Double V2, INT Scale) {

IF (scale <0) {

Throw new IllegalargumentException

"The Scale Must Be a Positive Integer Or Zero");

}

BigDecimal B1 = New BigDecimal (Double.ToString (V1));

BigDecimal B2 = New BigDecimal (Double.toString (V2));

Return (b1.divide (b2, scale, bigdecimal.round_half_up)). DoubleValue ();

}

/ **

* Provide precise decimal rounds.

* @Param V Needs a number of four rounds

* @Param scale reserves a few times

* @Return's result

* /

Public Static Double Round (double v, int scale) {

IF (scale <0) {

Throw new IllegalargumentException

"The Scale Must Be a Positive Integer Or Zero");

}

BigDecimal B = New BigDecimal (Double.ToString (V));

BigDecimal One = New BigDecimal ("1");

Return (B.divide (One, Scale, BigDecimal.Round_Half_up)). DoubleValue ();

}

Public static void main (String [] args) {

Double a = 123.345678;

Double D = ROUND (a, 2);

Debug.println ("Round ( A ", 2) ---> " D);

}

}

Remarks:

From the network, returning to the network;

转载请注明原文地址:https://www.9cbs.com/read-81198.html

New Post(0)