Java基本類型和String類型相互轉換

1.基本類型轉String類型:將基本類型的值加上雙引號" "便可html

public class stringToBasic {
  public static void main(String[] args) {
    int n1 = 100;
    float f1 = 1.1F;
    double d1 = 1.5;
    boolean b1 = true;
    String s1 = n1 + "";
    String s2 = f1 + "";
    String s3 = d1 + "";
    String s4 = b1 + "";
    System.out.println(s1); 
    System.out.println(s2);
    System.out.println(s3);
    System.out.println(s4);
  }
}

2.String類型轉基本類型:經過基本類型的包調用Parse**便可ide

public class stringToBasic {
  public static void main(String[] args) {
    String s1 = "123";
    int num1 = Integer.parseInt(s1);
    System.out.println(num1); // 123
    double d1 = Double.parseDouble(s1);
    System.out.println(d1);
    float f1 = Float.parseFloat(s1);
    System.out.println(f1);
    Long l1 = Long.parseLong(s1);
    System.out.println(l1);
    Byte b1 = Byte.parseByte(s1);
    System.out.println(b1);
    Short sr1 = Short.parseShort(s1);
    System.out.println(sr1);
    Boolean bl1 = Boolean.parseBoolean(s1);
    System.out.println(bl1);
  }
}
相關文章
相關標籤/搜索