Java复习,Java知识点以及Java面试题(三)

Object

   类层次结构的跟类,每个类都使用Object作为超类,每个类都直接或者间接继承于Object

  构造方法 public Object()

  成员方法 :

    public int hashCode():返回该对象的哈希码值,哈希值是根据哈希算法计算出来,于地址值有关

    public final Class getClass(): 返回此Object类的运行时类 

    toString() 返回对象的字符串表示,默认是由类的全路径+'@'+哈希值的十六进制表示。 这个表示其实是没有意义的,一般子类都会重写该方法。 可以自动生成    

    equals()比较两个对象是否相同。默认情况下,比较的是地址值是否相同。
而比较地址值是没有意义的,所以,一般子类也会重写该方法。可以自动生成。

    finalize() 用于垃圾回收,在不确定的时间

    clone() 可以实现对象的克隆,包括成员变量的数据复制,但是它和两个引用指向同一个对象是有区别的。

    直接输出一个对象名称,其实默认调用了该对象的toString()方法。

  面试题
    ==和equals()的区别?
    A:==
      基本类型:比较的是值是否相同
      引用类型:比较的是地址值是否相同
    B:equals()
      只能比较引用类型。默认情况下,比较的是地址值是否相同。
      但是,我们可以根据自己的需要重写该方法。

String

  构造方法:public String()
       public String(byte[] bytes)
       public String(byte[] bytes,int offset,int length)
       public String(char[] value)
       public String(char[] value,int offset,int count)
       public String(String original)
  面试题:

      字面值作为字符串对象和通过构造方法创建对象的不同

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

      前者创建两个对象后者创建一个对象

 1 /*
 2  * 看程序写结果
 3  */
 4 public class StringDemo3 {
 5     public static void main(String[] args) {
 6         String s1 = new String("hello");
 7         String s2 = new String("hello");
 8         System.out.println(s1 == s2);// false
 9         System.out.println(s1.equals(s2));// true
10 
11         String s3 = new String("hello");
12         String s4 = "hello";
13         System.out.println(s3 == s4);// false
14         System.out.println(s3.equals(s4));// true
15 
16         String s5 = "hello";
17         String s6 = "hello";
18         System.out.println(s5 == s6);// true
19         System.out.println(s5.equals(s6));// true
20     }
21 }

 

 1 /*
 2  * 看程序写结果
 3  * 字符串如果是变量相加,先开空间,在拼接。
 4  * 字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。
 5  */
 6 public class StringDemo4 {
 7     public static void main(String[] args) {
 8         String s1 = "hello";
 9         String s2 = "world";
10         String s3 = "helloworld";
11         System.out.println(s3 == s1 + s2);// false 字符串拼接是先开了一个空间(X002)然后在进行拼接而不是直接指向s3所指向的那个位置(X001)(方法区的常量池中).所以地址值不一样,false。
12         System.out.println(s3.equals((s1 + s2)));// true
13 
14         System.out.println(s3 == "hello" + "world");// false 这个我们错了,应该是true
15         System.out.println(s3.equals("hello" + "world"));// true
16 
17         // 通过反编译看源码,我们知道这里已经做好了处理。
18         // System.out.println(s3 == "helloworld");
19         // System.out.println(s3.equals("helloworld"));
20     }
21 }

 

  与判断功能相关的方法

    boolean equals(object obj): 

    boolean equalsIgnoreCase(String str):

    boolean contains(String str):

    boolean startwith(String str):

    boolean endswith(String str):

    boolean isEmpty();

 

  与获取相关的方法

    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):从指定位置开始到指定位置结束截取字符串。

 

字符串的遍历:

  1. charAt()获取每个字符,length()用来循环

  2.  toCharArray() 转换成字符数组,在对数组进行遍历

  

  与转换相关的方法

    byte[] getBytes():把字符串转换为字节数组。

    char[] toCharArray():把字符串转换为字符数组。

    static String valueOf(char[] chs):把字符数组转成字符串。

    static String valueOf(int i):把int类型的数据转成字符串。

    String toLowerCase():把字符串转成小写。

    String toUpperCase():把字符串转成大写。

    String concat(String str):把字符串拼接。

 

  其他常用方法

     String replace(char old, char new)

     String replace(String old, String new)

     String trim()

     int compateTo(String str)

     int compareToIgnoreCase(String str)

 

StringBuffer

  线程安全的可变字符串

  StringBuffer和String的区别? 前者长度和内容可变,后者不可变。如果使用前者做字符串的拼接,不会浪费太多的资源。

  构造方法:StringBuffer的构造方法:

         public StringBuffer(): 

         public StringBuffer(int capacity): 

         public StringBuffer(String str): 

  StringBuffer的方法:

    public int capacity():

    public int length():

    添加:public StringBuffer append(String str): 把任意类型数据添加到字符串缓冲区里,并返回字符串缓冲区本身。

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

    删除: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 String substring(int start):

       public String substring(int start, int end):

 

