CS115 lecture notes, Spring 1999 Week 2, Monday Reading: Chapter 2 Java syntax ----------- public class Hello { // main: generate some simple output public static void main (String[] args) { System.out.println ("Hello, world."); } } 1) a program includes a class definition, which is the words "public class Hello", plus everything between the squiggly braces 2) public is optional, class is not, Hello is a name you make up 3) comments can contain anything (until the next line) 4) this class definition contains one method definition, which is the words "public static void main (String[] args)" plus everything between the squiggly braces. 5) Which squiggly brace goes with which? They nest. Indentation helps make it visually obvious. 6) public is optional, static and void are not, and the name "main" is special. It indicates the place in the program where execution begins. 7) System.out.println is the full name of the command to print things. There is no command named "pintln", but there is one named "print". 8) semi-colon at the end of every statement. What's a statement? What's the difference between "println" and "print"? What happens when you get these rules wrong? Mostly syntax errors, possibly a run-time error or two. If there is no method named main, that causes a run-time error. Same thing if you leave out "static". Chapter 2 --------- Variables are named storage locations. There are different types of variables for different types of values. Things in quotes are String values. Can create a String variable: String fred; And assign a String value to it: fred = "Hello, world!"; Can create an integer variable: int bob; And assign an integer value to it: bob = 5; Can you put square pegs in round holes? Assignment statement puts a value in the box. "Give the variable a value", "Set a variable to a value" "Assign the value to the variable." etc. String values always appear in quotation marks. "123" is a String value. 123 is an integer value. They are not the same thing. Although you can convert from one to the other (as we'll see later). Printing variables ------------------ When you print a variable, you print the VALUE of the variable. System.out.println (fred); System.out.println (bob); Java knows what type each variable is (because you declared it), and prints it appropriately. Keywords -------- Avoid variables with blue names. While I'm thinking about it, all variable names begin with a lower case letter. More than one word? Make subsequent words start with capital. Examples: interestRate priceOfEggs Operators --------- + - * All do what you expect. Integer division is not what you expect! Always rounds to the lower integer (truncation). There's another type for dealing with fraction parts. You can use values AND variables as operands. Values, variables, and operators together are called "expressions". Order of operations ------------------- PEMDAS, as usual (expect there is no exponentiation argument). Within each category, left to right. 2*3-1 = 5 2/3-1 = -1 Operations on Strings --------------------- + is String concatenation. String fred = "leather jacket"; String bigotry = fred + "ism"; System.out.println (bigotry); Composition ----------- The ability to assemble building-blocks into larger structures, often by nesting one structure withing another. Example: expressions made up of smaller expressions Almost an example: anywhere you can put a simple value, you can also put a complex expression. Exception: the left side of an assignment must be a variable, since it specifies where the result will be stored. Values and expressions do not have locations. minute+1 = hour; // WRONG!!