常用类(更新ing)

字符串相关类

String类

Java.lang.String类代表不可变的字符序列

“XXXX”为该类的一个对象

常见的构造方法


public class Test1 {
	public static void main(String[] args) {
		String s1 = "hello";
		String s2 = "world";
		String s3 = "hello";
		System.out.println(s1 == s3); //true
		
		s1 = new String ("hello");
		s2 = new String ("hello");
		System.out.println(s1==s2);  //false
		System.out.println(s1.equals(s2)); //true
		
		char c[]= {'s','u','n',' ','j','a','v','a'};
		String s4 = new String(c);
		String s5 = new String(c,4,4);
		System.out.println(s4); //sun java
		System.out.println(s5); //java
	}

}


public class Test1 {
	public static void main(String[] args) {
		String s1 = "sun java";
		String s2 = "Sun Java";
		System.out.println(s1.charAt(1)); //u
		System.out.println(s2.length());//8
		System.out.println(s1.indexOf("java")); //4
		System.out.println(s1.indexOf("Java")); //-1
		System.out.println(s1.equals(s2));  //false
		System.out.println(s1.equalsIgnoreCase(s2)); //true
		
		String sr = s1.replace('j', 'J');
		System.out.println(sr);
	}

}


public class Test1 {
	public static void main(String[] args) {
		String s = "Welcome to Java World!";
		String s1 = " sun java ";
		System.out.println(s.startsWith("Welcome")); //true
		System.out.println(s.endsWith("World")); //false
		String sL = s.toLowerCase();
		String sU = s.toUpperCase();
		System.out.println(sL);
		System.out.println(sU);
		String subS = s.substring(11);
		System.out.println(subS);
		String sp = s1.trim();
		System.out.println(sp);
		}

}

静态重载方法

public static String valueOf(...)可以将基本类型数据转化为字符串;

eg: public static String valueOf(double d)

public static String valueOf(int i);

public String[] split(String regex)可以将一个字符串按照指定的分隔符分隔,然后返回分隔后的字符串数组。


public class Test1 {
	public static void main(String[] args) {
		int j = 1234567;
		String sNumber = String.valueOf(j);
		System.out.println("j 是" + sNumber.length()+"位数");
		
		String s = "Mary,F,1976";
		String[] sPlit = s.split(",");
		for(int i = 0;i<sPlit.length;i++) {
			System.out.println(sPlit[i]);
		}
	}

}

StringBuffer 类和StringBuilder类

可变的字符序列

String一旦被定义,就不可以被改变,s1+=s2是另外开辟了一片内容,s1,s2拷贝到新的内存中,效率低。

StringBuffer类,可变的,可以直接在后面开辟内存。

常用方法:

重载方法public StringBuffer append(...)可以为该StringBuffer对象添加字符序列,返回添加后的该StringBuffer对象引用。

重载方法public StringBuffer insert(...)可以为该StringBuffer对象在指定位置插入字符序列,返回修改后的该StringBuffer对象引用。

方法public StringBuffer delete(int start,int end)可以删除从start到end-1的一段字符串

方法public StringBuffer reverse()用于将字符序列逆序,返回修改后的该StringBuffer对象

【当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类。

和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。

StringBuilder 类在 Java 5 中被提出,它和 StringBuffer 之间的最大不同在于 StringBuilder 的方法不是线程安全的(不能同步访问)。

由于 StringBuilder 相较于 StringBuffer 有速度优势,所以多数情况下建议使用 StringBuilder 类。然而在应用程序要求线程安全的情况下,则必须使用 StringBuffer 类。】
--------------------- 
作者:Chin_style 
来源:CSDN 
原文:https://blog.csdn.net/weixin_41101173/article/details/79677982 
版权声明:本文为博主原创文章,转载请附上博文链接!

基本数据类型包装类

基本数据类型是不具备对象的特性的,比如基本类型不能调用方法、功能简单。。。,为了让基本数据类型也具备对象的特性, Java 为每个基本数据类型都提供了一个包装类,这样我们就可以像操作对象那样来操作基本数据类型。 

