

Java技术
2005: 03 04 05 06 07 08
09 10 11 12
2006: 01 02
Asp.net
2005: 07 08 09 10 11 12
2006: 01 02

| public class TestIntComparison { public static void main(String[] args) { int x = 5, y = 5; System.out.println( "x == y yields " + (x == y)); } } |
| D:\>java TestIntComparison x == y yields true |
| class Dog { int tag; int age; public void setTag(int t) {tag=t;} public void setAge(int a) {age=a;} } |
| Dog a = new Dog(); a.setTag(23129); a.setAge(7); Dog b = new Dog(); b.setTag(23129); b.setAge(7); if ( a==b ) { System.out.println("a is equal to b"); } else { System.out.println("a is not equal to b"); } |
| if ( a.equals(b) ) { System.out.println("a is equals() to b"); } else { System.out.println("a is not equals() to b"); } |
| class Dog { int tag; int age; public void setTag(int t) {tag=t;} public void setAge(int a) {age=a;} public boolean equals(Object o) { Dog d = (Dog)o; if ( tag==d.tag && age==d.age ) { return true; } return false; } } |
| fido.equals("blort"); |
| public boolean equals(Object o) { if ( o instanceof Dog ) { Dog d = (Dog)o; if ( tag==d.tag && age==d.age ) { return true; } } // false if not Dog or contents mismatched return false; } |
| boolean b = "abc".equals("def"); // false boolean c = "abc".equals("abc"); // true |