系统常见类

Object类
Object类的作用:
— 在Java中,所有的类 ,不管有没有定义其父类,都以Object类
作为直接或间接的父类。
—也就是说,所有的类都继承Object类。
—Object类定义了基本的成员变量和成员函数。
1.对象间的比较
2.将对象转换成字符串toString()
3.线程的等待与通知
4.返回对象所属的类getClass()
Class类

在java中,每个class类都有一个相应的class对象。也就是说,当我们编写一个类,编译完成后,在生成的.class文件中,就会产生一个Class对象,用于表示这个类的类型信息。
获取Class实例的三种方式:
1.利用对象调用getClass()方法获取该对象的class实例。
2.使用class类的静态方法forName(),用类的名字获取一个Class实例。
3.运用.class的方式来获取Class实例,对于基本数据类型的封装类,还可以采用.TYPE来获取相对应的基本数据类型的Class实例。
String类
String类的定义

public final class String

extends Object

implements Serializable, Comparable, CharSequence

1.使用了final 修饰符,说明String类没有子类,不可被继承。

2.实现了implements Serializable, Comparable, CharSequence

3.String类的使用

1.可以像基本数据类型使用方式一样创建对象。

int a=100;

String s=”hello”; ”hello”–字符串常量

2.可以通过String类提供的构造方法创建对象

String常用构造方法:

String() 创建一个空字符串对象。

String(String original) 通过指定的字符串常量,创建一个字符串对象。

String(byte[] bytes, int offset, int length)通过字节数组创建一个字符串对象。【字节数组转换成字符串】。

String(char[] value, int offset, intcount) 通过字符数组创建一个字符串对象。【字符数组转换成字符串】。

例如:

package com.click369.test1;

public class StringTest1 {

public static void main(String[] args) {

	//String的对象创建

	//1.可以像基本数据类型使用方式一样创建对象。

	String  s1="hello";

	//2.可以通过String类提供的构造方法创建对象

    /*

     * 常用构造方法:

	String() 创建一个空字符串对象。

	String(String original) 通过指定的字符串常量,创建一个字符串对象。

	String(byte[] bytes, int offset, int length)通过字节数组创建一个字符串对象。【字节数组转换成字符串】。

	//byte[] bytes---被转换的字节数组

	//int offset---字节数组转换数据的起始位置

	//int length---被转换的个数

	//将字节数组中的数字值转换成了字符串

	String(char[] value, int offset, int count) 通过字符数组创建一个字符串对象。【字符数组转换成字符串】。

    //char[] value---被转换的字符数组

	//int offset---字符数组转换数据的起始位置

	//int count---被转换的个数

     */

	String  s2=new String();

	String  s3=new String("hello");

	byte  bytes[]={97,98,99,100};

	String  s4=new String(bytes,0,4);

	System.out.println("s4=="+s4);

	char  value[]={'h','e','l','l','o'};

	String  s5=new String(value,0,value.length);

	System.out.println("s5=="+s5);

}

}

String类中常用方法的使用:

1.public char charAt(int index)得到原始字符串的指定位置[从0开始]的字符

2.public byte[] getBytes(String charsetName)

public byte[] getBytes()

3.public boolean equals(Object anObject)

4.public boolean equalsIgnoreCase(String anotherString)不考虑大小写

5.public boolean startsWith(String prefix)

6.public boolean endsWith(String suffix)

7.public int indexOf(String str)

8.public int lastIndexOf(String str)

9.public String substring(int beginIndex)

public String substring(int beginIndex,int endIndex)

10.public String concat(String str)

11.public String replace(String oldChar,String newChar)

12.public boolean matches(String regex) 匹配正则表达式

13.public boolean contains(String s)

14.public String[] split(String regex)

15.public String toLowerCase()转换为小写

16.public String toUpperCase()

17.public String trim()

18.public char[] toCharArray()

例如:

package com.click369.test1;

