Java Data type conversions

Source: Internet
Author: User

The conversion of AVA data types is generally divided into three types, namely:
(1). conversions between simple data types

In java, integer, real, and character types are treated as simple data type, from low to advanced (byte,short,char)--int--long--float--double


Conversions between simple data types can also be categorized as:
low-to-advanced Automatic type conversion
Advanced to low-level coercion type conversions
Wrapper class transition types can be converted

Low-level variables can be directly converted to advanced variables, which I call automatic type conversion, for example, The following statement can be directly in Java through:
BYTE b;
int i=b;
Long l=b;
Float f=b;
Double d=b;

If the low-level type is char, conversion to the advanced type (int) 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 lateral and therefore cannot be automatically converted to each other, and can be cast using the following coercion Type.
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 integer, so if you manipulate integer data, it is best to use the int type uniformly.

When you convert an advanced variable to a low-level variable, the situation becomes more complex, and you can use forced 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 you can imagine, this conversion is certainly likely to result in a drop in overflow or precision, so the author does not recommend using this Conversion.

1, float type to double type:

Float f1=100.00f;
Float f1=new Float (f1);

F1.doublevalue () is a method of returning a double value type for the float class
Double D1=f1.doublevalue ();

2, double type conversion to int type:

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

3, type int to double type:

int i1=200;
Double d1=i1;

(2). conversion of strings to other data types

4. Conversion between strings and other types:

⑴ 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);

⑵ a string as a value, to another type of conversion
The ① 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) method
The API can be consulted specifically.

(3). other useful data type conversions

5. Conversion of date class to other data types
There 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, seconds


There 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 format
SimpleDateFormat sy1=new SimpleDateFormat ("yyyyMMDD");
String Dateformat=sy1.format (date);

If you want to get a separate 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);

JS converts data into string type

* The extension of date to convert date to string     in the specified format;
* months (m), days (d), 12 hours (h), 24 hours (h), minutes (m), seconds (s), weeks (E), quarter (q) can be used 1-2 Placeholder     
* Year (y) can use 1-4 placeholders, milliseconds (S) only 1 placeholders (1-3 digits)     
* eg:     
* (new Date ()). pattern ("yyyy-mm-dd hh:mm:ss. S ") ==> 2006-07-02 08:09:04.423    
* (new Date ()). pattern (" yyyy-mm-dd E HH:mm:ss ") ==> 2009-03-10 20:09:04    
* (new Date ()). pattern ("yyyy-mm-dd EE Hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04    
* (new Date ()). pattern ("yyyy-mm-dd EEE Hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04& nbsp;   
* (new Date ()). pattern ("yyyy-m-d H:m:s.s") ==> 2006-7-2 8:9:4.
 

6. Character type turn time type:(2005-10-1)
Reportdate_str = "2005-10-01";
Reportdate_str = Reportdate_str + "00:00:00.0"
Reportdate = Java.sql.Timestamp.valueOf (reportdate_str);

7. Time-type Turn Character type:
V_date = reportdate.tostring ();

8. Converting a long type to a string type
Long app_unit = (long) userview.getdept_id ();
String s_app_unit = string.valeof (app_unit);
The basic type S can be converted to string type by string.valeof (s).

Java Data type conversions

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.