String和StringBuffer相互转换:

  StringBuffer的构造方法 public StringBuffer(String str):

  StringBuffer的append方法; 

  String的构造方法 public String(StringBuffer buffer):

  StringBuffer的toString方法;

 

StringBuilder

  线程不安全的可变字符串

 

  面试题:

    String, StringBuffer, StringBuilder的区别?

      String是内容不可变的,而StringBuffer,StringBuilder都是内容可变的

      StringBuffer是同步的,数据安全,效率低;StringBuilder是不同步的,数据不安全,效率高

    StringBuffer和数组的区别?

      两者都可以看出是一个容器,装其他的数据

      但是StringBuffer的数据最终是一个字符串数据

      而数组可以放置多种数据,但是必须是同一种数据类型

    

 1 /* 形式参数问题
 2  * String作为参数传递
 3  * StringBuffer作为参数传递 
 4  * 
 5  * 形式参数:
 6  *         基本类型:形式参数的改变不影响实际参数
 7  *         引用类型:形式参数的改变直接影响实际参数
 8  * 
 9  * 注意:
10  *         String作为参数传递,虽然String是引用类型但是效果和基本类型作为参数传递是一样的。
11  *        SreingBuffer运用赋值符号时形式参数的改变不影响实际参数。运用append()方法时,形式参数的改变直接影响实际参数
12  */
13 public class StringBufferDemo {
14     public static void main(String[] args) {
15         String s1 = "hello";
16         String s2 = "world";
17         System.out.println(s1 + "---" + s2);// hello---world
18         change(s1, s2);
19         System.out.println(s1 + "---" + s2);// hello---world
20 
21         StringBuffer sb1 = new StringBuffer("hello");
22         StringBuffer sb2 = new StringBuffer("world");
23         System.out.println(sb1 + "---" + sb2);// hello---world
24         change(sb1, sb2);
25         System.out.println(sb1 + "---" + sb2);// hello---worldworld
26 
27     }
28 
29     public static void change(StringBuffer sb1, StringBuffer sb2) {
30         sb1 = sb2;
31         sb2.append(sb1);
32     }
33 
34     public static void change(String s1, String s2) {
35         s1 = s2;
36         s2 = s1 + s2;
37     }
38 }

 

Arrays

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

  public static String toString(int[] a):

  public static void sort(int[] a):  底层快排实现

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

基本类型包装类

  Integer

    构造方法:public Integer (int value)

         public Integer (String s)

 1 /*
 2  * int类型和String类型的相互转换
 3  * 
 4  * int -- String
 5  *         String.valueOf(number)
 6  * 
 7  * String -- int
 8  *         Integer.parseInt(s)
 9  */
10 public class IntegerDemo {
11     public static void main(String[] args) {
12         // int -- String
13         int number = 100;
14         // 方式1
15         String s1 = "" + number;
16         System.out.println("s1:" + s1);
17         // 方式2
18         String s2 = String.valueOf(number);
19         System.out.println("s2:" + s2);
20         // 方式3
21         // int -- Integer -- String
22         Integer i = new Integer(number);
23         String s3 = i.toString();
24         System.out.println("s3:" + s3);
25         // 方式4
26         // public static String toString(int i)
27         String s4 = Integer.toString(number);
28         System.out.println("s4:" + s4);
29         System.out.println("-----------------");
30 
31         // String -- int
32         String s = "100";
33         // 方式1
34         // String -- Integer -- int
35         Integer ii = new Integer(s);
36         // public int intValue()
37         int x = ii.intValue();
38         System.out.println("x:" + x);
39         //方式2
40         //public static int parseInt(String s)
41         int y = Integer.parseInt(s);
42         System.out.println("y:"+y);
43     }
44 }

   面试题

 1 /*
 2  * 看程序写结果
 3  * 
 4  * 注意:Integer的数据直接赋值,如果在-128到127之间,会直接从缓冲池里获取数据,否则就创建一个新的空间
 5  */
 6 public class IntegerDemo {
 7     public static void main(String[] args) {
 8         Integer i1 = new Integer(127);
 9         Integer i2 = new Integer(127);
10         System.out.println(i1 == i2);
11         System.out.println(i1.equals(i2));
12         System.out.println("-----------");
13 
14         Integer i3 = new Integer(128);
15         Integer i4 = new Integer(128);
16         System.out.println(i3 == i4);
17         System.out.println(i3.equals(i4));
18         System.out.println("-----------");
19 
20         Integer i5 = 128;
21         Integer i6 = 128;
22         System.out.println(i5 == i6);          //false
23         System.out.println(i5.equals(i6));    //true
24         System.out.println("-----------");
25 
26         Integer i7 = 127;
27         Integer i8 = 127;
28         System.out.println(i7 == i8);        //true
29         System.out.println(i7.equals(i8));    //true
30 
31         // 通过查看源码,我们就知道了,针对-128到127之间的数据,做了一个数据缓冲池,如果数据是该范围内的,每次并不创建新的空间
32         // Integer ii = Integer.valueOf(127);
33     }

 

  Byte

  Short

  Long

  Float

  Double

  Character

    构造方法:public Character(char value)

    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):把给定的字符转换为小写字符

  Boolean

 

