01. StringBuffer和String的相互转换
-
String -> StringBuffer
- 通过构造方法
- 通过append()方法
-
StringBuffer -> String
- 通过构造方法
- 通过toString()方法
- 通过subString(0,length);
-
代码实现
package com.lhf.demo; /** * String -> StringBuffer * StringBuffer -> String * @author 海锋 * 2020年7月17日 */ public class Demo01_StringBuffer { public static void main(String[] args) { String str = "abc"; //通过构造方法 StringBuffer str1 = new StringBuffer(str); System.out.println(str1); //通过append()方法 StringBuffer str2 = new StringBuffer().append(str); System.out.println(str2); System.out.println("--------------"); StringBuffer sb = new StringBuffer("abc"); //通过toString()方法 String st = sb.toString(); System.out.println(st); //通过构造方法 String st1 = new String(sb); System.out.println(st1); //通过subString(0,length); String st2 = sb.substring(0); System.out.println(st2); } }
02. 字符串的面试题
-
String的面试题
package com.lhf.demo; /** * String的面试题 * @author 海锋 * 2020年7月17日 */ public class Demo02_String { public static void main(String[] args) { //A.判断定义为String类型的s1和s2是否相等 // String s1 ="abc"; // String s2 ="abc"; // System.out.println(s1 == s2);//true // System.out.println(s1.equals(s2));//true //B.下面这句话在内存中创建了几个对象? //C.判断定义为String类型的s1和s2是否相等 // String s1 = new String("abc"); // String s2 ="abc"; // System.out.println(s1 == s2);//false // System.out.println(s1.equals(s2));//true // //D.判断定义为String类型的s1和s2是否相等 // String s1 ="a" + "b" + "c"; // String s2 ="abc"; // System.out.println(s1 == s2);//true // System.out.println(s1.equals(s2));//true // //E.判断定义为String类型的s1和s2是否相等 String s1 ="ab"; String s2 ="abc"; String s3 = s1 + "c"; System.out.println(s3 == s2);//false System.out.println(s3.equals(s2));//true } }
03. String优化
-
在jdk1.0时,使用"+"拼接时,会创建新的String对象,拼接多少次就会创建多少对象,非常耗内存
-
在jdk1.5开始,使用"+"拼接对String进行了优化
-
代码实现
//情况一 String str1 = "a"; String str2 = str1 + "b" + "c" + "d"; //情况二 String str3 = "a"; String str4 = str3 + "b"; str4 = str4 + "c"; str4 = str4 + "d";
- 以上代码,在jdk1.0,都会创建四个对象
- 在jdk1.5,情况一:通过StringBuilder来拼接字符串提高效率;情况二:会创建多个StringBuilder对象,效率提高不明显。
04. StringBuilder概述
- 概述
- 一个可变的字符序列
- String、StringBuffer、StringBuilder的区别
- String是不可变的序列
- StringBuffer、StringBuilder是可变的字符序列
- 执行效率从高到低:StringBuilder -> StringBuffer -> String
05. 基本类型包装类
-
概述
-
基本类型数据不是对象,无法调用成员
-
使用包装类后,就可以调用成员
-
基本数据类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
double | Double |
float | Float |
char | Character |
boolean | Boolean |
- 除了int是Integer和char是Character以外的基本数据类型的包装类都是首字母大写的基本数据类型
06. Integer类的概述
- 概述
- Integer类在对象中包装了一个基本类型int的值
- 能在int类型和String类型之间互相转换
- 提供了处理int类型时非常有用的其他一些常量和方法
07. String和int类型的相互转换
-
String --> int
- 和""进行拼接
- public static String valueOf(int i):String类的静态方法
- public static String toString(int i):Integer类的静态方法
-
int --> String
- public static int parseInt(String s):Integer类的静态方法
- Integer(String numStr):Integer类的构造方法
-
代码实现
package com.lhf.demo; /** * int --> String * String --> int * @author 海锋 * 2020年7月17日 */ public class Demo04_Integer { public static void main(String[] args) { // 和""进行拼接 //int i = 3, j = 4; String str1 = "1" + "2"; // public static String valueOf(int i):String类的静态方法 System.out.println(str1); String str2 = String.valueOf(3); System.out.println(str2); // public static String toString(int i):Integer类的静态方法 String str3 = Integer.toString(4); System.out.println(str3); // public static int parseInt(String s):Integer类的静态方法 int str4 = Integer.parseInt(str3); System.out.println(str4); // Integer(String numStr):Integer类的构造方法 Integer in = new Integer("10"); System.out.println(in); } }
08. 装箱和拆箱
-
装箱和拆箱
- 装箱:将基本数据类型转换成包装类
- 拆箱:将包装类转换成基本数据类型
-
手动装箱和手动拆箱
- 手动装箱:开发人员调用方法将基本数据类型转换成包装类
- 手动拆箱:开发人员调用方法将包装类转换成基本数据类型
-
自动装箱和自动拆箱
- 概述:java5.0的新特性
- 自动装箱:系统自动将基本数据类型转换为包装类
- 自动拆箱:系统自动将包装类转换成基本数据类型
- 总结:不需要再调用方法便可以让包装类和基本数据类型实现相互转换
-
代码实现
//自动装箱 Integer num = 100; //手动拆箱 int intValue = num.intValue(); int sum1 = 1 + intValue; System.out.println(sum1); System.out.println("--------------"); //自动拆箱 int sum2 = 1 + num; System.out.println(sum2);
09. Integer面试题
-
代码实现
** * Integer面试题:装箱和拆箱 * @author 海锋 * 2020年7月17日 */ public class Demo05_Integer { public static void main(String[] args) { //看程序写结果 Integer i1 = new Integer(97);//手动装箱 Integer i2 = new Integer(97); System.out.println(i1 == i2);//false System.out.println(i1.equals(i2));//true System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); Integer i3 = new Integer(197); Integer i4 = new Integer(197); System.out.println(i3 == i4);//false System.out.println(i3.equals(i4));//true System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); Integer i5 = 97; Integer i6 = 97; System.out.println(i5 == i6);//true System.out.println(i5.equals(i6));//true System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); Integer i7 = 197; Integer i8 = 197; System.out.println(i7 == i8);//false System.out.println(i7.equals(i8));//true } }
10. Math类概述
-
代码实现
/** * Math类 * @author 海锋 * 2020年7月17日 */ public class Demo06_Math { public static void main(String[] args) { // public static int abs(int a)//求绝对值 System.out.println(Math.abs(-1.3)); // public static double ceil(double a)//向上取整 System.out.println(Math.ceil(2.3)); // public static double floor(double a)//向下取整 System.out.println(Math.floor(54.3)); // public static int max(int a,int b) min自学 System.out.println(Math.min(4, 5)); // public static double pow(double a,double b)//求a的b次幂 System.out.println(Math.pow(2, 4)); // public static double random()://随机产生大于0.1小于1.0的double小数 System.out.println(Math.random()); // public static int round(float a)//四舍五入 System.out.println(Math.round(5.34)); // public static double sqrt(double a)//开方 System.out.println(Math.sqrt(8)); } }