当前位置: 移动技术网 > IT编程>开发语言>Java > Java 系统常见类

Java 系统常见类

2020年07月27日  | 移动技术网IT编程  | 我要评论
String类字符串类(String)字符串常量 final修饰的一个类,使用双引号。字符串对象的创建:// 1、使用双引号创建字符串String s= "hello world";// 2、使用字符串的构造函数String s1 = new String();System.out.println(s1.length());String s2 = new String("hello world");System.out.println(s2);不能通过==比较字

String类

字符串类(String)
字符串常量 final修饰的一个类,使用双引号。

字符串对象的创建:
	// 1、使用双引号创建字符串
	String s= "hello world";

	// 2、使用字符串的构造函数
	String s1 = new String();
	System.out.println(s1.length());
	
	String s2 = new String("hello world");
	System.out.println(s2);

不能通过==比较字符串,因为字符串是字符常量,java的字符串的值是存储在字符串常量池的地方,整个内存中只有一份,==比较的是String 变量(即地址)所以java中字符串不能使用 == 比较,应该使用java 字符串提供的方法 equals。在字符串比较中,如果编译时,可以确定字符串值,不会创建对象。
如果编译时,字符串值不能确定(运行时才能确定),则会创建对象。

字符串类中的常见官方方法:
charAt()
codePointAt()
indexOf()
lastIndexOf()
length()
subString()
split()
trim()
toCharArray()
toUpperCase()
toLowerCase()
toString()
equals()
equalsIgnoreCase()
getBytes()
isEmpty()
matches()
replace()
replaceAll()
startsWith()
endsWith()
join()

StringBuilder和StringBuffer
StringBuffer:字符串变量(Synchronized,即线程安全)。如果要频繁对字符串内容进行修改,出于效率考虑最好使用 StringBuffer,如果想转成 String 类型,可以调用 StringBuffer 的 toString() 方法。
Java.lang.StringBuffer 线程安全的可变字符序列。在任意时间点上它都包含某种特定的字符序列,但通过某些方法调用可以改变该序列的长度和内容。可将字符串缓冲区安全地用于多个线程。
StringBuffer 上的主要操作是 append 和 insert 方法,可重载这些方法,以接受任意类型的数据。每个方法都能有效地将给定的数据转换成字符串,然后将该字符串的字符追加或插入到字符串缓冲区中。

StringBuilder:字符串变量(非线程安全)。在内部,StringBuilder 对象被当作是一个包含字符序列的变长数组,StringBuilder中的方法和StringBuffer方法几乎一样。
java.lang.StringBuilder 是一个可变的字符序列,是 JDK5.0 新增的。此类提供一个与 StringBuffer 兼容的 API,但不保证同步。该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍)。

三者的区别
String 类型和 StringBuffer 的主要性能区别:String 是不可变的对象, 因此在每次对 String 类型进行改变的时候,都会生成一个新的 String 对象,然后将指针指向新的 String 对象,所以经常改变内容的字符串最好不要用 String ,因为每次生成对象都会对系统性能产生影响,特别当内存中无引用对象多了以后, JVM 的 GC 就会开始工作,性能就会降低。
使用 StringBuffer 类时,每次都会对 StringBuffer 对象本身进行操作,而不是生成新的对象并改变对象引用。所以多数情况下推荐使用 StringBuffer ,特别是字符串对象经常改变的情况下。

Object类

Object类 是类层次结构的根类。每个类都使用 Object 作为父类。所有对象(包括数组)都实现这个类的方法。
简单来说就是,Object类是所有类的父类,包括我们所写的类,我们在使用类的时候就会利用Object类中的方法

public class TestObj {
	public static void main(String[] args) {
		Object obj = new String("hello world");//Object 作为父类的引用指向子类实例
		System.out.println(obj);
	}
}

