Conversion of Java Basic Types

Source: Internet
Author: User

/** <Br/> * <p> typechange. java <br/> * <p> mutual conversion of Basic Java types <br/> * @ author heardy <br/> * @ time 2011/01/30 <br/> * @ version 1.0 <br/> */<br/> public class typechange {</P> <p>/** <br/> * convert it to a string <br/> * <br/> * @ Param OBJ <br/> * @ return returns an empty string when null is returned <br/> */<br/> Public static string objecttostring (Object OBJ) {<br/> string STR = ""; <br/> try {<br/> STR = (string) OBJ; <br/> If (STR = NULL) {<br/> STR = ""; <br/>}< br/>} catch (classcastexception CE) {<br/> try {<br/> STR = string. valueof (OBJ); <br/>} catch (exception e) {<br/> STR = ""; <br/>}< br/> return Str. trim (); <br/>}</P> <p>/** <br/> * convert null to a null character <br/> * @ Param STR <br /> * @ return <br/> */<br/> Public static string nullofstring (Object OBJ) {<br/> string STR = objecttostring (OBJ); <br/> return STR; <br/>}</P> <p>/** <br/> * convert null to int <br/> * @ Param OBJ <br/> * @ return <br/> */<br/> Public static int nullofint (Object OBJ) {<br/> return objecttoint (OBJ ); <br/>}</P> <p> // ******************* convert object to another type ***** * *********** </P> <p>/*** <br/> * convert bytes <br/> *@ param STR <br/> * @ return <br/> * @ throws if conversion fails <br/> * throws numberformatexception <br/> */<br/> Public static byte objecttobyte (Object OBJ) {<br/> string STR = objecttostring (OBJ); <br/> STR = nullofstring (STR); <br/> try {<br/> return byte. parsebyte (STR); <br/>} catch (numberformatexception e) {<br/> system. out. println ("Conversion error" + E); <br/> throw E; <br/>}</P> <p>/** <br/> * Boolean conversion <br/> * @ Param Str <br/> * @ return is 1 or "true" returns true or false <br/> */<br/> Public static Boolean objecttoboolean (Object OBJ) {<br/> string STR = objecttostring (OBJ); <br/> If (Str. equals ("1") {<br/> return true; <br/>} else if (Str. equals ("0") {<br/> return false; <br/>}else {<br/> try {<br/> return Boolean. parseboolean (STR); <br/>} catch (exception e) {<br/> return false; <br/>}</P> <p>/** <br/> * convert int <br/> * <br/> * @ Param STR <br/> * @ return corresponding value or zero <br/> */<br/> Public static int objecttoint (Object OBJ) {<br/> string STR = objecttostring (OBJ); <br/> int I = 0; <br/> try {<br/> I = integer. parseint (Str. trim (); <br/>} catch (numberformatexception e) {<br/> I = 0; <br/>}< br/> return I; <br/>}</P> <p>/** <br/> * convert to short <br/> * @ Param OBJ <br/> * @ return corresponding value or zero <br/> */<br/> Public static short objecttoshort (Object OBJ) {<br/> string STR = objecttostring (OBJ); <br/> short I = 0; <br/> try {<br/> I = short. parseshort (Str. trim (); <br/>} catch (numberformatexception e) {<br/> I = 0; <br/>}< br/> return I; <br/>}</P> <p>/** <br/> * convert to double <br/> * @ Param OBJ <br/> * @ return corresponding value or zero <br/> */<br/> Public static double objecttodouble (Object OBJ) {<br/> string STR = objecttostring (OBJ); <br/> double I = 0; <br/> try {<br/> I = double. parsedouble (Str. trim (); <br/>} catch (numberformatexception e) {<br/> I = 0; <br/>}< br/> return I; <br/>}</P> <p>/** <br/> * convert to long <br/> * @ Param OBJ <br/> * @ return corresponding value or zero <br/> */<br/> Public static long objecttolong (Object OBJ) {<br/> string STR = objecttostring (OBJ); <br/> Long li = new long (0); <br/> try {<br/> li = long. valueof (STR); <br/>}catch (numberformatexception e) {<br/>}< br/> return Li. longvalue (); <br/>}</P> <p> // ******************* conversion of other types ***** * *********** </P> <p>/**** <br/> * convert double to long <br/> * @ Param d <br/> * @ return: only the preceding integer is truncated. <br/> */<br/> Public static long doubletolong (double D) {<br/> long l = 0; <br/> try {<br/> // data of the double type after decimal point must be filtered out before the double type is converted to long <br/> L = long. parselong (string. valueof (d ). substring (0, <br/> string. valueof (d ). lastindexof (". "); <br/>}catch (exception e) {<br/> L = 0; <br/>}< br/> return l; <br/>}</P> <p>/** <br/> * convert double to int <br/> * @ Param d <br/> * @ return: only the preceding integer is truncated. <br/> */<br/> Public static int doubletoint (double D) {<br/> int I = 0; <br/> try {<br/> // data of the double type after decimal point must be filtered out before the double type is converted to int <br/> I = integer. parseint (string. valueof (d ). substring (0, <br/> string. valueof (d ). lastindexof (". "); <br/>}catch (exception e) {<br/> I = 0; <br/>}< br/> return I; <br/>}</P> <p>/** <br/> * convert double to long (rounding) <br/> * @ Param d <br/> * @ return returns only the preceding INTEGER (rounding) <br/> */<br/> Public static long doubletolongwhithround (double D) {<br/> long l = 0; <br/> try {<br/> L = math. round (d); <br/>}catch (exception e) {<br/> L = 0; <br/>}< br/> return l; <br/>}</P> <p>/** <br/> * convert double to int (rounding) <br/> * @ Param d <br/> * @ return returns only the preceding INTEGER (rounding) <br/> */<br/> Public static int doubletointwhithround (double D) {<br/> int I = 0; <br/> try {<br/> I = (INT) math. round (d); <br/>}catch (exception e) {<br/> I = 0; <br/>}< br/> return I; <br/>}</P> <p> Public static double longtodouble (long d) {<br/> double L = 0; <br/> try {<br/> L = double. parsedouble (string. valueof (d); <br/>}catch (exception e) {<br/> L = 0; <br/>}< br/> return l; <br/>}</P> <p> Public static int longtoint (long d) {<br/> int L = 0; <br/> try {<br/> L = integer. parseint (string. valueof (d); <br/>}catch (exception e) {<br/> L = 0; <br/>}< br/> return l; <br/>}< br/>}

 

Converts the basic types...

 

Please point out any shortcomings!

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.