Introduction to the system of _android between Android data types

Source: Internet
Author: User
Tags numeric wrapper
Some novice Android friends may experience the distress of conversion between data types in Java, such as conversion between integers and float,double, conversions between integers and string types, and handling, display time problems, and so on. The following author on the development of some of the experience introduced to everyone.

We know that the data types of Android are classified into three categories, Boolean, character, and numeric, and the numeric type is divided into integral and floating-point types, and the Java variable type is Boolean, as opposed to the data type; character char; integer byte, short, int, long ; float float, double. The four types of integer variables and two floating-point variables correspond to different precision and range respectively. In addition, we often use two kinds of variables, string and date. The mutual conversion between these variable types is often used in our programming, and in the following discussion we will explain how to implement these transformations.

1 Kinds of data type conversions \ r
The conversion of Java data types is generally divided into three categories, respectively:
(1). Conversion between simple data types
(2). Conversion of strings to other data types
(3). Other practical data type conversions
Here we discuss the three types of conversions separately.

2 conversions between simple data types
In Java, integral, solid, and character types are treated as a simple data type, from low-level to advanced, respectively
[Center] (Byte,short,char)--int--long--float--double[/center]
The conversion between simple data types can be divided into:
Low-level to advanced automatic type conversions
Advanced to low-level forced-type conversions
Wrapper class transition type can be converted

2.1 Automatic type Conversion
Lower-level variables can be directly converted to advanced variables, which I call automatic type conversion, for example, 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 value, for example, \ r
Char c= ' C '; int i=c; System.out.println ("Output:" +i);
Output: output:99;
For Byte,short,char three types, they are peers, so they 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;
However, according to the author's experience, byte,short,int three types are integral type, so if you are working with integer data, it is best to use the int type uniformly.

2.2 Coercion Type conversion
When you convert an advanced variable to a low-level variable, the situation is a bit more complicated, and you can use coercion type conversions. That is, you must use the following statement format:
int i=99;byte b= (byte) I;char c= (char) i;float f= (float) i;
As can be imagined, this conversion is certainly likely to lead to overflow or precision degradation, so I do not recommend the use of this conversion.

2.3 Wrapper class Transition type conversion
As we discuss the conversion of other variable types, we need to understand the Java wrapper class, the so-called wrapper class, is that you can directly represent a simple type of variable as a class, in the execution of variable types of mutual conversion, we will use the bulk of these wrapper classes. Java has a total of six wrapper classes, Boolean, Character, Integer, long, float, and Double, literally we can see that they correspond to Boolean, char, int, long, float and double. and string and date themselves are classes. So there is no concept of packaging class.
When converting between simple data types (automatic or cast), we can always use the wrapper class for intermediate transitions.
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:
Example 1, when you want to convert a float to a double type:
float f1=100.00f; Float f1=new Float (F1);D ouble d1=f1.doublevalue ();//f1.doublevalue () method for returning a Double value of float class
When you want to convert the double type to an int type:
Double d1=100.00; Double D1=new double (D1); int I1=d1.intvalue ();
When you want to convert an int to a double, automatically converts:
int i1=200; Double d1=i1;
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
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.
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.