基本数据类型的包装类
- byte Byte
- short Short
- int Integer
- long Long
- float Float
- double Double
- char Character
- boolean Boolean
基本数据类型 声明变量 只有一个功能保存数据
- 基本数据类型转化包装类 增加了成员方法 和 成员变量
- Integer(int的包装类)类中 有进制转化的方法
1.//创建一个Integer对象
Integer i = new Integer(10);
System.out.println(i);
2.//将字符串转化成Integer对象
Integer i1 = new Integer(“123”);
System.out.println(i1);
3.//将Integer类型转化为 int类型
int num = i1.intValue();
System.out.println(num);
4.//将int类型 转化为 Integer类型
Integer i2 = Integer.valueOf(i1);
System.out.println(i2);
5.//声明一个Integer对象
//自动装箱 和 自动拆箱
//自动装箱 系统帮调用 valueOf
Integer n = 10;
System.out.println(n);
//自动拆箱 系统帮调用 intValue
int r = n + 15;
System.out.println(r);
字符串
- String 线程不安全 不可变字符串
- StringBuffer 可变字符串 线程安全的(耗费系统资源)
- StringBuilder 可变字符串 线程不安全
StringBuffer
1.//替换
StringBuffer sb = new StringBuffer();
sb.append(“gorilla”);
sb.replace(4,6, “xx”);
System.out.println(sb);
2.//字符数组转stringBuffer
char[] array = {‘w’,’o’,’s’,’s’,’a’};
String s = new String(array);
StringBuilder sb1 = new StringBuilder(s);
System.out.println(sb1);
3.//反转 键盘输入一个字符串 将字符串反转
System.out.println(“输入字符串”);
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
StringBuffer sb = new StringBuffer(str);
sb.reverse();//反转
System.out.println(sb);
4.
StringBuffer sb = new StringBuffer();
sb.append(“gorilla”);
//留头不留尾
//如果结束坐标超出字符串长度 相当于 清空字符串
sb.delete(4, 6);
System.out.println(sb);
//删除索引处的字符
sb.deleteCharAt(4);
System.out.println(sb);
//获取字符
char c = sb.charAt(1);
System.out.println(c);
5.//插入字符
StringBuffer sb = new StringBuffer();
sb.append(“gorilla”);
sb.insert(0, ‘x’);
System.out.println(sb);
//修改字符
sb.setCharAt(5, ‘x’);
System.out.println(sb);
6.//创建StringBuffer对象
StringBuffer sb = new StringBuffer();
//获取初始容量 默认16
System.out.println(sb.capacity());
//拼接(核心方法)
sb.append(“gorilla”).append(“321”);
System.out.println(sb.append(“123”));
//转化成字符串
String str = sb.toString();
Collection类
// 获取集合的长度
Collection collection = new ArrayList<>();
collection.add(“a”);
collection.add(“b”);
collection.add(“c”);
collection.add(“d”);
System.out.println(collection.size());// 判断是否包含某个元素
boolean b1 = collection.contains(“c”);
System.out.println(b1);// 从集合中删除一个元素
boolean b2 = collection.remove(“d”);
System.out.println(b2);4.// 判断集合 是否为空
boolean b3 = collection.isEmpty();
System.out.println(b3);