eric sun

Greenhorn
+ Follow
since Jan 22, 2002
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by eric sun

Originally posted by Seany Iris:
What will be the output when the following code is run?

1. class ThisClass{
2. public static void main(String args[]){
3. Object o=(Object)new ThisClass();
4. Object s=new Object();
5. if(o.equals(s))
6. System.out.println("true");
7. }
8. }



The answer is No output. Why ? Because the method equals() be overridden.
The equals() method in Object and all Object extends it. So you can override this method in new SubClass.
String , Integer .... etc class all override this method, so you can see if their content is same the result is true. But more object do not rewrite this method, the result is all false, like StringBuffer or Untitled5 .
Exp:
public class Untitled5 {
public static void main( String[] args[]){
Object obj0 = new Object();
Object obj1 = new Object();
Object obj2=(Object) new Untitled5() ;
Untitled5 u5_0 = new Untitled5() ;
Untitled5 u5_1 = new Untitled5() ;
Integer i_0 = new Integer(3);
Integer i_1 = new Integer(3);
System.out.println("obj0 equals obj1 ? "+obj0.equals(obj1));
System.out.println("obj0 equals obj2 ? "+obj0.equals(obj2));
System.out.println("u5_0 equals u5_1 ? "+u5_0.equals(u5_1));
System.out.println("i_0 equals i_1 ? "+i_0.equals(i_1));
}
}
The result is :
obj0 equals obj1 ? false
obj0 equals obj2 ? false
u5_0 equals u5_1 ? false
i_0 equals i_1 ? true

Originally posted by John Lynn:
I found this handy set of 4 science fiction-like sentances to help remember the java reserved words:
1. Abstract native try break default interface while byte finally throws
public case for do
2. super short volatile private synchronized this final return continue
throw long char
3. if long protected static continue float Boolean extends strictfp else
implements void instanceof int
4. const goto class catch new transient package import double protected
switch


strictfp What is this ?
Regards
Eric

Originally posted by Valentin Crettaz:

Yeah I'm thinking about writing one....
Just kidding


Yes, your example is a very good.
Regards
Eric
Bill, I will pass the exam at Friday before you.
Good luck to you and also to meself.
Notice here, if the first case number is "0" , there will be error with imcompatible types. Here need byte, short, char, int type.
1:LOOP: for (i=0;i<5;i++) {
2: switch(i++) {
3: case '0': System.out.println("A");
4: case 1: System.out.println("B"); break LOOP;
5: case 2: System.out.println("C"); break;
6: case 3: System.out.println("D"); break;
7: case 4: System.out.println("E");
8: case 'E' : System.out.println("F");
9:}
10:}
Now, let explain why the answer is C E F step by step.
Loop 1 :
1: i=0
2: swith(i=0) but (0)i++ = 1 => i
3-9: no case equal i ( 0 )
10: act the i++ in for() (1)i++=2 => i
Loop 2 :
1: i=2
2: swith(i=2) but (2)i++ = 3 => i
5: case 2: System.out.println("C"); break;
10: act the i++ in for() (3)i++=4 => i
Loop 3 :
1: i=4
2: swith(i=4) but (4)i++ = 5 => i
7: case 4: System.out.println("E");
8: case 'E' : System.out.println("F");
10: act the i++ in for() (4)i++=5 => i
end