API补习之java.lang包

59 篇文章 0 订阅

API补习 .java.lang包 1 原始类型 封装类 char         Char int           Integer

Integer num=new Integer(5);

int  num2=num.intValue;   //1



Integer num=Integer.valueof(2); //用于将相应的原始

值转换为其相应的封装类;



Charater  isLetter() 是否是字母 isWhiteSpace() 是否为空格或换行符

2  StringBuffer类 (默认情况下,StringBuffer类保留的空间是16个字符),当使用+时自动生成StringBUffer. 用于表示可以修改的字符串.

public class StringBuf {

   protected StringBuf(){}

   public static void main(String[] args){

    StringBuffer buf=new StringBuffer("Java");

    buf.append(" Guide Verl/");  //添加

    buf.append(3);//添加

    int index=5;

    buf.insert(index,"Student "); //插入 索引后的字符向后移

       index=23;

       buf.setCharAt(index, '.');   //改变字符

       int start=24;

       int end=25;

       buf.replace(start, end, "4");  //替换从哪到哪的字符

       String s=buf.toString();  //StirngBuf和String间的转换

       System.out.println(s);

   

   }

}

结果Java Student Guide Verl.4

第二个例子

public class Test {

    public static void stringReplace (String text) {

      text = text.replace('j' , 'i'); 

    }

   

    public static void bufferReplace (StringBuffer text) {

      text = text.append("C"); 

    }

   

     public static void main (String args[]) { 

       String textString = new String ("java"); 

       StringBuffer textBuffer = new StringBuffer ("java"); 

    

       stringReplace (textString); 

       bufferReplace (textBuffer); 

    

       System.out.println (textString + textBuffer); 

     } 

   }

答案是 javajavaC 这是String参数传递,是不可变的(immutable).

而题目中第七行text = text.append (“C”),append方法会改变text中的值 而这个text与main中的textBuffer是指向同一个对象,所以对应的输出是javac。 string的值永远不会改变! String a = "a";//假设a指向地址0x0001, a = "b";//重新负值后a指向地址0x0002,但0x0001地址中保存的"a"依旧存在,

但已经不再是a所指向的。 从表面上看String类型的对象改变了值,但事实是他不能改变值,只能改变指向

地址;StringBuffer则不同,直接改变指向的地址中保留的

例子三

StringBuffer s1 = new StringBuffer("a");

StringBuffer s2 = new StringBuffer("a");

   if( s1.equals(s2)) //为什么是false

      { System.out.print("yes");

    }

          String s11 = new String("a");

      String s21 = new String("a");

     if( s11.equals(s21))//为什么是true

               {

       System.out.print("yes");

      }

StringBuffer类中没有重新定义equals这个方法,因此这个方法就来自Object类,

而Object类中的equals方法是用来比较地址的,所以等于false. String类中重新定义了equals这个方法,而且比较的是值,而不是地址。所以会

是true。

3 Class类 实例.getClass() 由实例获得对对象 getName() 获得对象的名字 实例.getSuperclass() 由实例获得父类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值