程序员学习 javaAPI常用类

来源:互联网 发布:windows编译php扩展 编辑:程序博客网 时间:2024/04/29 00:02


 



API-常用类


Object类概述及其构造方法

类层次结构的根类

所有类都直接或者间接的继承自该类

构造方法

public Object()

/*

 * Object类: 类 Object 是类层次结构的根类。每个类都使用 Object 作为超类,祖宗类

 * 

 * 构造方法:

 *  Object() 空参数构造方法

 * 

 * 方法:

 *  protected Object clone()  创建并返回此对象的一个副本

protected void finalize() 当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。

对象的垃圾回收器: public static void gc()运行垃圾回收器。 

调用 gc 方法暗示着 Java 虚拟机做了一些努力来回收未用对象,以便能够快速地重用这些对象当前占用的内存

public int hashCode() 返回该对象的哈希码值

我们可以理解为就是对象的地址值

但是,在java中我们是不能够获取到内存地址值,所以说这个hashCode()得到的地址值不是内存中真正的 地址

这个地址值其实叫哈希码值

哈希码值 是怎么计算出来的? 通过哈希算法计算出来的,具体如何计算,后面讲.

同一个对象的哈希码值是相同的

不同对象的哈希码值一般情况下不同

public final Class getClass()返回此 Object 的运行时类 [通过哪个.class文件创建的该对象]

通过Class类中的getClass()方法,可以获取到Class对象的名称

 */

