Mutual conversions between various types in Java

Source: Internet
Author: User

1. Conversion of data types between shaping and character type:

  There are two ways to convert an int into char:

① is a Unicode encoding using char

Example: int num1 = 8; 

Char ch1 = (char) (NUM1 + 48); 

 two. Char converted to int type  

  

(char is the base data type, character is its wrapper type.) )  

    int num2 = Integer.parseint (Ch2.tostring ()); /span>

② is a Unicode encoding using char  

  Example: char CH3 = ' 8 ';

int num3 = ch3-48;

2. Conversion between the base data type and the string:

convert an int to string 

①. converting integers to character type

String num = integer.tostring (int n);

②.long type conversion to character type  

String num = long.tostring (Long n); 

③.short type conversion to character type  

String num = short.tostring (short n);

④.float type conversion to character type

String num = float.tostring (Float n);

⑤.double type conversion to character type  

String num = Double.tostring (Double N);

two. String converted to int

①. Convert to int

Int/integer num  =  Integer.parseInt ( String str); /span>

②. Convert to Long

Long/long num    =  Long.parselong (String str); /span>

③. Convert to Short type

Short/short num  =  Short.parseShort ( String str);

④. Convert to float type

Float/float num  =  float.parsefloat (String str); /span>

⑤. Convert to type double

Double/double num = double.parsedouble (String str);

Mutual conversions between various types in Java

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.