Conversion of Java data types

Source: Internet
Author: User

Conversion of Java data types, conversion of basic types to each other

1 How do I convert string strings to integer int?
A. There are 2 methods:
1). int i = Integer.parseint ([String]); Or
i = Integer.parseint ([string],[int radix]);
2). int i = integer.valueof (my_str). Intvalue ();
Note: The string is converted to Double, Float, and Long in a similar way.

2 How do I convert an integer int to a string string?

A. There are 3 ways:
1.) String s = string.valueof (i);
2.) String s = integer.tostring (i);
3.) String s = "" + I;
Note: Double, Float, Long turn into a string in a similar way.

1. Simple types of Java and their wrapper classes1.1Java Simple Type and package classwe know that the Java language is a typical object-oriented programming language, but Java provides support for these non-object-oriented, simple data types, given the simple structure of some basic data types and the advantages of small memory and fast access. Of course, when Java provides a large number of other classes, it also provides a wrapper class corresponding to the simple data type, so that Java has an int and an integer (float and float, double, and double ...). Different data types. There are two main classes of data types for the Java language: One is a simple type, also called the primary type (Primitive), and the other is a reference type (Reference). A simple type variable stores a specific value, whereas a variable of a reference type stores a reference to the object. Java determines the size of each of the simple types. These sizes do not change as the machine structure changes. This immutable size is one of the reasons why Java programs have a strong ability to migrate. The following table lists the simple types defined in Java, the number of bits, and the corresponding wrapper classes.  simple types in table Java
simple type < Span style= "Font-size:small;" >boolean byte char short int long float double void
bits number < Span style= "Font-size:small;" >1 8 16 16 32 64 32 64 --
Wrapper class Boolean Byte Character Short Integer Long Float Double Void
 1.2 Why Use encapsulation classesin the case of int and integer, although they all represent a 32-bit integer in nature, they are different data types. In fact, the integers used directly in Java are int (in the case of int and integer), and only if the data must appear as an object, the wrapper intege of int must encapsulate the integer value as an object. For Example: To add an integer to the vector in the Java.util package, you must encapsulate the integer value in an integer instance as follows:
vector v=new vector (); int k=121; v.addelemt (New Integer (k));
 in addition, integer as an int corresponding to the wrapper class, provides a number of methods, such as: Integer construction method, integer to the other various numeric types of conversion methods, etc., and these are not the type of int data. 2. Constants in Javawe need to be aware of the following types of constants. 2.1 Hexadecimal integer constantswhen in hexadecimal, you need to start with 0x or 0X, such as 0xff,0x9a.  2.2 Octal integer constantoctal must start with 0, such as 0123,034.  2.3 Long Integer typelong integers must end with L, such as 9l,342l.  2.4 Floating-point constantsbecause the default type of a decimal constant is double, you must add F (f) after the float type. The same variable with decimals defaults to the double type.
float F; f=1.3f;//must declare f.
 2.5 Character Constantscharacter constants are enclosed in two single quotes (note that string constants are enclosed in two double quotes). The characters in Java account for two bytes. Some of the commonly used escape characters. ①\r means accept keyboard input, equivalent to press ENTER;②\n indicates a line break;③\t represents a tab, equivalent to the table key;④\b means backspace key, equivalent to back space key;⑤\ ' denotes single quotation marks;⑥\ ' means double quotation marks;⑦\\ represents a slash \.  3. Conversions between simple data typesThere are two ways to convert a simple type of data: auto-convert and cast, usually in an expression or when a method's arguments are passed. 3.1 Automatic Conversionspecifically, when a more "small" data with a more "large" data, the system will automatically convert "small" data into "big" data, and then the operation. In the case of a method call, the actual parameter is "small", and the method called by the form parameter data is more "large" (if there is a match, of course, will call the matching method directly), the system will automatically convert "small" data into "big" data, and then the method calls, naturally, for multiple overloaded methods of the same name, will be converted to the most " Close 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 the amount of bytes, but the size of the range that represents the value. take a look at the following 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, converting to the advanced type (int) is converted to the corresponding ASCII value, for example
