zoukankan      html  css  js  c++  java
  • ==和equals的比较

     字符串只要new,就会产生一个新的地址
     == :比较的是地址 str1,str2存储在常量池中,内存优化,是同一个字符串
    equals :比较的是内容,只要内容一样结果就为true
     1 package myeclipseFiles2;
     2 
     3 public class String1 {
     4 
     5     public static void main(String[] args) {
     6         // TODO Auto-generated method stub
     7         String str1="hello";
     8         String str2="hello";
     9         String str3="Hello";
    10         
    11         String str4=new String("hello");
    12         String str5=new String("hello");
    13         //字符串只要new,就会产生一个新的地址
    14         //==比较的是地址 str1,str2存储在常量池中,内存优化,是同一个字符串
    15         System.out.println(str1==str3);//false
    16         System.out.println(str1==str2);//true
    17         System.out.println(str1==str4);//false
    18         System.out.println(str4==str5);//false
    19         System.out.println(str1==str3);//false
    20         //equals比较的是内容,只要内容一样结果就为true
    21         System.out.println(str1.equals(str4));//true
    22         System.out.println(str1.equals(str3));//false
    23         
    24         
    25     }
    26 
    27 }
     1 package myeclipseFiles2;
     2 
     3 public class String1 {
     4 
     5     public static void main(String[] args) {
     6         // TODO Auto-generated method stub
     7         String str1="Hello";
     8         String str4=new String("hello");
     9         str4="Hello";//重新赋值后,原来的str4 new出来的新地址被垃圾回收站回收成为空指针
    10         System.out.println(str1==str4);//true
    11     }
    12 
    13 }
  • 相关阅读:
    LoadLibrary And GetProcAddress And FreeLibrary
    Preprocessor Directives
    Pragma Directives
    How to use Union in c++?
    WhiteSpace
    Export Class and Struct
    Two Ways To Export from a DLL
    Know more about the organization of solution and project
    Cygwin
    二叉树及其应用
  • 原文地址:https://www.cnblogs.com/ztt0918/p/8026857.html
Copyright © 2011-2022 走看看