Java Notes

'else' Not Required

'else' is not required

It is not necessary to have the else part of an if statement. Maybe only 50% of the time there is an else part.


The if statement without an else has this form:
   if (condition) {
      do this if the condition is true


Here is a paintComponent() method with an if statement without an else clause.
public void paintComponent(Graphics g) {
  super.paintComponent(g);  // draw background etc.
  if (marks < 50) {
  g.drawString("Score = " + marks, 10, 50);
When the paintComponent() method begins, the Graphics context g uses by default. Therefore there is no need to set the color to black.