Java study notes-type conversion

I. Introduction

In the process of writing code recently, I found that it is necessary to convert List<Long> to List<String>. When I first wrote, the idea was to traverse the list directly, convert the value, and then plug the value. On the Internet, Baidu, everyone’s approach is the same. child. So I thought, it's better to sort out the various type conversions in java for later use.

 

2. Type conversion

1. Long is converted to String
String.valueOf();

2, Integer to String

//方法一:Integer类的静态方法toString()

Integer a = 2;

String str = Integer.toString(a)


//方法二:Integer类的成员方法toString()

Integer a = 2;

String str = a.toString();


//方法三:String类的静态方法valueOf()

Integer a = 2;

String str = String.valueOf(a);

 

3.String to Integer

String str = "...";
Integer i = null;
if(str!=null){
     i = Integer.valueOf(str);
}

 

4. String to list

 String str="小周,小艾,小贝";
 List<String> strings=Arrays.asList(str.split(","));
 System.out.println(strings);

 

5.Char array or char into String

String s = String.valueOf('c'); //效率最高的方法

String s = String.valueOf(new char[]{'c'}); //将一个char数组转换成String

 

Guess you like

Origin blog.csdn.net/mumuwang1234/article/details/109672866
Recommended