包装类 和 String类
包装类
Object类是所有类的父类
8种基本数据类型
jdk5之前:
Object o = 12;//在jdk5之前不行
jdk5之后包装类将基本数据类型转换为引用数据类型
包装类的分类
基本数据类型 | 包装类类型 |
---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
char | Character |
float | Float |
double | Double |
boolean | Boolean |
Integer包装类
案例:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
Integer integer = new Integer(10);
System.out.println(integer);
System.out.println(integer.toString());
Integer integer2 = Integer.valueOf(20);
System.out.println(integer2);
Integer integer3 = new Integer(250);
int a = integer3.intValue();
System.out.println(a);
String string = "123456";
Integer integer4 = new Integer(string);
System.out.println(integer4);
String string2 = "34521";
Integer b = Integer.valueOf(string2);
System.out.println(b);
int c = 234;
String string3 = c+"";
System.out.println(string3);
String string4 = "345678";
int d = Integer.parseInt(string4);
System.out.println(d);
String string5 = "0234543";
int e = Integer.parseInt(string5);
System.out.println(e);
String string6 = "23 45 67";
int f = Integer.parseInt(string6);
}
}
自动装箱拆箱
jdk5之后提供了自动拆箱和自动装箱的功能
自动装箱:将简单的数据类型直接赋值给包装类型
自动拆箱:将包装类型直接赋值给简单类型
案例:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
Integer i = new Integer(20);
int a = i;
Integer integer = 30;
}
}
常量池
企业面试题:
常量池:存在-128----127的常量,这些值是所有对象共享的 当值超过127 就会创建新的对象
案例:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
Integer integer1 = 120;
Integer integer2 = 120;
System.out.println(integer1==integer2);
Integer integer3 = 150;
Integer integer4 = 150;
System.out.println(integer3==integer4);
}
}
包装类的应用
String类
对象创建的方式
字符串池:在java中有字符串池,字符串的字面值直接放入串池中
第一种:
String string1 = "hello";
String string2 = "hello";
System.out.println(string1==string2);
第二种:
String string = new String("hello");
这行代码创建了两个对象
第一个对象:hello 放入到串池中
第二个对象:new 的时候在堆中创建了一个字符串对象 值为hello
注意:串池和堆是两个不同的内存空间
字符串拼接
String string1 = "hello";
String string2 = "hello";
System.out.println(string1==string2);
string1 = string1+"world";
System.out.println(string1==string2);
String类的对象如果拼接之后其地址是改变的 但是串池中的字符串是不变的
可变字符串:
StringBuffer : 是线程安全的 效率低
StringBuilder:线程不安全的 效率高 常用的
案例:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
String string1 = "hello";
String string2 = "hello";
System.out.println(string1==string2);
string1 = string1+"world";
System.out.println(string1==string2);
StringBuilder builder = new StringBuilder("hello");
StringBuilder builder2 = new StringBuilder("hello");
System.out.println(builder==builder2);
System.out.println(builder.hashCode());
System.out.println(builder2.hashCode());
builder.append("world");
System.out.println(builder.hashCode());
}
}
String类中的方法
1.charAt(int index):取出字符串中的每一个字符
index:索引 从0开始
2.length() :获取字符串的长度
3.contains(子串):用于判断子串是否在字符串中出现 存在true 不存在false
4.equals():判断两个字符串的内容是否相同
5.endsWith(String suffix)和 startsWith(String prefix) 测试此字符串是否以指定的后缀结束。
startsWith(String prefix, int toffset)测试此字符串从指定索引开始的子字符串是否以指定前缀开始。
案例:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
String string = "aglasdg";
System.out.println(string.startsWith("g",1));
System.out.println(string.endsWith("g"));
}
}
==================================================================
6..getBytes():将字符串转换为一个字节数组
案例:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
String string = "aglasdg";
byte[] bytes = string.getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
}
}
==================================================================
7.lastIndexOf(int ch) 和 IndexOf(int ch) 返回指定字符在此字符串中第一次出现处的索引
8.isEmpty() :判断字符串是否是空串
9.replace("a","A"); 替换字符
案例:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
String string = "adgsfhasdhashdglh";
string = string.replace("a","A");
System.out.println(string);
}
}
==================================================================
10.split(String regex) 分割
注意:有些符号需要转义
案例:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
String string = "ad.gsf.ha.dhas.hdg.lh";
String[] split = string.split("\\.");
for (int i = 0; i < split.length; i++) {
System.out.println(split[i]);
}
}
}
==================================================================
11.substring(3, 5) 截取子串 包头不包尾
案例:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
String string = "aghalsdfh";
String substring = string.substring(3, 5);
System.out.println(substring);
}
}
==================================================================
12.toCharArray() 将字符串转换为一个字符数组
案例:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
String string = "aghalsdfh";
char[] charArray = string.toCharArray();
for (int i = 0; i < charArray.length; i++) {
System.out.println(charArray[i]);
}
}
}
==================================================================
13.toUpperCase() toLowerCase() 大小写转换
案例:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
String string = "aghalsdfh";
String upperCase = string.toUpperCase();
System.out.println(upperCase);
String lowerCase = upperCase.toLowerCase();
System.out.println(lowerCase);
}
}
==================================================================
14.trim() 去除前后的空格
案例:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
String string = " agh al sdfh ";
String trim = string.trim();
System.out.println(trim);
}
}
大数据运算
案例:
package cn.wzx;
public class Demo1 {
public static void main(String[] args) {
double a = 1.0;
double b= 0.9;
double c = 0;
System.out.println(a-b);
System.out.println(a/c);
}
}
存在问题结果不够精确
divide(BigDecimal divisor, int scale, int roundingMode)
divisor:除数
scale:小数点保留几位
roundingMode:取舍方式 ROUND_HALF_UP
案例:
package cn.wzx;
import java.math.BigDecimal;
public class Demo1 {
public static void main(String[] args) {
BigDecimal a = new BigDecimal("1.0");
BigDecimal b = new BigDecimal("0.9");
System.out.println(a.subtract(b));
System.out.println(a.add(b));
System.out.println(a.multiply(b));
System.out.println(a.divide(b, 4, BigDecimal.ROUND_HALF_UP));
}
}