Home > Computer Programming > Beginning Java: Things to watch for

Beginning Java: Things to watch for

January 15, 2015 Leave a comment Go to comments

All of our early work will look a bit like this:

public class BlahDemo {
  // instantiate some Blahs and call their methods
}

class Blah {
  // variable declarations

  void method1() {
    // method1 code...
  }

  void method2() {
    // method2 code...
  }
}
  • Capitalization matters. numwords and numWords are totally different identifiers.
  • There are also capitalization conventions: classes are always proper case LikeThis, variables are always camel case likeThis
  • Types of brackets:
    • Curly ones {} for enclosing classes and method bodies
    • Round ones () for enclosing arguments in method calls
    • Square ones [] for enclosing array references
  • With curly braces, take a look at what encloses what. Specifically:
    • The Blah class is not within the BlahDemo class. It follows it.
    • The methods are not within one another. They just occur one after the other in the class.
    • All variable declarations and methods must be written within a class. You can’t just have stuff floating about.
    • Variables can be declared within the class, or within a method within the class. In the first case they are visible by the whole class and the whole class (including any methods) can use them. In the second case only the method they are contained in can see them.
  • Statements must end with a semi-colon, but don’t put semi-colons directly after a curly brace. If you do that you’ve almost certainly got it wrong.
  • Pay attention to the syntax of if… else if… else statements, for loops and while loops. This page gives examples of everything.
  • You only use the equals sign (=) when you are assigning (giving) a value to a variable, e.g. x = 5 means “let x take the value 5”. However, if you want to compare two values, you must use double equals (==), e.g. if (x == 5)….
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s