1 基本类型包装类
基本类型包装类的作用
将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据
常用的操作之一:用于基本数据类型与字符串之间的转换
基本类型对应的包装类
基本数据类型 包装类 byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean
2.Integer类
Integer类概述
包装一个对象中的原始类型 int 的值
Integer类构造方法
方法名 说明 public Integer(int value) 根据 int 值创建 Integer 对象(过时) public Integer(String s) 根据 String 值创建 Integer 对象(过时) public static Integer valueOf(int i) 返回表示指定的 int 值的 Integer 实例 public static Integer valueOf(String s) 返回一个保存指定值的 Integer 对象 String public class IntegerDemo { public static void main(String[] args) { //public Integer(int value):根据 int 值创建 Integer 对象(过时) Integer i1 = new Integer(100); System.out.println(i1); //public Integer(String s):根据 String 值创建 Integer 对象(过时) Integer i2 = new Integer("100"); // Integer i2 = new Integer("abc"); //NumberFormatException System.out.println(i2); System.out.println("--------"); //public static Integer valueOf(int i):返回表示指定的 int 值的 Integer 实例 Integer i3 = Integer.valueOf(100); System.out.println(i3); //public static Integer valueOf(String s):返回一个保存指定值的Integer对象 String Integer i4 = Integer.valueOf("100"); System.out.println(i4); } }
3.自动拆箱和自动装箱
自动装箱
把基本数据类型转换为对应的包装类类型
自动拆箱
把包装类类型转换为对应的基本数据类型
示例代码
Integer i = 100; // 自动装箱 i += 200; // i = i + 200; i + 200 自动拆箱;i = i + 200; 是自动装箱
3.1int和String类型的相互转换
int转换为String
转换方式
方式一:直接在数字后加一个空字符串
方式二:通过String类静态方法valueOf()
示例代码
public class IntegerDemo { public static void main(String[] args) { //int --- String int number = 100; //方式1 String s1 = number + ""; System.out.println(s1); //方式2 //public static String valueOf(int i) String s2 = String.valueOf(number); System.out.println(s2); System.out.println("--------"); } }
3.2 String转换为int
转换方式
方式一:先将字符串数字转成Integer,再调用valueOf()方法
方式二:通过Integer静态方法parseInt()进行转换
示例代码
public class IntegerDemo { public static void main(String[] args) { //String --- int String s = "100"; //方式1:String --- Integer --- int Integer i = Integer.valueOf(s); //public int intValue() int x = i.intValue(); System.out.println(x); //方式2 //public static int parseInt(String s) int y = Integer.parseInt(s); System.out.println(y); } }
字符串数据排序案例:
public class AnniIneteger {
public static void main(String[] args) {
String s ="69 55 66 22 22";
String[] g = s.split(" ");
int [] nub=new int[g.length];
for (int i = 0; i < g.length; i++) {
int nubs=Integer.parseInt(g[i]);
nub[i]=nubs;
}
for (int i = 0; i < nub.length; i++) {
System.out.println(nub[i]);
}
}
}
递归
递归的介绍
以编程的角度来看,递归指的是方法定义中调用方法本身的现象
把一个复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解
递归策略只需少量的程序就可描述出解题过程所需要的多次重复计算
递归的注意事项
递归一定要有出口。否则内存溢出
递归虽然有出口,但是递归的次数也不宜过多。否则内存溢出
案例:
public class MyFactorialDemo2 { public static void main(String[] args) { int sum = getSum(100); System.out.println(sum); } private static int getSum(int i) { //1- 100之间的和 //100 + (1-99之间的和) // 99 + (1- 98之间的和) //.... //1 //方法的作用: 求 1- i 之间和 if(i == 1){ return 1; }else{ return i + getSum(i -1); } } }
public class DiGuiDemo01 { public static void main(String[] args) { //调用方法 int result = jc(5); //输出结果 System.out.println("5的阶乘是:" + result); } //定义一个方法,用于递归求阶乘,参数为一个int类型的变量 public static int jc(int n) { //在方法内部判断该变量的值是否是1 if(n == 1) { //是:返回1 return 1; } else { //不是:返回n*(n-1)! return n*jc(n-1); } } }