public class ObjectDemo {

public static void main(String[] args) {

int i = 5;

System.out.println(i);

int[] arr = {1,2,3};

System.out.println(arr);

System.out.println("--------------------------");

//public int hashCode() 返回该对象的哈希码值

Student s = new Student();

System.out.println(s);

System.out.println(s.hashCode()  );//十进制

System.out.println( Integer.toHexString( s.hashCode()  )  );

Student s2 = new Student();

System.out.println(s2);

Student s3 = s;

System.out.println(s3);

System.out.println("--------------------------");

//public final Class<?> getClass()返回此 Object 的运行时类

Class c = s.getClass();

//public String getName()//获取名字

System.out.println(c.getName() 

 

 

 

public String toString()返回该对象的字符串表示

public boolean equals(Object obj)指示其他某个对象是否与此对象“相等”。

 * 

 *  == 与 equals的区别?

 *   

 *  == : 

 *   比较的基本数据类型: 比较的是数据的值是否相等

 *   比较的引用数据类型:比较的是哈希码值是否相等 

 *   

 *  equals:

 *   比较引用数据类型: 比较两个对象的内容是否相等

 *  

 *   默认的比较方式,采用的哈希码值比较,这种比较不是我们想要的

 *   建议所有的子类都重写此方法

 



String类 :

代表字符串。

 *  Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。

 * 

 * 字符串是常量;它们的值在创建之后不能更改;  因为 String 对象是不可变的,所以可以共享

 * 

 * 构造方法:

 *  public String(): 空参数构造方法

 *  public String(byte[] bytes) 把字节数组 转换成字符串 

 *  public String(byte[] bytes, int startIndex, int length) 把字节数组一部分 转换成字符串

 *  public String(char[] value) 把字符数组 转换成字符串

 *  public String(char[] value, int startIndex, int count)把字符数组一部分 转换成字符串

 *  public String(String original) 把字符串 转换成 字符串对象

 */

public class StringDemo {

public static void main(String[] args) {

//public String(): 空参数构造方法

String s1 = new String();

s1 = "hello";

System.out.println(s1);

System.out.println("------------");

//public String(byte[] bytes) 把字节数组 转换成字符串 

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

String s2 = new String(bys);

System.out.println(s2);

System.out.println("------------");

//public String(byte[] bytes, int startIndex, int length) 把字节数组一部分 转换成字符串

String s3 = new String(bys, 2, 2);//99,100

System.out.println(s3);

System.out.println("------------");

//public String(char[] value) 把字符数组 转换成字符串

char[] chs = {'a','b','c','d','e'};

String s4 = new String(chs);

System.out.println(s4);

System.out.println("------------");

//public String(char[] value, int startIndex, int count)把字符数组一部分 转换成字符串

String s5 = new String(chs, 0, 3);//abc

System.out.println(s5);

System.out.println("------------");

//public String(String original) 把字符串 转换成 字符串对象

String s6 = new String("haha");

System.out.println(s6);

System.out.println("------------");

//最简单的方式

String s7 = "hehe";

System.out.println(s7);

}

}

老师讲的几个面试题:

*

 * 面试题1

 *  String s = new String(hello)String s = hello;的区别?

 * 

 *  有区别。

 *  String s = new String(hello创建了两个对象,一个是new String()对象, 另一个是"hello"对象

 *  String s = "hello"; 创建了一个对象, "hello"对象

 *  

 * 面试题2

 *  String s1 = new String("hello");

String s2 = new String("hello");

System.out.println(s1==s2); //false

System.out.println(s1.equals(s2)); //true

 

String s3 = new String("hello");

String s4 = "hello";

System.out.println(s3==s4); //false

System.out.println(s3.equals(s4)); //true

String s5 = "hello";

String s6 = "hello";

System.out.println(s5==s6); //true

System.out.println(s5.equals(s6)); // true

 * 

 * 面试题3

 *  String s1 = "hello";

String s2 = "world";

String s3 = "helloworld";

System.out.println(s3 == s1+s2 ); // false

System.out.println(s3 == "hello" + "world");//true

System.out.println(s3.equals(s1+s2)); //true

 * 

 */

public class StringDemo3 {

public static void main(String[] args) {

/*

String s = new String("hello");

String s2 = "hello";

System.out.println(s == s2);

*/

/*

String s1 = new String("hello");

String s2 = new String("hello");

System.out.println(s1==s2); //false

System.out.println(s1.equals(s2)); //true

 

String s3 = new String("hello");

String s4 = "hello";

System.out.println(s3==s4); //false

System.out.println(s3);


  String类的方法:

 

 *  判断功能:

 *  boolean equals(Object obj):判断两个字符串内容是否相等,重写了Object类中的equals方法

 * boolean equalsIgnoreCase(String str):判断两个字符串内容是否相等,忽略大小写

 * boolean contains(String str): 判断当前字符串 是否包含 给定的字符串

 * boolean startsWith(String str): 判断当前字符串 是否以给定的字符串开头

 * boolean endsWith(String str) : 判断当前字符串 是否以给定的字符串结束

 * boolean isEmpty(): 判断当前字符串是否为 空字符串   ,  " " 空字符串

 */

public class StringMethodDemo {

public static void main(String[] args) {

String str = "hello";

//boolean equals(Object obj)

System.out.println( "equals:" + str.equals("hello") );

System.out.println( "equals:" + str.equals("Tom") );

System.out.println( "equals:" + str.equals("Hello") );

System.out.println("--------------------------------");

//boolean equalsIgnoreCase(String str)

System.out.println( "equalsIgnoreCase:" + str.equalsIgnoreCase("Hello") );

System.out.println("--------------------------------");

//boolean contains(String str)

System.out.println( "contains:" + str.contains("llo") );

System.out.println( "contains:" + str.contains("Tom") );

System.out.println( "contains:" + str.contains("hello") );

System.out.println( "contains:" + str.contains("Hello") );

System.out.println("--------------------------------");

//boolean startsWith(String str)

System.out.println( "startsWith:" + str.startsWith("he") );

System.out.println( "startsWith:" + str.startsWith("He") );

System.out.println("--------------------------------");

//boolean endsWith(String str)

System.out.println( "endsWith:" + str.endsWith("he") );

System.out.println( "endsWith:" + str.endsWith("llo") );

System.out.println("--------------------------------");

//boolean isEmpty()

// "" 空字符串

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

System.out.println( "isEmpty: " + str.isEmpty() );

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

//System.out.println( "isEmpty: " + null.isEmpty() );

package cn.itcast_05_StringMethod;

/*

 * String类中的获取功能

 *  int length() : 获取当前字符串的长度

char charAt(int index): 获取当前字符串中,给定索引位置对应的字符

int indexOf(int ch) : 获取当前字符串中, 给定的字符第一次出现的位置,没有找到返回-1

int indexOf(String str): 获取当前字符串中, 给定的字符串第一次出现的位置,没有找到返回-1

int indexOf(int ch,int fromIndex)从指定的位置开始, 获取当前字符串中, 给定的字符第一次出现的位置,没有找到返回-1

int indexOf(String str,int fromIndex)从指定的位置开始,获取当前字符串中, 给定的字符串第一次出现的位置,没有找到返回-1

String substring(int start) 从指定的位置开始,截取当前字符串到末尾,返回一个新的字符串 

String substring(int start,int end)从指定位置开始,到指定位置结束,截取当前字符串,返回一个新的字符串

注意:substring(int start,int end) 包含起始位置字符,不包含结束位置字符

 */

public class StringMethodDemo {

public static void main(String[] args) {

String str = "HelloWorld";

//int length() : 获取当前字符串的长度

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

System.out.println("------------------------");

//char charAt(int index): 获取当前字符串中,给定索引位置对应的字符

//java.lang.StringIndexOutOfBoundsException 字符串角标越界异常

//java.lang.ArraayIndexOutOfBoundsException 数组角标越界异常

//System.out.println( "charAt:" + str.charAt(10) );字符串角标越界异常

System.out.println( "charAt:" + str.charAt(5) );

System.out.println("------------------------");

//int indexOf(int ch) : 获取当前字符串中, 给定的字符第一次出现的位置

System.out.println( "indexOf:" + str.indexOf('e') );

System.out.println( "indexOf:" + str.indexOf(101) );//e 101

System.out.println("------------------------");

//int indexOf(String str): 获取当前字符串中, 给定的字符串第一次出现的位置

System.out.println( "indexOf:" + str.indexOf("ll") );

System.out.println( "indexOf:" + str.indexOf("World") );

System.out.println( "indexOf:" + str.indexOf("world") );//-1 

System.out.println("------------------------");

//int indexOf(int ch,int fromIndex)从指定的位置开始, 获取当前字符串中, 给定的字符第一次出现的位置,没有找到返回-1

System.out.println( "indexOf:" + str.indexOf('o', 0) );

System.out.println( "indexOf:" + str.indexOf('o', 5) );

System.out.println("------------------------");

//int indexOf(String str,int fromIndex)从指定的位置开始,获取当前字符串中, 给定的字符串第一次出现的位置,没有找到返回-1

System.out.println( "indexOf:" + str.indexOf("Wo", 3) );

System.out.println( "indexOf:" + str.indexOf("Wo", 6) );

System.out.println("------------------------");

//String substring(int start) 从指定的位置开始,截取当前字符串到末尾,返回一个新的字符串 

//String str = "HelloWorld";

String result = str.substring(5);//World

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

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

System.out.println("------------------------");

//String substring(int start,int end)从指定位置开始,到指定位置结束,截取当前字符串,返回一个新的字符串

result = str.substring(2, 5);

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

}

}


 String类的转换功能:

 * 

 *  byte[] getBytes() 把当前字符串 转换成 字节数组  

char[] toCharArray() 把当前字符串转换成 字符数组

static String valueOf(char[] chs) 静态方法,把给定的字符数组 转换成字符串

static String valueOf(int i) 静态方法,把基本数据类型 转换成 字符串

String toLowerCase() 把当前字符串全部转换为 小写字母

String toUpperCase() 把当前字符串全部转换为 大写字母

String concat(String str) 把当前字符串 与 给定的字符串进行 拼接

 

 

 */

public class StringMethodDemo {

public static void main(String[] args) {

String s = "AbCdE";

// byte[] getBytes() 把当前字符串 转换成 字节数组  

byte[] bys = s.getBytes();

for (int i = 0; i < bys.length; i++) {

System.out.println(bys[i]);

 

package cn.itcast_07_StringMethod;


 替换功能

String replace(char old,char new) 把当前字符串中,给定的旧字符,用新字符替换

String replace(String old,String new) 把当前字符串中,给定的旧字符串,用新字符串替换

去除字符串两端空格

String trim()

按自然顺序比较两个字符串  

int compareTo(String str) 比较字符串的大小

int compareToIgnoreCase(String str)  比较字符串的大小,忽略大小写

hello

iello

"hello".compareTo("iello");

结果: 

   大于0:当前字符串 比 给定字符串大

   等于0:当前字符串与 给定字符串一样

   小于0:当前字符串 比 给定字符串小

   

比较大小规则:   

  a<b<c<d<e...

  1<2<3<4....  

  数字 大写字母 小写字母  按照ASCII值比较

比较的流程:

使用两个字符串的第一位进行大小比较,

如果结果不为0,比较结束

如果结果为0,使用两个字符串下一位进行大小比较

  如果结果不为0,比较结束

  如果结果为0,继续下一位比较

 

  结束条件:

  要么比较的结果不为0

  要么比较到了两个字符串的末尾,还相同,返回0

 */

public class StringMethodDemo {

public static void main(String[] args) {

String str = "HelloJava";

//String replace(char old,char new) 把当前字符串中,给定的旧字符,用新字符替换

String result = str.replace('a', 'E');

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

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

System.out.println("--------------------------");

//String replace(String old,String new) 把当前字符串中,给定的旧字符串,用新字符串替换

String result2 = str.replace("Java", "haha");

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

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

System.out.println("--------------------------");

String s = "     hello java     ";

String newStr = s.trim();

System.out.println("---"+ s +"---");

System.out.println("---"+ newStr +"---");

System.out.println("--------------------------");

//int compareTo(String str) 比较字符串的大小

//String s1 = "abcde";//97

//String s2 = "bbcde";//98

//System.out.println( s1.compareTo(s2) );//-1

// String s1 = "abcbb";

// String s2 = "abcaa";

// System.out.println( s1.compareTo(s2)  );//1

// String s1 = "abc";

// String s2 = "abc";

// System.out.println( s1.compareTo(s2)  );//0

// String s1 = "Abc";//65

// String s2 = "abc";//97

// System.out.println( s1.compareTo(s2)  );//-32

String s1 = "abc123456789";//

String s2 = "abc";//

System.out.println( s1.compareTo(s2)  );//-32

System.out.println("--------------------------");

//int compareToIgnoreCase(String str)  比较字符串的大小,忽略大小写

s1 = "Abc";//65

s2 = "abc";//97

System.out.println( s1.compareToIgnoreCase(s2)  );//0

}

}

 

举例 :

/*

 * 统计大串中小串出现的次数

举例:在字符串”woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun”中java出现了5

分析:

1: 定义字符串

 

2:定义变量count,用来记录Java出现的次数 

3:在字符串中找"java",获取到位置int index =str.indexOf("java")

       找到了(位置不是-1)

a: count++

        b: 截取字符串subString(位置

                    位置 = index + "java".length()

c: 将返回的新字符串,覆盖原有字符串

d: 回到 步骤继续

       没找到(位置是 -1)

a: 结束循环  

4:打印次数

 */

public class StringTest {

public static void main(String[] args) {

//1: 定义字符串

String str = "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";//大串

String key = "java";//小串

//2:定义变量count,用来记录Java出现的次数 

int count = 0;

while (true) {

//3:在字符串中找"java",获取到位置int index =str.indexOf("java")

int index = str.indexOf(key);

if (index != -1) {

 //  找到了java

count++;

//位置 = index + "java".length(),将返回的新字符串,覆盖原有字符串

str = str.substring(index + key.length());

} else {

// 没找到(位置是 -1)

//a: 结束循环  

break;

}

}

//4:打印次数

System.out.println("java出现的次数: " + count);

}

StringBuffer: 字符串缓冲区

 *  特点:StringBuffer 线程安全的类  -- 代码安全 --不会出错 -- 效率低

 * 

 * String 与 StringBuffer的区别?

 *  String: 长度固定,内容固定

 *  StringBuffer: 长度可变,内容可变

 * 

 * 构造方法:

 *  public StringBuffer() 构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符。

 *  public StringBuffer(int capacity) 构造一个不带字符,但具有指定初始容量的字符串缓冲区。 

 *  public StringBuffer(String str)构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容。

 *   该字符串的初始容量为 16 加上字符串参数的长度。

 * 方法:

 *  public int capacity()返回当前容量 (框的容量)

 *  public int length() 返回长度(字符数)。  (框中实际使用的容量)

 */

public class StringBufferDemo {

public static void main(String[] args) {

//public StringBuffer()

StringBuffer sb = new StringBuffer();

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

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

System.out.println("----------------------");

//public StringBuffer(int capacity)

StringBuffer buffer = new StringBuffer(100);

System.out.println("capacity:"+ buffer.capacity());

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

System.out.println("----------------------");

//public StringBuffer(String str)

StringBuffer buffer2 = new StringBuffer("hello");

System.out.println("capacity:"+ buffer2.capacity());

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

}

}

 

添加功能

public StringBuffer append(String str) : 追加,在原有数据的基础上,添加新数据

将指定的参数类型数据[任意类型数据],添加到当前的字符串缓冲区对象中,返回当前字符串缓冲区对象

public StringBuffer insert(int offset,String str)

在指定的位置上,将指定的参数类型数据[任意类型数据],插入到当前的字符串缓冲区对象中,返回当前字符串缓冲区对象

方法: public String toString()返回此序列中数据的字符串表示形式。

 

 删除功能

public StringBuffer deleteCharAt(int index)

把指定位置上的字符,在当前的字符串缓冲区对象中删除,返回当前字符串缓冲对象

public StringBuffer delete(int start,int end)

从指定位置开始,到指定位置结束,在当前的字符串缓冲区对象中删除,返回当前字符串缓冲对象

包左不包右

替换功能

public StringBuffer replace(int start,int end,String str) 包左不包右

从指定位置开始,到指定位置结束,用给定的字符串,将字符串缓冲区中的数据替换,返回当前的字符串缓冲区对象

        反转功能  

        public StringBuffer reverse()//反转,返回当前的字符串缓冲区对象

 

 */

public class StringBufferMethod3 {

public static void main(String[] args) {

StringBuffer buffer = new StringBuffer();

buffer.append("Hello");

buffer.append("JavaWeb");

buffer.append("JavaEE");

buffer.append("Android");

//public StringBuffer replace(int start,int end,String str)

buffer.replace(0, 5, "JavaSE");

//public StringBuffer reverse()

StringBuffer buffer2 = new StringBuffer("123");

buffer2.reverse();

System.out.println(buffer);

System.out.println(buffer2);

}

}

/*

 * StringBuffer截取功能

public String substring(int start)

从指定位置开始到最后,截取该字符串缓冲区的数据,返回字符串对象

public String substring(int start,int end)

从指定位置开始到指定位置结束,截取该字符串缓冲区的数据,返回字符串对象

截取功能和前面几个功能的不同

返回值类型是String类型,本身没有发生改变

 

 

 

 */

public class StringBufferMethod4 {

public static void main(String[] args) {

StringBuffer buffer = new StringBuffer();

//添加数据

buffer.append(false);

buffer.append("Java");

buffer.append("SSH");

buffer.append("JNI");

// public String substring(int start);

// String result = buffer.substring(9);

//public String substring(int start,int end)

String result = buffer.substring(9, 12);

System.out.println(buffer);

System.out.println(result);

package cn.itcast_04_StringBuffer_Test;

/*

 * StringStringBuffer的相互转换

 * 

 *  StringBuffer  ---> String

 *  通过String类的构造方法

 *  public String(StringBuffer buffer)

 *  通过String类的方法

 *  public static String valueOf(Object obj)

 *  通过StringBuffer类的方法

 *  public String toString()

 * 

 *  String -----> StringBuffer

 *  通过StringBuffer类的构造方法

 *  public StringBuffer(String str)

 *  通过StringBuffer类的方法

 *  public StringBuffer append(String str)

 *  public StringBuffer insert(int offset, String str)

 * 

 */

public class StringBufferTest {

public static void main(String[] args) {

//StringBuffer  ---> String

StringBuffer sb = new StringBuffer("JavaSE");

//方式1: 通过String类的构造方法,  public String(StringBuffer buffer)

//String str = new String( sb );

//方式2: 通过String类的方法, public static String valueOf(Object obj)

//String str = String.valueOf(sb);

//方式3:通过StringBuffer类的方法 , public String toString()

String str = sb.toString();

System.out.println(str);

 

System.out.println("----------------------------------");

//String -----> StringBuffer

String s = "Hello";

//方式1: 通过StringBuffer类的构造方法, public StringBuffer(String str)

//StringBuffer buffer2 = new StringBuffer( s );

//方式2: 通过StringBuffer类的方法, public StringBuffer append(String str)

StringBuffer buffer2 = new StringBuffer();

buffer2.append("haha");

System.out.println(buffer2);

}

}

import java.util.Arrays;

 
StringBuilder:

一个可变的字符序列。此类提供一个与 StringBuffer 兼容的 API,但不保证同步


String,StringBuffer,StringBuilder的区别

String: 
长度固定,内容不可变
StringBuffer: v1.0版本
长度可变,内容可变, 线程同步 --代码运行安全 -- 效率低
StringBuilder:v1.5版本
长度可变,内容可变,  线程不同步 -- 代码运行有隐患 -- 效率高



Arrays类概述

针对数组进行操作的工具类。

提供了排序,查找等功能。

成员方法

public static String toString(int[] a) 把各种数组 转换为字符串

public static void sort(int[] a) 把各种数组 进行元素排序

public static int binarySearch(int[] a,int key) 在各种类型数组中,进行执行数值的查找,折半查找方式(二分查找法)

注意:该方法的使用,前提要求 给定的数组是一个有序的数组

 

 

 */

public class ArraysDemo {

public static void main(String[] args) {

int[] arr = {1,5,9,3,7};

 

//public static String toString(int[] a) 把各种数组 转换为字符串

String result = Arrays.toString(arr);

System.out.println(result);//[1, 5, 9, 3, 7]

//public static void sort(int[] a) 把各种数组 进行元素排序

Arrays.sort(arr);

System.out.println("排序后:" + Arrays.toString(arr));//排序后:[1, 3, 5, 7, 9]

//public static int binarySearch(int[] a,int key)

// 前提要求 给定的数组是一个有序的数组  折半查找方式(二分查找法)

//int index = Arrays.binarySearch(arr, 7);

//int index = Arrays.binarySearch(arr, 9);

//System.out.println("arr数组中9的位置:" + index);

//int index = Arrays.binarySearch(arr, 0);//-1

//int index = Arrays.binarySearch(arr, 2);//-2

int index = Arrays.binarySearch(arr, 4);//-3

System.out.println("arr数组中4数据对应的位置:" + index);

}

}


 Character中方法
 
        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 CharacterMethod {
public static void main(String[] args) {
System.out.println("isUpperCase:"+ Character.isUpperCase('A'));
System.out.println("isUpperCase:"+ Character.isUpperCase('a'));
System.out.println("-----------------------");

System.out.println("isLowerCase:"+ Character.isLowerCase('A'));
System.out.println("isLowerCase:"+ Character.isLowerCase('a'));
System.out.println("-----------------------");

System.out.println("isDigit:"+ Character.isDigit('A'));
System.out.println("isDigit:"+ Character.isDigit('a'));
System.out.println("isDigit:"+ Character.isDigit('5'));
System.out.println("-----------------------");

System.out.println("toUpperCase:"+ Character.toUpperCase('a'));
System.out.println("toUpperCase:"+ Character.toUpperCase('b'));
System.out.println("-----------------------");

System.out.println("toLowerCase:"+ Character.toLowerCase('A'));
System.out.println("toLowerCase:"+ Character.toLowerCase('B'));
System.out.println("-----------------------");
}

}





 * 

Math类概述
Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。 
成员方法
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() : 随机数
public static int round(float a) 参数为double的自学    四舍五入
public static double sqrt(double a) : 正平方根




 Random类概述
此类用于产生随机数
如果用相同的种子创建两个 Random 实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
构造方法
public Random() : 使用默认的种子生成随机数  [默认的种子是当前时间的毫秒值]
public Random(long seed): 指定给定的种子产生随机数

方法:
public int nextInt() : 获取int范围内的随机数
public int nextInt(int n): 获取0到n之间的随机数 [不包含n]
 */
public class RandomDemo {
public static void main(String[] args) {
//创建对象
//Random r = new Random();
Random r = new Random( 1234 );

for (int i = 0; i < 10; i++) {
//System.out.println(  r.nextInt()  );
System.out.println(  r.nextInt(100)  );
}
}
}




/*
System类概述
System 类包含一些有用的类字段和方法。它不能被实例化。 
成员方法
public static void gc()
public static void exit(int status)
public static long currentTimeMillis()
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)


 */
public class SystemDemo {
public static void main(String[] args) {
//System.in  键盘输入   标准输入流
//System.out 控制台       标准输出流
//System.err 控制台       标准错误流  error
//System.err.println("这是什么效果");

Person p = new Person("周瑜", 28);
System.out.println(p);

p = null;
System.gc();
}
}

System类概述
System 类包含一些有用的类字段和方法。它不能被实例化。 
成员方法
public static void gc() : 垃圾回收器
public static void exit(int status) : 终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。
public static long currentTimeMillis() : 得到当前系统时间 与 1970-01-01 00:00:00 之间的差值的 毫秒值
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)





  Date:类

 Date 表示特定的瞬间,精确到毫秒。 
 
 * 构造方法:
 * public Date() : 使用系统当前时间的毫秒值 来创建 日期对象
 * public Date(long date) : 使用指定的毫秒值 来创建 日期对象
 * 参数date: 自 1970 年 1 月 1 日 00:00:00 GMT 以来的毫秒数
 */
public class DateDemo {
public static void main(String[] args) {
//Date date = new Date();
//System.out.println( date );

//public Date(long date)
Date date2 = new Date(1000);
System.out.println( date2 );
}
}




 
DateFormat: 日期格式化类
 
 * 格式化(也就是日期 -> 文本)
 * public final String format(Date date) : 将一个 Date 格式化为日期/时间字符串。
解析(文本-> 日期)
public Date parse(String source) : 从给定字符串的开始解析文本,以生成一个日期


SimpleDateFormat: 日期格式化类的子类
构造方法:
public SimpleDateFormat(): 空参数构造方法
public SimpleDateFormat(String pattern)
用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat


日期模式:
y  年  
M  年中的月份 
d  月份中的天数 
H  一天中的小时数(0-23) 
m  小时中的分钟数 
s  分钟中的秒数 
 */
public class DateFormatDemo {


public static void main(String[] args) throws ParseException {
//method1();

method2();
}


//解析(文本-> 日期)
private static void method2() throws ParseException {
//定义日期的字符串
String time = "2015-12-12 12:12:12";
//创建日期格式化对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//文本-> 日期
Date date = sdf.parse(time);
System.out.println(date);
}


//格式化(也就是日期 -> 文本)
public static void method1() {
//格式化(也就是日期 -> 文本)
Date date = new Date();
//创建日期格式化类对象
//SimpleDateFormat sdf = new SimpleDateFormat();
//SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH时mm分ss秒");
//日期 -> 文本
String time = sdf.format(date);
System.out.println(time);
}
}

       

0 0
原创粉丝点击