包装类(如:Integer,Double等)这些类封装了一个相应的基本数据类型数值,并为其提供了一系列操作。

以Java.lang.Integer类为例:构造方法


public class Test1 {
	public static void main(String[] args) {
		Integer i = new Integer(100);
		Double d = new Double("123.456");
		int j = i.intValue() + d.intValue();
		float f = i.floatValue()+d.floatValue();
		System.out.println(j + " " + f);
		double pi = Double.parseDouble("3.1415926");
		double r = Double.valueOf("2.0").doubleValue();
		double s = pi*r*r;
		System.out.println(s);
		try {
			int k = Integer.parseInt("1.25");
		}catch (NumberFormatException e) {
			System.out.println("数据格式不对");
		}
		System.out.println(Integer.toBinaryString(123)+"B");
		System.out.println(Integer.toHexString(123)+"H");
		System.out.println(Integer.toOctalString(123)+"O");
	}

}

Java 中基本类型和包装类之间的转换

自动装箱和拆箱的机制,包装类和基本类型之间可以轻松转换

装箱:把基本类型转换成包装类,使其具有对象的性质,又可分为手动装箱和自动装箱

拆箱:把包装类对象转换成基本类型的值,又可分为手动拆箱和自动拆箱

 

Math类


public class Test1 {
	public static void main(String[] args) {
		double a = Math.random();
		double b = Math.random();
		System.out.println(a +" "+ b);
		System.out.println(Math.sqrt(a*a+b*b));
		System.out.println(Math.pow(a, 8));
		System.out.println(Math.round(b));
		System.out.println(Math.log(Math.pow(a, b)Math.E, 15));
		double d = 60.0, r=Math.PI/4;
		System.out.println(Math.toRadians(d));
		System.out.println(Math.toDegrees(r));
	}

}

File类

java.io.File类用于表示文件(目录)

File类只用于表示文件(目录)的信息(名称,大小等),不能用于文件内容的访问

代表系统文件名(路径和文件名)

File类的常见构造方法:

public File(String pathname)

以pathname为路径创建File对象,如果pathname是相对路径,则默认的当前路径在系统属性user.dir中存储

public File(String parent,String child)

以parent为父路径,child为子路径创建file对象

File的静态属性string separator存储了当前系统的路径分隔符

package com.imooc.collection;

import java.io.File;

public class testFile {
	public static void main(String args[]) {
		//创建或删除目录
		File file = new File("D:\\A");
		
		if(!file.exists())
			file.mkdirs();
		else
			file.delete();
		
		//是否是一个目录
		System.out.println(file.isDirectory());
	}

}

 

//以树状结构展现特定的文件夹及其子文件
import java.io.*;
public class FileList {
	public static void main(String[] args) {
		File f = new File("d:/A");
		tree(f);
	}
	
	private static void tree(File f) {
		File[] childs = f.listFiles();
		for(int i=0;i<childs.length;i++) {
			System.out.println(childs[i].getName());
			if(childs[i].isDirectory())
				tree(childs[i]);
			
		}
	}
}

RandomAccessFile java提供的对文件内容的访问,既可以读文件也可以写文件。

RandomAccessFile支持随机访问文件,可以访问文件的任意位置

(1)Java文件模型

    在硬盘上的文件是byte存储的,是数据的集合

