Home >Java >javaTutorial >Conversion between primitive types and string in Java

Conversion between primitive types and string in Java

黄舟
黄舟Original
2017-01-18 15:38:501442browse

1. Basic type----->String

1. toString() method of packaging class; such as: Short.toString(c);

2.String The valueOf() method of the class; such as:

2. String----->Basic type

1. The parseInt(str) method of the packaging class; such as: Integer.parseInt (str);

2. Valueof(str) method of packaging class; such as: Inteer.valueOf(str)

3. Code demonstration:

Test class: Test Code:

package com.bluesky;  
  
public class Test {  
  
    public static void main(String[] args) {  
          
        String str = "1314520";  
          
        int love = 520;  
          
        //基本类型转换字符串  
        String str1=Integer.toString(love); //利用包装类的toString()方法;  
        String str2=String.valueOf(love);   //利用String类的valueOf()方法;  
        System.out.println("str1="+str1);  
        System.out.println("str2="+str2);  
          
        //字符串转换基本类型  
        int love1=Integer.parseInt(str);   //利用包装类的parseInt()方法;  
        int love2=Integer.valueOf(str);    //利用包装类的valueOf()方法;  
        System.out.println("love1="+love1);  
        System.out.println("love2="+love2);  
          
                  
  
    }  
  
}

The above is the content of conversion between basic types and strings in Java. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn