黑马程序员——Java API-常用类

来源:互联网 发布:2017淘宝直通车 编辑:程序博客网 时间:2024/05/21 04:19

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

API的概述
(1)应用程序编程接口。
(2)就是JDK提供给我们的一些提高编程效率的java类。

Object类
(1)Object是类层次结构的根类,所有的类都直接或者间接的继承自Object类。
(2)Object类的构造方法有一个,并且是无参构造
子类构造方法默认访问父类的构造是无参构造
(3)要掌握的方法:
A:toString()
返回对象的字符串表示,默认是由类的全路径+'@'+哈希值的十六进制表示。
B:equals()
比较两个对象是否相同。默认情况下,比较的是地址值是否相同。
而比较地址值是没有意义的,所以,一般子类也会重写该方法。
(4)要了解的方法:
A:hashCode() 返回对象的哈希值。不是实际地址值,可以理解为地址值。
B:getClass() 返回对象的字节码文件对象。
C:finalize() 用于垃圾回收,在不确定的时间。
D:clone() 可以实现对象的克隆,包括成员变量的数据复制,但是它和两个引用指向同一个对象是有区别的。
(5)两个注意问题;
A:直接输出一个对象名称,其实默认调用了该对象的toString()方法。
==和equals()的区别?
A:==
基本类型:比较的是值是否相同
引用类型:比较的是地址值是否相同
B:equals()
只能比较引用类型。默认情况下,比较的是地址值是否相同。
但是,我们可以根据自己的需要重写该方法。


Scanner的使用
(1)在JDK5以后出现的用于键盘录入数据的类。
(2)构造方法:
A:System.in这个东西。
它其实是标准的输入流,对应于键盘录入
B:构造方法
InputStream is = System.in;
Scanner(InputStream is)
C:常用的格式
Scanner sc = new Scanner(System.in);
(3)基本方法格式:
A:hasNextXxx() 判断是否是某种类型的元素
B:nextXxx() 返回某种类型的元素
(4)要掌握的两个方法
A:public int nextInt()
B:public String nextLine()
(5)需要注意的小问题
A:同一个Scanner对象,先获取数值,再获取字符串会出现一个小问题。
B:解决方案:
a:重新定义一个Scanner对象
b:把所有的数据都用字符串获取,然后再进行相应的转换
示例:
package cn.itheima_01;
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
// 创建对象
Scanner sc = new Scanner(System.in);

int x = sc.nextInt();

System.out.println("x:" + x);
}
}

package cn.itheima01;
import java.util.Scanner;
public class ScannerDemo1 {
public static void main(String[] args) {
//键盘录入int类型的数据
Scanner sc = new Scanner(System.in);

//判断是否是int类型元素
if(sc.hasNextInt()){
int a = sc.nextInt();
System.out.println("a="+a);
}else{
System.out.println("请输入正确的数据");
}
}
}
String类的概述和使用
(1)多个字符组成的一串数据。
其实它可以和字符数组进行相互转换。
(2)构造方法:
A:public String()
B:public String(byte[] bytes)
C:public String(byte[] bytes,int offset,int length)
D:public String(char[] value)
E:public String(char[] value,int offset,int count)
F:public String(String original)
下面的这一个虽然不是构造方法,但是结果也是一个字符串对象
G:String s = "hello";
(3)字符串的特点
字符串一旦被赋值,就不能改变。
注意:这里指的是字符串的内容不能改变,而不是引用不能改变。
字符串的功能
A:判断功能
boolean equals(Object obj):比较字符串内容是否相同。
boolean equalsIgnoreCase(String str):忽略大小写比较字符串内容是否相同。
boolean contains(String str):判断大字符串是否包含小字符串。
boolean startsWith(String str):判断是否以指定字符串开头。
boolean endsWith(String str):判断是否以指定字符串结尾。
boolean isEmpty():判断字符串内容是否为空。

示例:
public class StringDemo {
public static void main(String[] args) {
// 创建字符串对象
String s1 = "helloworld";
String s2 = "helloworld";
String s3 = "HelloWorld";

// boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
System.out.println("equals:" + s1.equals(s2));
System.out.println("equals:" + s1.equals(s3));
System.out.println("-----------------------");

// boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
System.out.println("equals:" + s1.equalsIgnoreCase(s2));
System.out.println("equals:" + s1.equalsIgnoreCase(s3));
System.out.println("-----------------------");

// boolean contains(String str):判断大字符串中是否包含小字符串
System.out.println("contains:" + s1.contains("hello"));
System.out.println("contains:" + s1.contains("hw"));
System.out.println("-----------------------");

// boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
System.out.println("startsWith:" + s1.startsWith("h"));
System.out.println("startsWith:" + s1.startsWith("hello"));
System.out.println("startsWith:" + s1.startsWith("world"));
System.out.println("-----------------------");

// boolean isEmpty():判断字符串是否为空。
System.out.println("isEmpty:" + s1.isEmpty());

String s4 = "";
String s5 = null;
System.out.println("isEmpty:" + s4.isEmpty());
// NullPointerException
// s5对象都不存在,所以不能调用方法,空指针异常
System.out.println("isEmpty:" + s5.isEmpty());
}
}

B:获取功能
int length()
char charAt(int index)
int indexOf(int ch)
int indexOf(String str)
int indexOf(int ch,int fromIndex)
int indexOf(String str,int fromIndex)
String substring(int start)
String substring(int start,int end)

示例:
public class StringDemo {
public static void main(String[] args) {
// 定义一个字符串对象
String s = "helloworld";

// int length():获取字符串的长度。
System.out.println("s.length:" + s.length());
System.out.println("----------------------");

// char charAt(int index):获取指定索引位置的字符
System.out.println("charAt:" + s.charAt(7));
System.out.println("----------------------");

// int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
System.out.println("indexOf:" + s.indexOf('l'));
System.out.println("----------------------");

// int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
System.out.println("indexOf:" + s.indexOf("owo"));
System.out.println("----------------------");

// int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
System.out.println("indexOf:" + s.indexOf('l', 4));
System.out.println("indexOf:" + s.indexOf('k', 4)); // -1
System.out.println("indexOf:" + s.indexOf('l', 40)); // -1
System.out.println("----------------------");

// String substring(int start):从指定位置开始截取字符串,默认到末尾。包含start这个索引
System.out.println("substring:" + s.substring(5));
System.out.println("substring:" + s.substring(0));
System.out.println("----------------------");

// String substring(int start,int
// end):从指定位置开始到指定位置结束截取字符串。包括start索引但是不包end索引
System.out.println("substring:" + s.substring(3, 8));
System.out.println("substring:" + s.substring(0, s.length()));
}
}

C:转换功能
byte[] getBytes()
char[] toCharArray()
static String valueOf(char[] chs)
static String valueOf(int i)
String toLowerCase()
String toUpperCase()
String concat(String str)

示例:
public class StringDemo {
public static void main(String[] args) {
// 定义一个字符串对象
String s = "JavaSE";

// byte[] getBytes():把字符串转换为字节数组。
byte[] bys = s.getBytes();
for (int x = 0; x < bys.length; x++) {
System.out.println(bys[x]);
}
System.out.println("----------------");

// char[] toCharArray():把字符串转换为字符数组。
char[] chs = s.toCharArray();
for (int x = 0; x < chs.length; x++) {
System.out.println(chs[x]);
}
System.out.println("----------------");

// static String valueOf(char[] chs):把字符数组转成字符串。
String ss = String.valueOf(chs);
System.out.println(ss);
System.out.println("----------------");


// static String valueOf(int i):把int类型的数据转成字符串。
int i = 100;
String sss = String.valueOf(i);
System.out.println(sss);
System.out.println("----------------");

// String toLowerCase():把字符串转成小写。
System.out.println("toLowerCase:" + s.toLowerCase());
System.out.println("s:" + s);
// System.out.println("----------------");
// String toUpperCase():把字符串转成大写。
System.out.println("toUpperCase:" + s.toUpperCase());
System.out.println("----------------");

// String concat(String str):把字符串拼接。
String s1 = "hello";
String s2 = "world";
String s3 = s1 + s2;
String s4 = s1.concat(s2);
System.out.println("s3:"+s3);
System.out.println("s4:"+s4);
}
}

D:其他功能
a:替换功能 
String replace(char old,char new)
String replace(String old,String new)
b:去空格功能
String trim()
c:按字典比较功能
int compareTo(String str)
int compareToIgnoreCase(String str) 

示例:
public class StringDemo {
public static void main(String[] args) {
// 替换功能
String s1 = "helloworld";
String s2 = s1.replace('l', 'k');
String s3 = s1.replace("owo", "ak47");
System.out.println("s1:" + s1);
System.out.println("s2:" + s2);
System.out.println("s3:" + s3);
System.out.println("---------------");

// 去除字符串两空格
String s4 = " hello world  ";
String s5 = s4.trim();
System.out.println("s4:" + s4 + "---");
System.out.println("s5:" + s5 + "---");

// 按字典顺序比较两个字符串
String s6 = "hello";
String s7 = "hello";
String s8 = "abc";
String s9 = "xyz";
System.out.println(s6.compareTo(s7));// 0
System.out.println(s6.compareTo(s8));// 7
System.out.println(s6.compareTo(s9));// -16
}
}

StringBuffer:
(1)用字符串做拼接,比较耗时并且也耗内存,而这种拼接操作又是比较常见的,为了解决这个问题,Java就提供了
  一个字符串缓冲区类。StringBuffer供我们使用。
(2)StringBuffer的构造方法
A:StringBuffer()
B:StringBuffer(int size)
C:StringBuffer(String str)
(3)StringBuffer的常见功能
A:添加功能:
public StringBuffer append(String str):可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身

示例:
public class StringBufferDemo {
public static void main(String[] args) {
// 创建字符串缓冲区对象
StringBuffer sb = new StringBuffer();

// public StringBuffer append(String str)
// StringBuffer sb2 = sb.append("hello");
// System.out.println("sb:" + sb);
// System.out.println("sb2:" + sb2);
// System.out.println(sb == sb2); // true

// 一步一步的添加数据
// sb.append("hello");
// sb.append(true);
// sb.append(12);
// sb.append(34.56);

// 链式编程
sb.append("hello").append(true).append(12).append(34.56);
System.out.println("sb:" + sb);


// public StringBuffer insert(int offset,String
// str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
sb.insert(5, "world");

System.out.println("sb:" + sb);
}
}

B:删除功能
     public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身
  public StringBuffer delete(int start,int end):删除从指定位置开始指定位置结束的内容,并返回本身

示例:
public class StringBufferDemo {
public static void main(String[] args) {
// 创建对象
StringBuffer sb = new StringBuffer();
// 添加功能
sb.append("hello").append("world").append("java");
System.out.println("sb:" + sb);

// public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身
// 需求:我要删除e这个字符,肿么办?
// sb.deleteCharAt(1);
// 需求:我要删除第一个l这个字符,肿么办?

// sb.deleteCharAt(1);
// public StringBuffer delete(int start,int

// end):删除从指定位置开始指定位置结束的内容,并返回本身
// 需求:我要删除world这个字符串,肿么办?
// sb.delete(5, 10);

// 需求:我要删除所有的数据
sb.delete(0, sb.length());

System.out.println("sb:" + sb);
}
}

C:替换功能
public StringBuffer replace(int start,int end,String str):从start开始到end用str替换

示例:
public class StringBufferDemo {
public static void main(String[] args) {
// 创建字符串缓冲区对象
StringBuffer sb = new StringBuffer();

// 添加数据
sb.append("hello");
sb.append("world");
sb.append("java");
System.out.println("sb:" + sb);

// public StringBuffer replace(int start,int end,String
// str):从start开始到end用str替换
// 需求:我要把world这个数据替换为"节日快乐"
sb.replace(5, 10, "节日快乐");
System.out.println("sb:" + sb);
}
}

D:反转功能
public StringBuffer reverse()

示例:
public class StringBufferDemo {
public static void main(String[] args) {
// 创建字符串缓冲区对象
StringBuffer sb = new StringBuffer();

// 添加数据
sb.append("
片画动爱我");
System.out.println("sb:" + sb);

// public StringBuffer reverse()
sb.reverse();
System.out.println("sb:" + sb);
}
}

E:截取功能:注意返回值类型不再是StringBuffer本身了
public String substring(int start)
 
