How to implement conversion between basic data types and characters in Java

Source: Internet
Author: User

  Absrtact: In our study of Java, the conversion between data types is common to us, we also know that the basic data types have automatic conversion and casting, in the int, short, long, float, double conversion between we are relatively clear, But we should also know what type conversions are between them and the string and character types, and let's explore their transformations together:

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 +);
two. Char converted to int type   
① parseint method for using int
Example: Character CH2 = ' 8 '; (char is the base data type, Character is its wrapper type.) )
int num2 = Integer.parseint (ch2.tostring ());
② 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. Convert String to int  
①. Converting to int type
Int/integer num = integer.parseint (String str);
②. Converting to Long type
Long/long num = long.parselong (String str);
③. Converting to Short type
short/short num = short.parseshort (String str);
④. Convert to float type
float/float num = float.parsefloat (String str);
⑤. Converting to double type
Double/double num = double.parsedouble (String str);

How to implement conversion between basic data types and characters 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.