Spring Semester 2005


Lecture Notes Two: Values (literals), operators, expressions and variables.

Communicating with Your User

Last time we developed the smallest Java program. The empty program:
class One {
    public static void main(String[] args) {

    }
}
You noticed that we changed its name. That's your degree of freedom.

Let's make it print something:

class One {
    public static void main(String[] args) {
	System.out.println("This is my song for the asking..."); 
    }
}
We will focus on the inside, the part that is clearly visible.

What does it look like?

class One {
    public static void main(String[] args) {
	System.out.println("This is my song for the asking..."); 
	System.out.println("       and I'll"); 
	System.out.println("Ask me          play"); 
	System.out.println("                      it"); 
	System.out.println("                 so sweetly, I'll"); 
	System.out.println("               make you smile..."); 
    }
}
The inside is a sequence of instructions (commands) separated by semicolons.

What instructions do we know (and could use in our programs so far)?

Only one (print line) and we'll add one in a second: print.

The Difference between print and println

What does the following program do?
class One {
    public static void main(String[] args) {
	System.out.println("One"); 
	System.out.println("Two"); 
	System.out.println("Three"); 
	System.out.println("Four"); 
	System.out.println("Five"); 
	System.out.println("Six"); 
    }
}
Let's change the program above to:
class One {
    public static void main(String[] args) {
	System.out.print("One"); 
	System.out.print("Two"); 
	System.out.print("Three"); 
	System.out.print("Four"); 
	System.out.print("Five"); 
	System.out.print("Six"); 
    }
}
So System.out.print(...) does not move the cursor to the next line.

Special Characters: ", \ and newline

Consider the following program:
class One {
    public static void main(String[] args) {
	System.out.println(". . . . . ."); 
	System.out.println(". . . . . ."); 
	System.out.println(". . . . . ."); 
	System.out.println(". . . . . ."); 
	System.out.println(". . . . . ."); 
	System.out.println(". . . . . ."); 
    }
}
Can you achieve the exact same result without using println?

Here's some help: what does the following program do?

class One {
    public static void main(String[] args) {
	System.out.print("\n\n\n\n"); 
    }
}
No, it doesn't print nothing. It prints four characters. (Really? Four?)

What does the following program do?

class One {
    public static void main(String[] args) {
	System.out.println("\""); 
	System.out.println("\"\""); 
	System.out.println("\"\"\""); 
	System.out.println("\"\"\"\""); 
    }
}
What does this program do? Explain.
class One {
    public static void main(String[] args) {
	System.out.print("\"\"); 
    }
}
What's to be done?

Operators and Expressions

Now that we understand printing, what can we print?

We can print numbers and expressions with numbers.

Try to predict what the next program will print and then run it:

class One {
    public static void main(String[] args) {
	System.out.println(1); 
	System.out.println(1 + 1); 
	System.out.println(2 * 3); 
	System.out.println(1 + 2 * 3); 
	System.out.println(2 * 3 + 1); 
	System.out.println(2 * (3 + 1)); 
	System.out.println(2 / 3); 
	System.out.println(3.14); 
	System.out.println(2.0); 
	System.out.println(2.0 / 3.0); 
	System.out.println(4 * 2 / 3); 
	System.out.println(4.0 * 2.0 / 3.0); 
	System.out.println(2 / 3 * 4); 
	System.out.println(2.0 / 3.0 * 4.0); 
	System.out.println(1 - 2 + 3); 
	System.out.println(1 - (2 + 3)); 
	System.out.println(1 + 2 - 3); 
	System.out.println(1 + (2 - 3)); 
	System.out.println(7 % 3); 
	System.out.println(4 % 5); 
	System.out.println(1 < 2); 
	System.out.println(1 > 2); 
	System.out.println(1 == 2); 
	System.out.println(2 == 2); 
	System.out.println(2 <= 2); 
	System.out.println(2 >= 2); 
	System.out.println("Larry" + "Bird"); 
	System.out.println("a" + "1"); 
	System.out.println("a" + 1); 
	System.out.println("1" + "1"); 
	System.out.println("1" + 1); 
	System.out.println(1 + 1); 
	System.out.println("1" + (1 + 1)); 
    }
}
OK, that's a lot.

Plenty of operators.

Take your time, have patience. Be persistent.

Ask for help and clarification.

Variables: Names for Locations

Here's how you can remember calculated values for later:
class One {
    public static void main(String[] args) {

	int n; 

	n = 1 + 1; 

	System.out.println(n); 

    }
}
Variables are names for values of a particular type.

Assignment statements associate a value with a particular name.

The association can change, so that's why the name used as such is the name of a variable.

You could think of it as the name of a location of variable content.

Two more examples will end our first set of experiments:

class One {
    public static void main(String[] args) {

	int n = 1; 

	n = n + 1; 

	System.out.println(n); 

    }
}
Variables need to be declared, and initialized.

The value in a variable can be updated.

In an assignment statement the right hand side is calculated first.

When the value of the expression is determined, it replaces the previous value of the variable.

So n receives a value of 1 + 1 = 2.

Note the difference between mathematical notation and the one used in programming.

One last thing:

class One{
    public static void main(String[] args) {

	int n; 

        n = 3.14; 

	System.out.println(n); 

    }
}
Why doesn't this work?

What can we do about it? (Answer: make n a double).


Last updated by Adrian German for A201/A597/I210 on Tue Jan 11 09:34:52 EST 2005