黑马程序员_Java-API.1

来源:互联网 发布:2016淘宝店开店流程 编辑:程序博客网 时间:2024/05/22 15:44

------- android培训、java培训、期待与您交流! ----------


Java-API.1

一:Object类

1.toString()方法

(1).思想

     用输出语句直接输出对象的名称,其实底层调用了该对象的toString()方法。如果我们认为某个类应该有某个功能,而我们在这个类中确没有方法,这个时候,就应该在其父类中查找。


(2).注意:任何类都直接或者间接继承自Object类。


(3).格式:

               public String toString():返回对象的字符串表示形式。


(4).字符串的组成:

                              getClass().getName() + '@' + Integer.toHexString(hashCode())

A.public final Class<?> getClass():返回的Object类的运行时类。(反射)


B.Class类下面有一个方法:public String getName() 返回某个类的完整路径名称。包括包名。


C.Integer类有一个静态方法:

                                                public static String toHexString(int i)

                                                返回给定参数的十六进制表示的字符串。


D.public int hashCode():返回该对象的哈希码值。

   是根据对象的地址值计算出来的一个对应的整数值。

   可以理解为地址值。


(5).代码体现:

public class StudentTest {

            public static void main(String[] args) {

                        // 创建学生对象

                       Student s = new Student();


                       s.setName("林青霞");

                       s.setAge(26);


                      // cn.itcast_01.Student@181ed9e

                      System.out.println(s);

                      // cn.itcast_01.Student@181ed9e

                      System.out.println(s.toString());


                      //toString()有些时候灵活度不高。这个时候,一般选择getXxx()方法

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

           }

}


(6).关于==做比较的问题

      ==用作比较的:

     A.基本类型比较的是基本类型的值是否相同。


     B.引用类型比较的是引用类型的地址值是否相同。


2.equals()方法

   格式: 

            public boolean equals(Object obj):默认就是比较对象的是否相等,比较的是地址值。

   而一般来说,同一个类的不同对象,地址值肯定不同。所以,Object类的equals()的默认操作是没有意义。

   所以,一般我们会重写该方法比较类的成员变量值是否相同。(按照需求进行比较)


二:Scanner类

1.代码体现1:

import java.util.Scanner;


public class ScannerDemo {

           public static void main(String[] args) {

                      // 创建对象

                      Scanner sc = new Scanner(System.in);


                     // 获取int类型数据

                     // int num = sc.nextInt();


                     // 获取String类型的数据
                     // public String nextLine()

                     String line = sc.nextLine();

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

         }
}


2.代码体现2:

   int -- int     正常

   String -- String      正常

   String -- int    正常

   int -- String    不正常


  如何解决这个不正确的情况:

  (1).重新创建一个Scanner对象。


  (2).都用字符串接收,然后把一个字符串转换成int类型。


  public class ScannerDemo2 {

              public static void main(String[] args) {

                         // 创建对象

                         Scanner sc = new Scanner(System.in);


                         // 获取两个int类型数据
                         int a = sc.nextInt();
                         // int b = sc.nextInt();


                         //对sc对象重新赋值

                         sc = new Scanner(System.in);
                         // 获取两个字符串类型
                         // String a = sc.nextLine();
                         String b = sc.nextLine();
                         // int b = sc.nextInt();

                         System.out.println(a + "***" + b);

             }
}


三:String类

1.String类概述及其构造方法

(1).字符串:由多个字符组成一串数据。


(2).构造方法

     String()

     String(byte[] bytes) 

     String(byte[] bytes, int index, int length)

     String(char[] value)

     String(char[] value, int index, int length)

     String(String str)

     直接给字符串对象赋值。


(3).成员方法:

                      public int length():获取字符串的长度


(3).代码体现:

public class StringDemo {

            public static void main(String[] args) {

                        // 方式1 String()

                       String s1 = new String();

                       // s1 = "abcde";

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

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

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


                       // 方式2 String(byte[] bytes)

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

                       String s2 = new String(bys);

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

                       System.out.println("s2.length()" + s2.length());

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


                       // 方式3 String(byte[] bytes, int index, int length)

                       String s3 = new String(bys, 2, 3);

                       // StringIndexOutOfBoundsException 字符串索引越界异常

                       // String s3 = new String(bys, 2, 32);

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

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

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


                        // 方式4 String(char[] value)

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

                       String s4 = new String(chs);

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

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

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


                       // 方式5 String(char[] value, int index, int length)
                       String s5 = new String(chs, 1, 3);

                       System.out.println("s5:" + s5);
                       System.out.println("s5.length():" + s5.length());
                       System.out.println("--------------------------");


                       // 方式6 String(String str)

                       String s6 = new String("abcde");

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

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

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


                       // 方式7

                       String s7 = "abcde";

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

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

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

           }
}


2.String类常见面试题

(1).字符串一旦初始化就不可以被改变

     注意:这里指的是字符串在常量池里面的值不能发生改变。而不是说字符串的引用不能变。


(2).String s1 = new String("abc");String s2 = "abc";有区别吗?有。

     第一种方式,其实在内存中有两个对象存在。

     第二种方式,在内存中只有一个对象存在。


3.String类的判断功能

(1).判断功能:

     boolean equals(Object obj):判断字符串的内容是否相同,区分大小写。

     boolean equalsIgnoreCase(String str):判断字符串的内容是否相同,不区分大小写。

     boolean contains(String str):判断字符串对象是否包含给定的字符串。

     boolean startsWith(String str):判断字符串对象是否以给定的字符串开始。

     boolean endsWith(String str):判断字符串对象是否以给定的字符串结束。

     boolean isEmpty():判断字符串对象是否为空。数据是否为空。


