字符串是什么意思 字符怎么输入


字符串是什么意思 字符怎么输入


9.1 装箱拆箱封装类所有的基本类型,都有对应的类类型 。比如int对应的类是Integer,这种类就叫做封装类 。
package digit;public class TestNumber {public static void main(String[] args) {int i = 5;//把一个基本类型的变量 , 转换为Integer类型Integer it = new Integer(i);//把一个Integer对象,转换为一个基本类型的intint i2 = it.intValue();}}Number类数字封装类有Byte,Short,Integer,Long,Float,Double 。这些类都是抽象类Number的子类 。

自动装箱与拆箱不需要调用构造方法,通过**=**符号自动把 基本类型 转换为 类类型 就叫装箱
不需要调用Integer的intValue方法 , 通过**=就自动转换成int类型**,就叫拆箱
package digit; public class TestNumber {public static void main(String[] args) {int i = 5;//基本类型转换成封装类型Integer it = new Integer(i);//自动转换就叫装箱Integer it2 = i;//封装类型转换成基本类型int i2 = it.intValue();//自动转换就叫拆箱int i3 = it;}}
int的最大值和最小值int的最大值可以通过其对应的封装类Integer.MAX_VALUE获取
package digit;public class TestNumber {public static void main(String[] args) {//int的最大值System.out.println(Integer.MAX_VALUE);//int的最小值System.out.println(Integer.MIN_VALUE);}
9.2字符串转换
数字转成字符串【字符串是什么意思 字符怎么输入】方法1: 使用String类的静态方法valueOf 方法2: 先把基本类型装箱为对象,然后调用对象的toString
package digit;public class TestNumber {public static void main(String[] args) {int i = 5;//方法1String str = String.valueOf(i);//方法2Integer it = i;String str2 = it.toString();}}
字符串转成数字调用Integer的静态方法parseInt
package digit;public class TestNumber {public static void main(String[] args) {String str = "999";int i= Integer.parseInt(str);System.out.println(i);}}
练习把浮点数 3.14 转换为 字符串 "3.14" 再把字符串 “3.14” 转换为 浮点数 3.14
如果字符串是 3.1a4,转换为浮点数会得到什么?
public class TestNumber {public static void main(String[] args) {double i = 3.14;String str = String.valueOf(i);System.out.println("3.14转换成的字符串 " + str);double i2 = Float.parseFloat(str);System.out.println("\"3.14\"转换成的浮点数 " + i2);String str2 = "3.1a4";double i3 = Float.parseFloat(str2);System.out.println("\"3.1s4\"转换成的浮点数 " + i3);}}

9.3数学方法
四舍五入, 随机数,开方,次方,π,自然常数public class TestNumber {public static void main(String[] args) {float f1 = 5.4f;float f2 = 5.5f;//5.4四舍五入即5System.out.println(Math.round(f1));//5.5四舍五入即6System.out.println(Math.round(f2));//得到一个0-1之间的随机浮点数(取不到1)System.out.println(Math.random());//得到一个0-10之间的随机整数 (取不到10)System.out.println((int)( Math.random()*10));//开方System.out.println(Math.sqrt(9));//次方(2的4次方)System.out.println(Math.pow(2,4));//πSystem.out.println(Math.PI);//自然常数System.out.println(Math.E);}}
练习-数学方法

public class TestNumber {public static void main(String[] args) {int n = Integer.MAX_VALUE;double value = http://www.fzline.cn//sh/Math.pow((1 + 1/(double)n),n);System.out.println("n 取最大整数时value: " + value);double error = Math.abs(Math.E - value);System.out.println("error = " + error);}}

推荐阅读