Module 2: More About Syntax


Objectives

 

By the end of this module, for simple HelloWorld-like programs, you will be able to:

 

2.0 Audio:
 


2.0    Comments

 

One can insert comments into code that do not affect the code itself:

  • For example:

  • Comments are similar to their analogue in editing text documents, e.g.

  • There are three kinds of comments:
    1. Single line comments that begin with //.
    2. Multi-line comments beginning with /* and ending with */.
    3. Javadoc comments (for advanced use in automated documentation).

  • We will use only the single-line comment in this course.

  • A single line comment ends at the end of the line.

  • This is a mistake:

 

2.1 Exercise: What is the compilation error for the above program? Fix the error, add your own additional multi-line and single-line comments and compile again. For all answers that involve writing text, answering a question, or drawing in this module, write them in module2.pdf. You can also include code in module2.pdf. if that helps make your point, for example, by showing how you fixed the error above. Similarly, in future modules you will submit module3.pdf, module4.pdf, and so on (for the text, drawing and other non-program responses).
 

2.2 Audio:
 

2.3 Video:

 


2.1    Strings

 

About strings:

  • A string in general computer science terminology is a sequence of letters or symbols.

  • A string in Java is any sequence of letters or symbols enclosed by a matching pair of double-quotes.

  • Examples of strings:
            "Hello World!"
    
            "'sup?"
    
            "@#%&* [Expletive deleted]"
         

  • Thus far, we have only used strings for printing:

  • Later, of course, we will see many other uses.

  • A string cannot be typed over multiple lines.

  • This is a mistake:

 

2.4 Exercise: What is the compilation error for the above program? How would you fix the error?
 

2.5 Video:

 


2.2    Escape sequences

 

The fact that strings begin and end with double quotes begs the question: how does one print out a double-quote?

  • To solve this problem, Java uses a trick:
           ⇒ A special code is used for a double-quote.

  • But this begs the question: how do you print a backslash?

    Answer: a double-backslash prints as a single one.

    The above program prints out two backslashes, separated by a space.

  • Java has a number of (esoteric) escape sequences.
           ⇒ There's no need to know them all.

  • The important ones:
    • \" for a double-quote.
    • \\ for a backslash.
    • \n for a new line.
 

2.6 Exercise: Write a program called PracticeEscaping.java that will print out

  "    "   \\\
  "    "    \    
  """"""    \    
  "    "    \    
  "    "   \\\  
Note: in the first line, there are 3 spaces between the last double-quote and the first backslash.
 

2.7 Video:

 

2.8 Audio:
 


2.3    print and println

 

You've been wondering why something that prints is called println instead of print.
 

2.9 Exercise: Write up this program to see what it prints to the screen.

Compare the output to the standard HelloWorld program very carefully.
 

2.10 Exercise: Without using println, get the above program to print exactly like the standard HelloWorld. Write this program in HelloWorldPrint.java. Hint: use the right escape sequence.
 

About print:

  • Thus, println is essentially a print followed by the escape sequence for a newline.

  • Later, when we learn for-loops, we will see a use for print.
 

2.11 Audio:
 


2.4    Identifiers

 

There are three kinds of "words" in Java:

  1. The first kind are the core language words, called reserved words such as class and void:

    • Reserved words are used to define program structure and control.
    • Reserved words may only be used in the ways they are meant for.


    2.12 Exercise: Look up the list of reserved words in Java. How many are there?

  2. Words that we make up for our programs, such as: HelloWorld:

    • These are words we can change.
             ⇒ Recall: we've changed the name of the class before.
    • It may come as a surprise that argv is something we could re-name.
             ⇒ We will understand this when we learn about method parameters.

  3. The third kind are words chosen by other programmers, especially those who designed code in the Java library, such as:

    • We have no choice in using these words.
             ⇒ We can't change them.
 

About identifiers:

  • The latter two kinds of words are called identifiers.

  • There are some restrictions on creating identifiers:
    • They must use only letters, numbers, the underscore _ symbol, or the $ (dollar) symbol.
    • They cannot start with a number.
    • They cannot be one of the reserved words.
     

    2.13 Exercise: Which one of the following is NOT a permissible identifier?

    1. Idea
    2. 1dea
    3. id12
    4. Id__
     

    2.14 Exercise: Which one of the following is NOT a permissible identifier?

    1. super
    2. sup_r
    3. sup12
    4. supercalifragilisticexpialidocious_is_not_be_the_longest_word_isnt_that_crazy

  • There are also some stylistic guidelines in choosing identifiers. We'll talk about these later.
 

2.15 Exercise: Change one of the identifers in the standard HelloWorld program to something else.
 

Let us next explore errors made with these words.
 

For each of the programs below: (1) Try to identify the problem (if any) just by reading. (2) Then, use the edit-compile-test process to see what the error is (if any), reporting your results in module2.pdf.
 

2.16 Exercise:

Program 1:

 

2.17 Exercise:

Program 2:

 

2.18 Exercise:

Program 3:

 

2.19 Exercise:

Program 4:

 

2.20 Exercise:

Program 5:

 


2.5    Good writing habits

 

First, let's point out how NOT to do it:

  • Write programs without full concentration.

  • Write carelessly, thinking that repeated compilation will find your syntax errors anyway.

  • Write by looking at existing code, letter by letter.

  • Write in sequence from the first letter to the last.
 

Good writing habits:

  • Write and type carefully, at a slow enough pace to avoid common mistyping errors.
           ⇒ The cost of mistyping is high: detecting errors.

  • When you type a left bracket, especially of a class or method, immediately type in the right bracket as well.

    Then, after that, move the cursor in between and type what needs to be in between.

  • If you are typing something similar to some existing program, try not to do this letter by letter, but instead line by line.
           ⇒ It will improve your reading ability.

  • Get the indentation correct right away, as opposed to fixing indentation later.
           ⇒ This will help you detect errors as you type.

  • When you type in an identifier, pause momentarily to make sure it's correct, and what was intended to be there.
 

For example, with HelloWorld:

  • First type in the class:

  • Then the method outline:

  • And then the method code.

  • Generally, if the brackets are going to be separated by multiple lines, type both and then move the cursor backwards.
 

2.21 Video:

 

2.22 Audio:
 

On to Module 3


© 2017, Rahul Simha