基本数据类型包括byte、short、int、long、boolean、char、float、double。
String转换为基本数据类型有两种方法,分别是:
比如:Integer.parseInt(s)
String s = "123";
System.out.println(Integer.parseInt(s)); //123
System.out.println(Double.parseDouble(s)); //123.0
System.out.println(Double.valueOf(s)); //123.0
基本数据类型转换为字符串也有两种方法,分别是:
int num1 = 123;
double num2 = 1.23;
System.out.println(String.valueOf(num1)); //123
System.out.println(String.valueOf(num2)); //1.23
int num = 123;
double num2 = 1.23;
System.out.println(Integer.toString(num)); //123
System.out.println(Double.toString(num2)); //1.23
1)一个字符串可以看作是由多个char组成的,String是一个对象,也就是说,String是char的有序合集。
2)String有两种创建方式,比较字符串内容时使用.equals()方法,不能使用==,==比较的是字符串的地址是否相同。
3)使用.charAt(index)方法可以获取String字符串中下标为index位置的char值。
本文固定URL:https://www.dotcpp.com/qa/57