Lesson: Basic Syntax

Overview: 
Explore the basic syntax of Java.
Objectives: 

Understand the syntax rules of Java, basic program structure, naming conventions, code blocks and comments.

Content: 

As we move forward, we will start to acquire new vocabulary at an increasing rate. It is normal for vocabulary to take a while to stick, so don’t give up! Additionally, syntax is something you will learn by doing (and messing up!). Syntax is a challenge that you will always be getting better at, and is something nobody is perfect at.

Java programs consist of one or more classes (templates defining the characteristics and functions of an object). Classes consist of one or more class level variables (or constants) and one or more methods.
 
Variables are memory locations in which we can store data, like a number or string of characters. Variables can be changed as the program executes. Constants are variables that are set to an initial value and are not changed during execution.
 
Methods consist of one or more method local variables (or constants) and one or more statements.
 
Statements are code that change the state of the variables in a program and are how the program performs computations or make decisions about what to do next.
 
Here is what this looks like:

All of these things (other than statements) have names. Names in Java (also called identifiers) follow these rules:

  • Start with a letter, $ or _ (underscore). After the first character, you may use any combination of characters.
  • Java Keywords cannot be used as names.
  • Names are case sensitive. That means MyName is not the same as myname or myName.

While not requred by Java, most programmers follow this convention:

  • Class names start with an upper case letter. (MyClass rather than myClass)
  • Variable and method names start with a lower case letter. (myVariable rather than MyVariable)
  • When names are made up compound words, the first letter of the second word is upper case: MyClass, myVariable, myMethod.
  • Upper case all words in a constant and separate compound words with an underscore: MY_CONSTANT, WHEEL_DIAMETER, CAMERA_IP.

Keywords are reserved words used by Java. A list of the keywords can be found here. The meanings of the keywords will be discussed later. Note: when something is ‘reserved’ it means you cannot use it as a name of a variable or class. If you try to use a keyword in this way, the program will not compile.

Comments are used to document what your code is doing to aid both yourself and others in reading your code later on. Comments are important and should be used liberally. For example, there may be a complicated set of steps for a robot to lift an arm and open a gripper. In order to understand that is what is happening, someone would have to know what each step of your code does. A comment at the start of that set of steps that says “// lift arm and open gripper” makes things much easier for outsiders to utilize. Here is a discussion of the Java comment formats. Don't worry about JavaDoc (mentioned in the article), that is an advanced topic. 
 
It is important to note that even when legitimate code is written inside a comment, it will never run. The compiler always ignores the contents of any comment. This is a simple way to remove a portion of code from your program temporarily without having to copy and paste or re-type your code when finished.

Whitespace is also a form of commenting. Whitespace is blank lines, indents, extra spaces used to make your code easier to read. Again, this is both for yourself and others that may have to read your code. Use whitespace to separate operations, groups of statements and dense lines of code.

Note that braces { } are used to identify blocks of code that are treated as a group. All of the code for a class is enclosed in braces just as all code in a method is enclosed in braces. Just like a set of parentheses can have multiple layers like (x - (y-2)), so can braces. It is not uncommon for the end of a program to have many closing braces as each layer is closed off. Making sure each opening brace is paired with a closing brace is essential.

Finally, all statements end in a semicolon.

 

Navigation: