The article directories
- 1. Basic data type and String type conversion
-
- 1.1 Introduction and Usage
- 1.2 Precautions
1. Basic data type and String type conversion
1.1 Introduction and Usage
- Case: StringToBasic. Java
// Basic data type ->String
int n1 = 100;
float f1 = 1.1 F;
double d1 = 4.5;
boolean b1 = true;
String s1 = n1 + "";
String s2 = f1 + "";
String s3 = d1 + "";
String s4 = b1 + "";
System.out.println(s1 + "" + s2 + "" + s3 + "" + s4);
//String-> The corresponding basic data type
String s5 = "123";
// We'll go into more detail when OOP talks about objects and methods
// Read the corresponding method of the wrapper class that uses the basic datatype to get the basic datatype
int num1 = Integer.parseInt(s5);
double num2 = Double.parseDouble(s5);
float num3 = Float.parseFloat(s5);
long num4 = Long.parseLong(s5);
byte num5 = Byte.parseByte(s5);
boolean b = Boolean.parseBoolean("true");
short num6 = Short.parseShort(s5);
System.out.println("= = = = = = = = = = = = = = = = = = =");
System.out.println(num1);/ / 123
System.out.println(num2);/ / 123.0
System.out.println(num3);/ / 123.0
System.out.println(num4);/ / 123
System.out.println(num5);/ / 123
System.out.println(num6);/ / 123
System.out.println(b);//true
// How to convert a string to a character char -
// read s5.charAt(0) to get the first character of s5 string '1'
System.out.println(s5.charAt(0));
Copy the code
1.2 Precautions
- Case: StringToBasicDetail. Java
String str = "hello";
/ / to int
int n1 = Integer.parseInt(str);
System.out.println(n1);
Copy the code