       public String substring(int start,int end)

示例:
public class StringBufferDemo {
public static void main(String[] args) {
// 创建字符串缓冲区对象
StringBuffer sb = new StringBuffer();

// 添加元素
sb.append("hello").append("world").append("java");
System.out.println("sb:" + sb);

// 截取功能
// public String substring(int start)
String s = sb.substring(5);
System.out.println("s:" + s);
System.out.println("sb:" + sb);

// public String substring(int start,int end)
String ss = sb.substring(5, 10);
System.out.println("ss:" + ss);
System.out.println("sb:" + sb);
}
}

String和StringBuffer的相互转换?

示例:

public class StringBufferTest {
public static void main(String[] args) {
// String -- StringBuffer
String s = "hello";
// 注意:不能把字符串的值直接赋值给StringBuffer
// StringBuffer sb = "hello";
// StringBuffer sb = s;
// 方式1:通过构造方法
StringBuffer sb = new StringBuffer(s);
// 方式2:通过append()方法
StringBuffer sb2 = new StringBuffer();
sb2.append(s);
System.out.println("sb:" + sb);
System.out.println("sb2:" + sb2);
System.out.println("---------------");

// StringBuffer -- String
StringBuffer buffer = new StringBuffer("java");
// String(StringBuffer buffer)
// 方式1:通过构造方法
String str = new String(buffer);
// 方式2:通过toString()方法
String str2 = buffer.toString();
System.out.println("str:" + str);
System.out.println("str2:" + str2);
}
}

Integer
(1)为了让基本类型的数据进行更多的操作,Java就为每种基本类型提供了对应的包装类类型
byte         Byte
short Short
int Integer
long         Long
float         Float
double Double
char         Character
boolean Boolean
(2)Integer的构造方法
A:Integer i = new Integer(100);
B:Integer i = new Integer("100");
注意:这里的字符串必须是由数字字符组成
(3)String和int的相互转换
A:String -- int
Integer.parseInt("100");
B:int -- String
String.valueOf(100);
(4)其他的功能(了解)
进制转换
(5)JDK5的新特性
自动装箱 基本类型--引用类型
自动拆箱 引用类型--基本类型

Character(了解)
(1)Character构造方法
Character ch = new Character('a');
(2)要掌握的方法:
public static boolean isUpperCase(char ch):判断给定的字符是否是大写字符
  public static boolean isLowerCase(char ch):判断给定的字符是否是小写字符
  public static boolean isDigit(char ch):判断给定的字符是否是数字字符
  public static char toUpperCase(char ch):把给定的字符转换为大写字符
 
