zoukankan      html  css  js  c++  java
  • String类中"=="、equals和普通类中"=="、equals的比较

    package cn.method.demo;
    
    public class StringDemo2 {
            public static void main(String[] args) {
                String s1=new String("qy95");
                String s2="qy95";
                System.out.println(s1);
                System.out.println(s2);
                //在String 类中引用类型(==)表示该对象内存地址数值比较
                //而String类中的equals是该类本身中自己定义的方法,用于字符串内容的比较
                //简而言之:在String类中  "=="比较·内存地址    "equals"比较内容
                System.out.println(s2==s1);     //false   
                System.out.println(s1.equals(s2));   //true
        
    }
    
    }
    
    /*
    普通类中比较的都是地址
    **/
    package cn.equals.demo; public class Person extends Object{ private String name; private int age; public Person(){ } public Person(String name,int age){ this.name=name; this.age=age; } } package cn.equals.demo; public class Text { public static void main(String[] args) { Person p=new Person("张三",77); Person p1=new Person("张三",77); boolean e=p.equals(p1); //比较的都是地址 System.out.println(e); //false System.out.println(p==p1); //false } }
  • 相关阅读:
    POJ 1300 Open Door
    POJ 2230 Watchcow
    codevs 1028 花店橱窗布置
    codevs 1021 玛丽卡
    codevs 1519 过路费
    codevs 3287 货车运输
    codevs 3305 水果姐逛水果街二
    codevs 1036 商务旅行
    codevs 4605 LCA
    POJ 1330 Nearest Common Ancestors
  • 原文地址:https://www.cnblogs.com/qurui1998/p/10552300.html
Copyright © 2011-2022 走看看