(2)打开文件

    有两种模式“rw"”r"

    RandomAccessFile raf = new RandomAccessFile(file,"rw“)

    文件指针,打开文件时指针在开头 pointer = 0;

(3)写方法

    raf.write(int)--->只写一个字节(后8bit),同时指针指向下一个位置,准备重新写入

(4)读方法

    int b = raf.read()------>读一个字节

(5)文件读写完成之后一定要关闭

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;

public class rafDemo {
    public static void main(String[] args) throws IOException {
        File demo = new File("demo");
        if(!demo.exists())
            demo.mkdir();
        File file = new File(demo,"raf.dat");
        if(!file.exists())
            file.createNewFile();

        RandomAccessFile raf = new RandomAccessFile(file,"rw");
        //指针的位置
        System.out.println(raf.getFilePointer());

        raf.write('A'); //一次只写A的后八位
        System.out.println(raf.getFilePointer());
        raf.write('B');

        int i = 0x7fffffff;
        //每次只写一个字节,写8次
        raf.write(i >>>24); //高八位
        raf.write(i >>> 16);
        raf.write(i >>> 8);
        raf.write(i);
        System.out.println(raf.getFilePointer());

        //可以直接写一个int
        raf.writeInt(i);

        String s = "中";
        byte[] gbk = s.getBytes("gbk");
        raf.write(gbk);
        System.out.println(raf.length());

        //读文件,必须把指针移到头部
        raf.seek(0);
        //一次性读取,把文件内容读到字节数组中
        byte[] buf = new byte[(int)raf.length()];
        raf.read(buf);
        System.out.println(Arrays.toString(buf));

        raf.close();

    }
}

枚举类


public class TestEnum {
	public enum MyColor{ red, green, blue }; //与c不同,不能与0,1,对应和替换
	public static void main(String[] args) {
		MyColor m = MyColor.red;
		switch(m) {
		case red:
			System.out.println("red");
			break;
		case green:
			System.out.println("green");
			break;
		case blue:
			System.out.println("blue");
			break;
		default:
			System.out.println("XX");
		
		}
		System.out.println(m);
	}

}

Date 和 SimpleDateFormat 类

在程序开发中,经常需要处理日期和时间的相关数据,此时我们可以使用 java.util 包中的 Date 类。这个类最主要的作用就是获取当前时间,我们来看下 Date 类的使用:

使用Date类的默认无参构造方法创建出的对象就代表当前时间,我们可以直接输出Date对象显示当前的事件

若想按指定格式输出,就需要SimpleDateFormat

1. 使用 format() 方法将日期转换为指定格式的文本

yyyy-MM-dd HH:mm:ss” 为预定义字符串, yyyy 表示四位年, MM 表示两位月份, dd 表示两位日期, HH 表示小时(使用24小时制), mm 表示分钟, ss 表示秒,这样就指定了转换的目标格式,最后调用 format() 方法将时间转换为指定的格式的字符串。

2. 使用 parse() 方法将文本转换为日期

 代码中的 “yyyy年MM月dd日 HH:mm:ss” 指定了字符串的日期格式,调用 parse() 方法将文本转换为日期。

package com.imooc.test;

import java.util.*;
import java.text.*;

public class TestDate {
	public static void main(String[] args)  {
		Date d = new Date();
		System.out.println(d);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
		String today = sdf.format(d);
		System.out.println(today);
		
		String day = "2018年10月20日 11:10:10";
		SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		Date date = null;
		try {
			date = df.parse(day);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("当前时间:"+ date);
	}

}

1、 调用 SimpleDateFormat 对象的 parse() 方法时可能会出现转换异常,即 ParseException ,因此需要进行异常处理

2、 使用 Date 类时需要导入 java.util 包,使用 SimpleDateFormat 时需要导入 java.text 包

Calendar类的应用

Date 类最主要的作用就是获得当前时间,同时这个类里面也具有设置时间以及一些其他的功能,但是由于本身设计的问题,这些方法却遭到众多批评,不建议使用,更推荐使用 Calendar 类进行时间和日期的处理。

 java.util.Calendar 类是一个抽象类,可以通过调用 getInstance() 静态方法获取一个 Calendar 对象,此对象已由当前日期时间初始化,即默认代表当前时间,如 Calendar c = Calendar.getInstance();

其中,调用Calendar类的getInstance()方法获取一个实例,然后通过调用get()方法获取日期时间信息,参数为需要获得的字段的值

 

Calendar类还提供了getTime()方法,用来获取Date对象,完成Calendar和Date的转换,还可以通过getTimeInMillis()方法,获得以毫秒为单位的时间

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值