在尚学堂的视频中,马士兵老师讲解了Java中常用的类,主要包括字符串相关类(String类和StringBuffer类 )、基本数据类型包装类、Math类、File类以及枚举类。字符串类型都是一种类类型,java中的字符串都是对象,也就是说每一个字符串都可以看成是某一个字符串相关类的类对象。基本数据类型包装类封装了封装了相应的基本数据类型数值,并为它提供一系列操作。Math类提供提供一系列方法用于科学计算。File类代表系统文件名,通过它来访问这些文件的属性,对文件进行系列操作。在枚举类中,通过事先定义好的一些常量来解决java中的类型问题。
字符串相关类
String类
String 类代表字符串。Java 中的所有字符串,比如 "aaa" ,都作为此类的实例实现。字符串是常量;它们的值在创建之后不能够被更改,它的字符序列是不可变的。
常用构造方法:
实例:
class Test{
public static void main(String[] args){
char str1[] = {'a', 'b', 'c','d','e'};
String s1="hello";
s1 = new String("Hello World");
System.out.println("创建String对象为original的拷贝为:" + s1);
String str = new String(str1);
System.out.println("字符数组创建对象为:" + str);
String str3 = new String(str1,1,4);
System.out.println("用一个字符数组从offset项开始的count个字符序列创建的对象为:" + str3);
}
}
常用方法:
实例:
class Test1{
public static void main(String[] args){
String str1="Welcome to Java World!";
String str2=" Java Hello ";
String str3="Welcome,to,Java,World!";
Double str4=1221.5;
System.out.println(str1.startsWith("Welcome"));
System.out.println(str1.toUpperCase());
System.out.println(str1.substring(5));
System.out.println(str2.trim());
//以逗号分隔,获得一串数组
String Okay[] = str3.split(",");
//打印这个数组
System.out.println(java.util.Arrays.toString(Okay));
System.out.println(String.valueOf(str4));
}
}
StringBuffer类
常用构造方法:
class aa{
public static void main(String[] args){
//分配空间,默认长度为16字符
StringBuffer buf = new StringBuffer();
//分配空间,长度为512字符
StringBuffer buf2 = new StringBuffer(512);
String s= "朱丹";
StringBuffer buf1 = new StringBuffer(s);
System.out.println(buf);
System.out.println(buf2);
System.out.println(buf1);
}
}
常用方法:
class aa{
public static void main(String[] args){
String s= "zd";
char[] a={'a','b','c'};
StringBuffer buf1 = new StringBuffer(s);
buf1.append(',').append("好好学习");
buf1.append(',').append("天天向上");
System.out.println("字符序列为:"+buf1);
StringBuffer buf2 = new StringBuffer(buf1);
buf2.insert(0,1);
System.out.println("插入字符序列后为:"+buf2);
StringBuffer buf3 = new StringBuffer(buf1);
buf3.delete(2,5);
System.out.println("删除字符序列后为:"+buf3);
System.out.println("逆序为:"+buf1.reverse());
}
}
基本数据类型包装类
常用构造方法:
class cc{
public static void main(String[] args){
String s= "1221";
Integer int1=new Integer(2);
Integer int2=new Integer(s);
System.out.println(int1);
System.out.println(int2);
}
}
常用方法:
Math类
class cc{
public static void main(String[] args){
//产生随机数
double a=Math.random();
double b=Math.random();
System.out.println("a为"+a);
System.out.println("b为"+b);
//平方根运算
System.out.println("平方根为:"+Math.sqrt(a*a+b*b));
//幂运算
System.out.println("幂运算结果为:"+Math.pow(a,8));
//
System.out.println("b转换为long类型为:"+Math.round(b));
System.out.println("两个数最大值为:"+Math.max(a,b));
}
}
File类
import java.io.*;
class TT{
public static void main(String[] args){
//分隔符“/”
String separator=File.separator;
System.out.println(separator);
String filename="zd.txt";
String directory="zd1"+separator+"zd2";
File f=new File(directory,filename);
if(f.exists())
{
System.out.println("文件名:"+f.getAbsolutePath());
System.out.println("文件大小:"+f.length());
}
else
{
f.getParentFile().mkdirs();
try
{
f.createNewFile();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}