James Thomson

Greenhorn
+ Follow
since Nov 19, 2001
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 James Thomson

Blimey! You're right! How strange that equals() hasn't been overridden in StringBuffer! Well that's worth knowing, thanks! Guess I was jumping to conclusions.
Yes, that's right, except for one small misconception. In your example:
for (i=0;i<5;i++) S.o.p(i);
o/p is 01234
for (1=0;i<5;++i) S.o.p(i);
o/p is 01234
The output is the same whether you use the pre- or post-increment. The expression ++i or i++ is evaluated at the end of every loop cycle.
Your working for the main problem:
for(i=0,j=0;i+j<20;++i,j+=i--) S.o.p(i+j);
is correct except that the output will start at 0.
0
1
2
3
...
19
as the expression ++i,j+=i-- is only evaluated at the end of every loop cycle, therefore the initial value is 0.
Ah well in that case, since the add() method of Frame (inherited from Container) expects a reference to a Component as it's argument, you must make an explicit cast to satisfy the compiler:
this.add((Component)third);
You could equally cast it to a Button.
The 'bounds' of a component are defined by it's x,y location, width and height. Therefore changing the location affects the bounds as does changing the size.
You're quite right. You CAN'T invoke a non-static method from the static main() method. However, in your example you create an instance of class Chenwei before invoking a method on that instance. This is OK. If you attempted to call the non-static method directly from main() you would get a compiler error.
public class Chenwei{
public static void chen(){
System.out.println("static");
}
public void wei(){
System.out.println("non static");
}
public static void main(String args[]){
chenwei baby=new chenwei();
baby.chen(); // OK
baby.wei(); // OK
chen(); //OK
wei(); // Compiler error! Static reference to non-static method
}
}
Hope that makes sense!
The equals() method is defined for class StringBuffer, and checks for equivalence of StringBuffer objects. Bear in mind that the following code:
StringBuffer sb = new StringBuffer("test");
System.out.println(sb.equals("test"));
...will print false because the literal "test" is a String object, not a StringBuffer, and therefore not equal, whether it contains the same characters or not. The following code:
StringBuffer sb1 = new StringBuffer("test");
StringBuffer sb2 = new StringBuffer("test");
System.out.println(sb1.equals(sb2));
...will print true.