Java初级笔记_常用API

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

一、 Object类
      (1)是定义在java.lang包下的,是所有类的超类。所有类都直接或者间接的继承自Object类。
     父类:超类,根类,基类。
     子类:派生类。

(2)要掌握的方法:
      public String toString():返回对象的字符串表示形式。
 默认情况下的组合:类的全路径名称+@+对象的哈希值的十六进制表示。
         这种做法对我们来说,一般没有意义,所以,建议重写。
  重写做法:一般是把所有的成员变量组合成一个字符串返回。
         实在不愿意自己写,自动生成。

      public boolean equals(Object obj):比较对象的地址值是否相同。
默认情况下,比较的是对象的地址值是否相同。
 如果有自己的特殊需求,请重写Object类的该方法。
 怎么重写呢?

public boolean equals(Object obj) {
if(this == obj) {
return true;
}
if(!(obj instraceof Student)) {
return false;
}
Student s = (Student) obj;
return this.name.equals(s.name) && this.age == s.age;
}

如果有多个成员变量,把多个比较用&&连接即可。
引用类型用equals()方法。
基本类型用==号即可。
         实在不愿意自己写,自动生成。


(3)面试题:
      ==和equals()的区别?
==:
可以比较基本类型,也可以比较引用类型。
比较基本类型,比较的是值是否相同。
比较引用类型,比较的是地址值是否相同。
equals:
只能比较引用类型。
默认情况下,比较的是地址值是否相同。
如果想比较内容,请自己重写Object类的equals()方法。
二、 Scanner的概述
      (1)Scanner是JDK5以后出现的方便我们从键盘接受数据的类。
(2)Scanner的构造格式:
       Scanner sc = new Scanner(System.in);
System.in 是System类下面有一个静态的成员变量in。它的类型是InputStream。
  代表的是标准键盘输入流。也就是键盘录入数据。
Scanner是对其进行了封装,提供了各种转换功能。方便我们获取到想要的数据类型的数据。

(3)要掌握的两个功能:
       A:返回int类型
   public int nextInt()
       B:返回String类型
   public String nextLine()
   public String next()
注意事项
   先next,再nextLine会有问题。
   解决方案:
     重新建立Scanner对象。//一般不会这样做。因为消耗资源
     根据需求,选择合适的方法。
     统一一种方法。
三、 String类的概述和使用
      (1)由多个字符组成的一串数据。
(2)构造方法:
    A:String s = new String();
    B:String s = new String(byte[] bys);
    C:String s = new String(byte[] bys,int startIndex,int count);
    D:String s = new String(char[] chs);
    E:String s = new String(char[] chs,int startIndex,int count);
    F:String s = new String(String s2);
    G:String s = "hello";
    常使用:
BCDEG
(3)面试题:
    A:字符串一旦被赋值就不能被改动。
注意:这里的改动指的是字符串的内容,而不是字符串对象的引用。
    B:String s = new String("hello");和String s = "hello";有区别吗?是什么呢?
有。
  前者创建了两个对象。
  后者创建了一个对象。

    C:看程序,写结果
String s1 = new String("hello");
  String s2 = new String("hello");
  System.out.println(s1==s2);
  System.out.println(s1.equals(s2));

  String s3 = new String("hello");
  String s4 = "hello";
  System.out.println(s3==s4);
  System.out.println(s3.equals(s4));

  String s5 = "hello";
  String s6 = "hello";
  System.out.println(s5==s6);
  System.out.println(s5.equals(s6));

    D:看程序,写结果
String s7 = "hello";
  String s8 = "world";
  String s9 = "helloworld";
  System.out.println(s9==s7+s8);
  System.out.println(s9=="hello"+"world");

  变量就直接造,常量先找,如果有就使用,否则就造。

(4)字符串的常见功能:(补齐中文)
    A:判断功能
boolean equals(Object obj)
  boolean equalsIgnoreCase(String str)
  boolean contains(String str)
  boolean startsWith(String str)
  boolean endsWith(String str)
  boolean 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)


    C:转换功能
byte[] getBytes()  //获取字符串的字节数组
  char[] toCharArray()  //获取字符串的字符数组
  static String copyValueOf(char[] chs)
  static String valueOf(char[] chs)
  static String valueOf(int i)
  String toLowerCase()
  String toUpperCase()
  String concat(String str)


    D:其他功能
String replace(char old,char new)
  String replace(String old,String new)

  String[] split(String regex)

  String trim()

  int compareTo(String str)
  int compareToIgnoreCase(String str) 

(5)案例:
    A:遍历字符串
String s = "hello";

  for(int x=0; x<s.length(); x++) {
System.out.println(s.charAt(x));
  }

    B:统计字符串中大写字母,小写字母以及数字字符出现的次数
    C:把一个字符串的首字母变成大写,其他的全部小写
    D:统计大串中小串出现的次数

  ------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值