Date类

		Date的两个构造函数。
		// 获取了一个时间对象
		// 注意,获取的时间是当前时间(本机)
		Date date = new Date();

		// 构造一个指定的时间,通过时间戳指定
		Date d2 = new Date(20000L);//参数为long 毫秒数
		System.out.println(d2);

		获取时间戳
		// 时间戳  1970.01.01 00:00:00 ~ 到当前这个时间的毫秒数
		System.out.println(date.getTime());

LocalTime 类

public void testLocal() {
		LocalTime lt = LocalTime.now();// 获取当前时间对象
		
		System.out.println(lt.getHour());// 获取当前小时数
		System.out.println(lt.getMinute());// 获取当前分钟
		System.out.println(lt.getSecond());// 获取当前秒数
		System.out.println(lt.getNano());// 获取当前纳秒数
		
		LocalDate ld = LocalDate.now();// 如果使用日期,则使用LocalDate
		
		System.out.println(ld.getMonthValue());// 获取当前月数
		System.out.println(ld.getYear());// 获取当前年份
		
		LocalDateTime ldt = LocalDateTime.now();
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");//格式化时间,线程安全
		
		System.out.println(ldt.format(df));
	}

Calendar 日历类

public class TestCalendar {

	public static void main(String[] args) {
		
		// Calender 主要的作用就是在jdk提供补充Date类
		Calendar calendar = Calendar.getInstance(); 
		System.out.println(calendar.get(Calendar.YEAR));// 获取当前年份
		System.out.println(calendar.get(Calendar.MONTH) + 1);//// 获取当前月份-1
		System.out.println(calendar.get(Calendar.DAY_OF_MONTH));// 获取当前是该月的第几天
		System.out.println(calendar.get(Calendar.DATE));// 获取当前是该月的第几天
		System.out.println(calendar.get(Calendar.WEEK_OF_MONTH));// 获取当前是该月的第几周
	}
}

NumberFormat类

NumberFormat是Format子类,是java用来格式化数值的一个类

public void testNumberFormat() {
		// 获取系统默认的货币符号
		NumberFormat numberFormat = NumberFormat.getCurrencyInstance();
		String format = numberFormat.format(789135);
		System.out.println(format);
		
		// 指定格式化的标准
		// 可以指定需要的国家货币符号
		System.out.println(NumberFormat.getCurrencyInstance(Locale.FRENCH).format(45678213));
		
		// 格式化数字为科学计数法
		System.out.println(NumberFormat.getNumberInstance().format(123456789));
		// 格式化为百分比
		System.out.println(NumberFormat.getPercentInstance().format(0.5));
	}

DecimalFormat类

DecimalFormat是NumberFormat子类,主要的目的就是格式化小数(浮点数)

public void testDecimalFormat() {
		double num = 3.1415926;
		// # 主要是在.之后,表示要保留的小数位
		DecimalFormat df = new DecimalFormat("0.00");
		String format = df.format(num);
		System.out.println(format);//3.14
		
		DecimalFormat df2 = new DecimalFormat("0E0");
		System.out.println(df2.format(num));//3E0
	}

BigDecimal类

主要用来处理大数据(位数),同时也提供准确计算大数据的方案。

public void testBigDecimal() {
		// 计算机在存储浮点数,通过一种算法存储的,而不是真正的存储
		// 在计算的时候,可能会出现精度的损失
		System.out.println(0.1 + 0.2);//输出 0.30000000000000004
		System.out.println(0.3 / 0.2);//输出 1.4999999999999998
		
		// 注意,建议在使用BigDecimal是,将数据做成字符串,再转换
		BigDecimal bc1 = new BigDecimal("0.1");
		BigDecimal bc2 = new BigDecimal("0.2");
		System.out.println(bc1.add(bc2));//输出 0.3
		System.out.println(bc1.subtract(bc2));//输出 -0.1
		System.out.println(bc1.multiply(bc2));//输出 0.02
		System.out.println(bc1.divide(bc2));//输出 0.5
	}

本文地址:https://blog.csdn.net/ly86501814/article/details/107580707

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网