        public static char toLowerCase(char ch):把给定的字符转换为小写字符

示例:
public class CharacterDemo {
public static void main(String[] args) {
// public static boolean isUpperCase(char ch):判断给定的字符是否是大写字符
System.out.println("isUpperCase:" + Character.isUpperCase('A'));
System.out.println("isUpperCase:" + Character.isUpperCase('a'));
System.out.println("isUpperCase:" + Character.isUpperCase('0'));
System.out.println("-----------------------------------------");
// public static boolean isLowerCase(char ch):判断给定的字符是否是小写字符
System.out.println("isLowerCase:" + Character.isLowerCase('A'));
System.out.println("isLowerCase:" + Character.isLowerCase('a'));
System.out.println("isLowerCase:" + Character.isLowerCase('0'));
System.out.println("-----------------------------------------");
// public static boolean isDigit(char ch):判断给定的字符是否是数字字符
System.out.println("isDigit:" + Character.isDigit('A'));
System.out.println("isDigit:" + Character.isDigit('a'));
System.out.println("isDigit:" + Character.isDigit('0'));
System.out.println("-----------------------------------------");
// public static char toUpperCase(char ch):把给定的字符转换为大写字符
System.out.println("toUpperCase:" + Character.toUpperCase('A'));
System.out.println("toUpperCase:" + Character.toUpperCase('a'));
System.out.println("-----------------------------------------");
// public static char toLowerCase(char ch):把给定的字符转换为小写字符
System.out.println("toLowerCase:" + Character.toLowerCase('A'));
System.out.println("toLowerCase:" + Character.toLowerCase('a'));
}
}

Math
(1)针对数学运算进行操作的类
(2)常见方法
public static int abs(int a):绝对值
  public static double ceil(double a):向上取整
  public static double floor(double a):向下取整
  public static int max(int a,int b):最大值 (min自学)
  public static double pow(double a,double b):a的b次幂
  public static double random():随机数 [0.0,1.0)
  public static int round(float a) 四舍五入(参数为double的自学)
  public static double sqrt(double a):正平方根

示例:
public class MathDemo {
public static void main(String[] args) {
// public static final double PI
System.out.println("PI:" + Math.PI);
// public static final double E
System.out.println("E:" + Math.E);
System.out.println("--------------");

// public static int abs(int a):绝对值
System.out.println("abs:" + Math.abs(10));
System.out.println("abs:" + Math.abs(-10));
System.out.println("--------------");

// public static double ceil(double a):向上取整
System.out.println("ceil:" + Math.ceil(12.34));
System.out.println("ceil:" + Math.ceil(12.56));
System.out.println("--------------");

// public static double floor(double a):向下取整
System.out.println("floor:" + Math.floor(12.34));
System.out.println("floor:" + Math.floor(12.56));
System.out.println("--------------");

// public static int max(int a,int b):最大值
System.out.println("max:" + Math.max(12, 23));
// 需求:我要获取三个数据中的最大值
// 方法的嵌套调用
System.out.println("max:" + Math.max(Math.max(12, 23), 18));
// 需求:我要获取四个数据中的最大值
System.out.println("max:"
+ Math.max(Math.max(12, 78), Math.max(34, 56)));
System.out.println("--------------");

// public static double pow(double a,double b):a的b次幂
System.out.println("pow:" + Math.pow(2, 3));
System.out.println("--------------");

// public static double random():随机数 [0.0,1.0)
System.out.println("random:" + Math.random());
// 获取一个1-100之间的随机数
System.out.println("random:" + ((int) (Math.random() * 100) + 1));
System.out.println("--------------");

// public static int round(float a) 四舍五入(参数为double的自学)
System.out.println("round:" + Math.round(12.34f));
System.out.println("round:" + Math.round(12.56f));
System.out.println("--------------");

//public static double sqrt(double a):正平方根
System.out.println("sqrt:"+Math.sqrt(4));
}
}

Random(理解)
(1)用于产生随机数的类
(2)构造方法:
A:Random() 默认种子,每次产生的随机数不同
B:Random(long seed) 指定种子,每次种子相同,随机数就相同
(3)成员方法:
A:int nextInt() 返回int范围内的随机数
B:int nextInt(int n) 返回[0,n)范围内的随机数


System
(1)系统类,提供了一些有用的字段和方法
(2)成员方法
public static void gc():运行垃圾回收器。 
  public static void exit(int status):终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状
   态码表示异常终止。 
  public static long currentTimeMillis():返回以毫秒为单位的当前时间
  public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
  从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。

 BigInteger(理解)
(1)针对大整数的运算
(2)构造方法
A:BigInteger(String s)
(3)成员方法
  public BigInteger add(BigInteger val):加
  public BigInteger subtract(BigInteger val):减
 
        public BigInteger multiply(BigInteger val):乘
 
        public BigInteger divide(BigInteger val):除
 
        public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组

BigDecimal(理解)
(1)浮点数据做运算,会丢失精度。所以,针对浮点数据的操作建议采用BigDecimal。(金融相关的项目)
(2)构造方法
A:BigDecimal(String s)
(3)成员方法:

public BigDecimal add(BigDecimal augend)
  public BigDecimal subtract(BigDecimal subtrahend)
 
        public BigDecimal multiply(BigDecimal multiplicand)
 
        public BigDecimal divide(BigDecimal divisor)
  public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):商,几位小数,如何舍取
  

Date/DateFormat
(1)Date是日期类,可以精确到毫秒。
A:构造方法
Date()
Date(long time)
B:成员方法
getTime()
setTime(long time)
C:日期和毫秒值的相互转换
案例:你来到这个世界多少天了?
(2)DateFormat针对日期进行格式化和针对字符串进行解析的类,但是是抽象类,所以使用其子类                 SimpleDateFormat
A:SimpleDateFormat(String pattern) 给定模式
yyyy-MM-dd HH:mm:ss
B:日期和字符串的转换
a:Date -- String
format()

b:String -- Date
parse()
Calendar
(1)日历类,封装了所有的日历字段值,通过统一的方法根据传入不同的日历字段可以获取值。
(2)如何得到一个日历对象呢?
Calendar rightNow = Calendar.getInstance();
本质返回的是子类对象
(3)成员方法
A:根据日历字段得到对应的值
B:根据日历字段和一个正负数确定是添加还是减去对应日历字段的值
C:设置日历对象的年月日

0 0
原创粉丝点击