正则表达式:

  Pattern类:正则表达式的编译表示形式

  正则表达式规则:

A:字符
    x 字符 x。举例:'a'表示字符a
    \\ 反斜线字符("\")。
    \n 新行(换行)符 ('\u000A') 
    \r 回车符 ('\u000D')
    
B:字符类
    [abc] a、b 或 c (3者选其一)(简单类) 
    [^abc] 任何字符,除了 a、b 或 c(否定) 
    [a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围) 
    [0-9] 0到9的字符都包括
    
C:预定义字符类
    . 任何字符。我的就是.字符本身,怎么表示呢? \.
    \d 数字:[0-9]
    \w 单词字符:[a-zA-Z_0-9]
        在正则表达式里面组成单词的东西必须有这些东西组成

D:边界匹配器
    ^ 行的开头 
    $ 行的结尾 
    \b 单词边界
        就是不是单词字符([a-zA-Z_0-9])的地方。
        举例:hello world?haha;xixi
    
E:Greedy 数量词 
    X? X,一次或一次也没有
    X* X,零次或多次
    X+ X,一次或多次
    X{n} X,恰好 n 次 
    X{n,} X,至少 n 次 
    X{n,m} X,至少 n 次,但是不超过 m 次 

   判断:String类的public boolean matches(String regex):

   分割:String类的public String[] split(String regex):

   替换:String类的public String replaceAll(String regex,String replacement)

   Pattern和matcher的使用

 

Math

  成员变量:

    public static final double PI

    public static final double E

  成员方法

    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):返回最大值

    public static double pow(double a, double b):平方根

    public static double random():产生0到1随机数

    public static int round(float a):四舍五入

    public static double sqrt(double a):正平方根

 

Random

  构造方法:public Random(): 没有给种子,用的是默认种子,是当前时间的毫秒值

       public Random(long seed): 给出指定的种子 给定种子后随机出来的数是固定的

   成员方法:public int nextInt():

        public int nextInt(int n):

 

System

  无构造方法,不能被实例化

  成员方法:public static void gc():  运行垃圾回收器 

       public static void exit(int status): 终止当前正在运行的java虚拟机,参数用作状态码;根据惯例,非 0 的状态码表示异常终止。即表示0状态码表示正常终止。

       public static long currentTimeMillis():返回以毫秒为单位的当前时间[可以用来计算程序运行的时间]    

       public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length): 用于将一个数组的元素快速拷贝到另一个数组

  

BigInteger

  构造方法:BigInteger(String val)

  成员方法:

    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

  由于在运算的时候,float类型和double很容易丢失精度,为了能精确的表示、计算浮点数,Java提供了BigDecimal。BigDecimal类:不可变的、任意精度的有符号十进制数,可以解决数据丢失问题。

  构造方法:public BigDecimal(String val)

  成员方法: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

  构造方法:Date():根据当前的默认毫秒值创建日期对象

      Date(long date):根据给定的毫秒值创建日期对象

  成员方法:public long getTime(): 获取时间,以毫秒为单位

       public void setTime(long time):设置时间

  

DateFormat

  Date -- String(格式化)
    public final String format(Date date)
  String -- Date(解析)
    public Date parse(String source)

  DateForamt:可以进行日期和字符串的格式化和解析,但是由于是抽象类,所以使用具体子类SimpleDateFormat。

  SimpleDateFormat的构造方法:
    SimpleDateFormat():默认模式
    
SimpleDateFormat(String pattern):给定的模式(该模式为:年 y月 M 日 d时 H分 m秒 s)

 

Calendar

  抽象类,使用Calender.getInstance()获取子类对象

  public int get (int field): 返回给定日历字段的值,日历类中的每个日历字段都是静态的成员变量,并且是int类型。   

  public void add(int field,int amount):根据给定的日历字段和对应的时间,来对当前的日历进行操作。

  public final void set(int year,int month,int date):设置当前日历的年月日

  

posted @ 2020-09-14 18:36  生于思考  阅读(136)  评论(0编辑  收藏  举报