public class StringTest2 {

public static void main(String[] args) {

	String  s1=new String("hello,world");

	//public char charAt(int index)得到原始字符串的指定位置[从0开始]的字符

	char ch=s1.charAt(5);

	System.out.println("ch=="+ch); //,

	//public byte[] getBytes(String charsetName)将原始字符按照指定字符编码转换成字节数组。

	//charsetName--字符编码【utf-8,GBK,GB2312...】

	//public byte[] getBytes()将原始字符换成字节数组。

	byte bytes[]=s1.getBytes();

	for(byte  b:bytes){

		System.out.println("b=="+b);

	}

	//?????????

	//public boolean equals(Object anObject)比较两个字符串是否相等。

	boolean b=s1.equals("hello,world");

	System.out.println("b=="+b);

	//public boolean equalsIgnoreCase(String anotherString)比较两个字符串是否相等,不考虑大小写

	boolean b1=s1.equalsIgnoreCase("HELLO,world");

	System.out.println("b1=="+b1);

	//public boolean startsWith(String prefix)前缀匹配【从一组用户名中得到所有姓李的用户名】

	String names[]={"李三","张三","李四","张四"};

	for(String name:names){

		if(name.startsWith("李")){

			System.out.println("name=="+name);

		}

	}

	//public boolean endsWith(String suffix)后缀匹配【从一组用户名中得到所有用“四”结尾的用户】

	for(String name:names){

		if(name.endsWith("四")){

			System.out.println("name=="+name);

		}

	}

	//public int indexOf(String str)得到指定字符串在原始字符串中第一次出现的位置。[没有就-1]

	int in=s1.indexOf("a");

	System.out.println("in=="+in);

	//public int lastIndexOf(String str)得到指定字符串在原始字符串中最后一次出现的位置。[没有就-1]

	int ind=s1.lastIndexOf("o");

	System.out.println("ind=="+ind);

	//public String substring(int beginIndex)截取字符串

	//public String substring(int beginIndex,int endIndex)【包前不包后】

	String sub1=s1.substring(2, 9);

	System.out.println("sub1=="+sub1);

	//http://e.hiphotos.baidu.com/image/pic/item/4610b912c8fcc3cef70d70409845d688d53f20f7.jpg

	String imgurl="http://e.hiphotos.baidu.com/image/pic/item/4610b912c8fcc3cef70d70409845d688d53f20f7.jpg";

	String imgname=imgurl.substring(imgurl.lastIndexOf("/")+1);

	System.out.println("imgname=="+imgname);

	//public String concat(String str) 类似与“+”,字符串链接符

	String newstr=s1.concat(",网星软件");

	System.out.println("newstr=="+newstr);

	//public String replace(String oldChar,String newChar)替换字符串

	String info="name=zhangsan,age=23";

	String newinfo=info.replace("zhangsan","lisi");

	System.out.println("newinfo=="+newinfo);

	//public boolean matches(String regex)  匹配正则表达式

	//public boolean contains(String s)判断指定的字符串是否在原始字符串中存在

	boolean  b2=s1.contains("Hello");

	System.out.println("b2=="+b2);

	//public String[] split(String regex)把原始字符串按照指定的分隔符拆分成String数组

	String info1="name=zhangsan,age=23";

	String  infoArray[]=info1.split("=");

	for(String  stri:infoArray){

		System.out.println("stri:"+stri);

	}

	//public String toLowerCase()转换为小写

	//public String toUpperCase()转换为大写

	//public String trim()//去除两端空格

	String s2="   zhang  san   ";

	System.out.println("s2:"+s2.length());

	String zhangsanstring=s2.trim();

	System.out.println("zhangsanstring:"+zhangsanstring.length());

	//public char[] toCharArray()将字符串转换成字符数组

	char  charray[]=s1.toCharArray();

	for(char c:charray){

		System.out.println("c=="+c);

	}

	

}

}

BigDecimal类

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

浮点数的错位问题

​ 计算机在存储浮点数,通过一种算法存储的,而不是真正 的存储,在计算的时候,可能会出现精度的损失。

BigDecimal bc1 = new BigDecimal(0.1);

BigDecimal bc2 = new BigDecimal(0.2);

System.out.println(bc1.add(bc2));

注意:在使用BigDecimal时,将数据做成字符串,再转换

BigDecimal bc1 = new BigDecimal(“0.1”);

BigDecimal bc2 = new BigDecimal(“0.2”);

System类和Runtime类

​ System类: java官方提供跟系统相关的类。

​ System类的构造函数是私有的,所有属性和方法都是静态的

​ |-- in |–out |–err

​ |–System.currentTimeMillis(); // 获取当前时间戳

​ |–System.nanoTime(); //获取当前时间的纳秒数

​ |–Sysytem.arraycopy(Object src, int srcpos,Object dest, int destpos, int length);

​ System.exit(0); //终止JVM运行

​ System.gc(); //强制调用一次垃圾回收机制

​ System.loadLibrary(“xxx”); //加载底层库

​ Runtime runtime = Runtime.getRuntime();

​ runtime.exec(""); // 能够执行当前系统的命令

​ System.exec(“mspaint”) // 打开画板

​ System.exec(“notepad”) // 打开记事本

​ System.exec(“calc”) // 打开计算器

​ // 获取字节码开始执行时的信息

System.out.println(runtime.totalMemory()); 总内存

System.out.println(runtime.freeMemory()); 剩余内存

System.out.println(runtime.maxMemory()); 最大内存

Calendar类

主要作用就是在jdk提供补充Date

Calendar calendar = Calendar.getInstance();

System.out.println(calendar.get(Calendar.YEAR));

System.out.println(calendar.get(Calendar.MONTH)+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));

LocalTime now = LocalTime.now(); // 获取当前时间对象

LocalTime time = LocalTime.of(15.23); // 获取一个需要的时间对象

LocalDate now2 = LocalDate.now(); //如果使用日期,则使用LocalDate

LocalDateTime ldt = LocalDateTime.now(); //当前年月日时分秒

// 官方提供了几种常见的格式,如果需要就是这种,则选择即可

String format = ldt.format(DateTimeFormatter.ISO_DATE);

String format = ldt.format(DateTimeFormatter.ISO_TIME);

String format = ldt.format(DateTimeFormatter.ISO_DATE_TIME);

// 如果官方提供的不能满足使用,则需要手动格式化

String format2 = lat.format(DateTimeFormatter.ofpattern(“yyyy-MM-dd HH:mm:ss”));

Instant i = Instant.now(); // 伦敦标准时间

NumberFormat

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

格式化货币 格式化百分比 格式化数字(科学计数法)

DecimalFormat

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

Math.round() 四舍五入 Math.ceil() 向上取整 Math.floor()向下取整

// # 主要是在.之后,表示要保留的小数位

​ % 表示已百分比显示

// 获取系统默认的货币符号

NumberFormat numberFormat = NumberFormat.getCurrencyInstance();

String format = numberFormat.format(12345);

// 执行格式化的标准

// 可以指定需要的国家货币符号

System.out.println(NumberFormat.getCurrencyInstance(Locale.JAPAN).format(456123));

// 格式化数字为科学计数法

System.out.println(NumberFormat.getNumberInstance().format(5464564));

// 格式化为百分比

System.out.println(NumberFormat.getPercentInstance().format(0.2));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值