String a="abc"和String b=new String("abc")的区别及String相关常用操作

  在讨论两者区别之前我们先明确.equals()和==的区别

  String a = "abc";

  String b = new String("abc");

  String c = "abc";

  String d = new String("abc");

  System.out.println(a==b);

  System.out.println(a==c);

  System.out.println(b==d);

  System.out.println(a.equals(b));

  输出的结果是:false

                              true

                              false

                              true

  String是引用类型,==既比较地址又比较值,.equals()只比较值。

  如果按正常声明并赋值String类型,如String a="abc";系统会自动在内存中查找是否有abc这个值,如有,将会把a的头指针指向已存在的abc值。而new出来的字符串,不管内存中本来有没有abc值,都会新建一块内存,把abc存进去。需要注意的是,如果:

         String a=new String("abc");

         String b="abc";

         System.out.println(a==b);

         结果为:false

         猜测原因为new出来的类型和直接赋值的类型在内存中存储方式不一样,所以系统找不到已存在的字符串abc。因不了解Java底层写法。所以……姑且这么记吧。

        JDK API官方文档写法:

       

   String str = "abc";
 

等效于:

     char data[] = {'a', 'b', 'c'};
     String str = new String(data);

  //在类的内部,同类型的对象引用,可以调用类内的private属性信息
        public String(String original) {
        int size = original.count;
        char[] originalValue = original.value;
        }

  String bb = new String("abc");         //分配了两块内存,abc一块内存,bb新建了一块内存把abc复制过去而不是把bb的引用指向abc

  String cc = "a" + "b" + "c";           //分配了5块内存

//两个重要的构造函数
  String(char[] value)
          分配一个新的 String,使其表示字符数组参数中当前包含的字符序列
   String(String original)
          初始化一个新创建的 String 对象,使其表示一个与参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的副本
   


String相关常用操作:

//重要方法
  int indexOf(String str)
          返回指定字符在此字符串中第一次出现处的索引


        int index = url.indexOf("8081");
     System.out.println("index=" + index);
     
     String sub = url.substring(index);
     System.out.println(sub);

    String[] split(String regex)

 String cookie = "uname=sssss,pwd=abc";
     String[] upwd = cookie.split(",");
     for(int i=0;i<upwd.length;i++){
      System.out.println(upwd[i]);
      
     }



//字符串转换成整数的方式
    String aa = "102";
     Integer bb = new Integer(aa);
     Integer cc = Integer.parseInt(aa);
     Integer dd = Integer.valueOf(aa);     
     System.out.println("dd=" + dd);

//把整形转换为字符串
     String kk = String.valueOf(bb);

      //字符串转换成日期
 String aa = "2016-10-13";
     String bb = "08/23/2016";
     SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd");
     SimpleDateFormat s2 = new SimpleDateFormat("MM/dd/yyyy");
     try {
      Date d1 = s1.parse(aa);
         Date d2 = s2.parse(bb); 
  } catch (Exception e) {
   e.printStackTrace();
  }
  
  
  //日期转字符
  Date today = new Date();
  SimpleDateFormat s3 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
  String pp = s3.format(today);
  System.out.println(pp);

      



String a="hello world"; //在java中有一个常量池,当创建String 类型的引用变量给它赋时,java会到它的常量池中找"hello world"是不是在常量池中已存在。如果已经存在则返回这个常量池中的"hello world"的地址(在java中叫引用)给变量a 。注意a并不是一个对象,而是一个引用类型的变量。它里面存的实际上是一个地址,而这个是指向一个字符串对象的。在程序中凡是以"hello world"这种常量似的形式给出的都被放在常量池中。 String b=new String("hello world"); //这种用new关键字定义的字符串,是在堆中分配空间的。而分配空间就是由new去完成的,由new去决定分配多大空间,并对空间初始化为字符串"hello world" 返回其在堆上的地址。 通过上面的原理,可以做如下实验: String a="hello world"; String b="hello world"; String c=new String("hello world"); String d=new String("hello world"); if(a==b) System.out.println("a==b"); else System.out.println("a!=b"); if(c==d) System.out.println("c==d"); else System.out.println("c!=d"); //输出结果: a==b c!=d 为什么会出现上面的情况呢? String a="hello world"; String b="hello world"; 通过上面的讲解可以知道,a和b都是指向常量池的同一个常量字符串"hello world"的,因此它们返回的地址是相同的。a和b都是引用类型,相当于c语言里面的指针。java里面没有指针的概念,但是实际上引用变量里面放的确实是地址,只是java为了安全不允许我们对想c语言中的那样对指针进行操作(如++ 、--)等。这样就有效的防止了指针在内存中的游离。 而对于 String c=new String("hello world"); String d=new String("hello world"); 来说是不相等的,他们是有new在堆中开辟了两块内存空间,返回的地址当然是不相等的了。如果我们要比较这两个字符串的内容怎么办呢?可以用下面的语句: if(c.equals(d)) System.out.println("c==d"); else System.out.println("c!=d"); //输出 c==d
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值