Char c= ' C '; int i=c; System.out.println ("Output:" +i);  
output: output:99; ③ for the Byte,short,char three types, they are lateral and therefore cannot be automatically converted to each other, and can use the following coercion type conversions.
Short i=99; Char c= (char) i; System.out.println ("Output:" +c);
output: output:c; ④ If there is a method in the polymorphism of the object:f (byte x) {...};f (short x) {...};f (int x) {...};f (long x) {...};f (float x) {...};f (double x) {...};also: Char y= ' a '; So, which method does the statement F (Y) call? The answer is: f (int x) {...} method because its formal parameters are "large" and "close" to the actual parameter.  and for the method:f (float x) {...};f (double x) {...};also: long y=123l; Then the method called by the statement F (Y) is F (float x) {...}.  3.2 Castswhen converting "large" data to "small" data, you can use forced type conversions. That is, you must use the following statement format:int n= (int) 3.14159/2;as you can imagine, this conversion can certainly lead to a drop in overflow or precision.  3.3 The data type of an expression is automatically promotedFor automatic elevation of types, note the following rules. ① all the values of the Byte,short,char type will be promoted to the int type;② If one operand is long, the result is a long type;③ If one of the operands is a float type, the result is a float type;④ If one of the operands is double, the result is a double type;example,
byte B; b=3; b= (Byte) (b*3);//must declare byte.
 3.4 Wrapper class transition type conversionIn General, we declare a variable first, and then generate a corresponding wrapper class, you can use the wrapper class of various methods for the type conversion. For example:① When you want to convert the float type to double type:
float f1=100.00f; float f1=new Float (F1); double D1=f1.doublevalue ();//f1.doublevalue () is a method of returning a double value type for the float class
 ② When you want to convert a double type to an int type:
double d1=100.00; double d1=new double (D1); int I1=d1.intvalue ();
 A simple type of variable is converted to the appropriate wrapper class, which can take advantage of the wrapper class's constructor. That is: Boolean (boolean value), Character (char value), Integer (int value), long (Long value), float (float value), double (double Value)and in each packing class, the total tangible is xxvalue () method, to obtain its corresponding simple type data. This method can also realize the conversion 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 types4.1 Conversion of other types to strings① The string conversion method of the Calling class: X.tostring ();② Automatic conversion: x+ "";③ method of using String: string.volueof (X); 4.2 strings as values to other types of conversionsThe ① is converted to the corresponding wrapper instance before the corresponding method is converted to another type .For example, the character "32.1" converts the value of a double type to the format: 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) methodThe API can be consulted specifically. 5. Conversion of date class to other data typesThere is no direct correspondence between the integer and date classes, except that you can use the int to represent the year, month, day, time, minute, and second, thus establishing a correspondence between the two, in which you can use the three forms of the Date class constructor:①date (int year, int month, int Date): type int for years, months, and days②date (int year, int month, int Date, int hrs, int min): int, month, day, time, minute③date (int year, int month, int Date, int hrs, int min, int sec): type int for years, months, days, hours, minutes, secondsThere is an interesting correspondence between the long and the date classes, which is to represent a time as a number of milliseconds from 0:0 GMT, January 1, 1970, 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 of the date class you can use the date class's 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 number of long integers that we said earlier, and as with the wrapper class, the date class has a ToString () method that can convert it to a string class. sometimes we want to get a specific format for date, for example 20020324, we can use the following methods, first introduced in the file,
import Java.text.SimpleDateFormat;import java.util.*;java.util.Date Date = new Java.util.Date (); //If you want to get the YYYYMMDD formatSimpleDateFormat sy1=new SimpleDateFormat ("YyyyMMDD");String Dateformat=sy1.format (date); //If you wish to get a separate year, month, daySimpleDateFormat 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);

Transferred from: http://zhangjunhd.blog.51cto.com/113473/17552

Conversion of Java data types

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.