(2).代码体现:

public class StringDemo {

           public static void main(String[] args) {

                      // 创建字符串对象

                      String s = "HelloWorld";


                      // boolean equals(Object obj):判断字符串的内容是否相同,区分大小写。

                      System.out.println(s.equals("HelloWorld"));

                      System.out.println(s.equals("helloworld"));

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


                      // boolean equalsIgnoreCase(String str):判断字符串的内容是否相同,不区分大小写。

                      System.out.println(s.equalsIgnoreCase("HelloWorld"));

                      System.out.println(s.equalsIgnoreCase("helloworld"));

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


                      // boolean contains(String str):判断字符串对象是否包含给定的字符串。

                      System.out.println(s.contains("or"));

                      System.out.println(s.contains("ak"));

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


                      // boolean startsWith(String str):判断字符串对象是否以给定的字符串开始。

                      System.out.println(s.startsWith("Hel"));

                      System.out.println(s.startsWith("hello"));

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


                      // boolean endsWith(String str):判断字符串对象是否以给定的字符串结束。省略不讲。

                      // boolean isEmpty():判断字符串对象是否为空。数据是否为空。

                      System.out.println(s.isEmpty());

                      String s2 = "";

                      System.out.println(s2.isEmpty());

                      // String s3 = null;

                      // NullPointerException 空指针异常

                      // System.out.println(s3.isEmpty());

         }

}


4.String类的获取功能

(1).字符串的获取功能:

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


(2).代码体现:

public class StringDemo {

           public static void main(String[] args) {

                      // 创建字符串对象

                      String s = "helloworld";


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

                      System.out.println(s.length());

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


                      // char charAt(int index):返回字符串中给定索引处的字符

                      System.out.println(s.charAt(2));

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


                      // 遍历字符串。

                      for (int x = 0; x < s.length(); x++) {

                                   char ch = s.charAt(x);

                                   System.out.println(ch);
      }

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


                     // int indexOf(int ch):返回指定字符在此字符串中第一次出现的索引

                     System.out.println(s.indexOf('l'));

                     // int indexOf(int ch,int fromIndex):返回在此字符串中第一次出现指定字符的索引,从指定的索引开始搜索。

                     System.out.println(s.indexOf('l', 4));

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


                     // 常见的方法:包左不包右。

                     // String substring(int start):截取字符串。返回从指定位置开始截取后的字符串。

                     System.out.println(s.substring(4));

                     // String substring(int start,int end)截取字符串。返回从指定位置开始到指定位置结束截取后的字符串。

                     System.out.println(s.substring(4, 8));


                     //截取的串要和以前一样。

                     System.out.println(s.substring(0));

                     System.out.println(s.substring(0,s.length()));

                     System.out.println(s);

         }
}


5.String类的转换功能

(1).字符串的转换功能:

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

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

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

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

     static String valueOf(int i)基本类型:把int(基本类型)转换成字符串。

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

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

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


(2).代码体现:

public class StringDemo {

           public static void main(String[] args) {

                      // 创建字符串对象

                      String s = "HelloWorld";


                      // 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 copyValueOf(char[] chs):把字符数组转换成字符串。

                     char[] chs2 = { 'a', 'b', 'c', '中', '国' };

                     String s2 = String.copyValueOf(chs2);

                     System.out.println(s2);

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


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

                     String s3 = String.valueOf(chs2);

                     System.out.println(s3);

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


                     // static String valueOf(int i)

                     int i = 100;

                     String s4 = String.valueOf(i);

                     System.out.println(s4);

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


                     // String toLowerCase():把字符串变成小写

                     System.out.println(s.toLowerCase());

                     // String toUpperCase():把字符串变成大写

                     System.out.println(s.toUpperCase());

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


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

                     String s5 = "hello";

                     String s6 = s5 + "world";

                     String s7 = s5.concat("world");

                     System.out.println(s6);

                     System.out.println(s7);

          }
}


6.String类的其他功能

(1).替换功能:

                       String replace(char oldChar,char newChar):用新的字符去替换指定的旧字符

                       String replace(String oldString,String newString):用新的字符串去替换指定的旧字符串


(2).切割功能:

                       String[] split(String regex)

                      去除字符串两端空格:String trim()


(3).按字典顺序比较两个字符串:

                      int compareTo(String str)


(4).代码体现:

public class StringDemo {

            public static void main(String[] args) {

                        // 替换功能

                        String s = "helloworld";

                        System.out.println(s.replace('l', 'p'));

                        System.out.println(s.replace("ll", "ak47"));


                        // 切割功能

                        String ages = "20-30";

                        // if(age>=20 && age<=30){人显示出来供我选择}

                        String[] strArray = ages.split("-");

                        for (int x = 0; x < strArray.length; x++) {

                                     // String -- int

                                     System.out.println(strArray[x]);

                        }


                        // 去除空格功能

                       String name = "  admin hello      ";

                       System.out.println("***" + name.trim() + "***");


                        // 按字典顺序比较两个字符串

                       String s1 = "hello";

                       String s2 = "aello";

                       String s3 = "mello";

                       String s4 = "hello";

                       String s5 = "Hello";

                       //System.out.println('m'+1);//109

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

                       System.out.println(s1.compareTo(s3));// -5

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

                       System.out.println(s1.compareTo(s5));//32

          }

}

0 0