• Post Reply Bookmark Topic Watch Topic
  • New Topic

Few simple questions  RSS feed

 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to clarify a few things that have been nagging me for awhile.

First, what are the real benefits of making an interface? It contains methods that other classes can use, but those classes still need to write the whole method from scratch plus all other methods that the interface contains.

In all the code I have written the almost only loop I use is the FOR loop. I don�t think I have ever used WHILE loop for real. I just haven�t been in any situation that the FOR loop couldn�t handle. Someone show me an example where the WHILE loop is necessary.

I don�t really get why I should use the try-catch statements, it seems that many people do include them in their code, but why create a behaviour that only executes when things go wrong when you don�t won�t your program to go wrong in the first place.

What are static methods and variables really good for?

Thats about it, thanks
[ August 16, 2005: Message edited by: Unnar Bj�rnsson ]
 
Marilyn de Queiroz
Sheriff
Posts: 9082
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's usually better to ask one question per thread rather than 4 questions in one thread.

An interface is useful when you have dis-similar objects that have something in common. For example, a tire is not a ball and a ball is not a tire, but they can both be inflatable. They can also both be bounceable.

Tradition calls for using a for loop any time that you know in advance how many times you are going to do a loop. If the loop might be terminated on something other than x iterations, a while loop is best.

You may want your program to do something else besides end (or before it ends) when it runs into a problem. Or you may want to give the user a message other than the default stack trace.

Classes don't always have to be instantiated as objects. OO techniques are great, but there are times when procedural or linear techniques are better.
[ August 16, 2005: Message edited by: Marilyn de Queiroz ]
 
Lee Diego
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All loops are the same and you can therefore
use any type of loop (for, while and even do-while(might have to use an if))

for instance the following code can be written in many ways...

System.out.println("Init");
for ( int i = 0; i < 5; i++ ) {
System.out.println("nbr = " + i);
}

with a while loop it becomes

System.out.println("Init");
int i = 0;
while ( i < 5 ) {
System.out.println("nbr = " + i);
i++;
}

or we could write it...

int i;
for (i = 0, System.out.println("Init"); i < 5;
System.out.println("nbr = " + i), i++ );

Thus the for and while loops are just the same loop on different forms

init-code;
while ( terminate-loop-expression ) {
code-statements;
perform-each-iteration-code;
}

for ( init-code; terminate-loop-expression; perform-each-iteration-code ) {
code-statements;
}

The point is its all about style, if you know
how many iterations the loop is going to perform
use for if you don't use while (do-while is not used very often).

// Lee
 
Lee Diego
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a nice example of the use of a static variables and functions
look in the java standard class Math,
the constant PI (an example everything is constant in this class)
is defined as

public static final double PI = 3.14 ... (probably lots of digits);

now if this constant where not defined as static
you would have to create an instance of the object
when you want to use the constant.

and i think it is preferable to use

int area = Math.PI * radius * radius;

than

Math m = new Math();
int area = m.PI * radius * radius;

// Lee
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All loops are the same and you can therefore
use any type of loop (for, while and even do-while(might have to use an if))


Not sure I agree with this 100%.



yes you could probably work a for loop to get the needed behaviour, but in this type of case I believe the while loop makes much more sense.

I think of for loops for iterating a number of times, and a while loop for checking whether a condition is met.
 
Lee Diego
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, for example...



But i totaly agree with you, for for fixed iterations, while for non-fixed.

// Lee
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!