CS 149
Professor: Alvin Chao
Programming is the art of telling another human being what one wants the computer to do
Compiling a Java Program
Java Primitive Types
Variables and memory
Example from Textbook chapter 2 section 3.
1 int a = 5;
2 int b = a; // a and b are now equal
3 a = 3; // set a to something new
4 int c = 0;
Memory diagram:
Line 1
a [ 5 ]
Line 2
b [ 5 ]
Line 3
a [ 3 ]
b [ 5 ]
Line 4
a [ 3 ]
b [ 5 ]
c [ 0 ]
Plus sign operator
Example similar to textbook chapter 2.4
1 public class HourMinute {
2
3 public static void main(String[] args) {
4 int hour = 11;
5 int minute = 59;
6 System.out.print("The current time is ");
7 System.out.print(hour);
8
9 System.out.print(":");
10 System.out.print(minute);
11 System.out.println(".");
12
13 System.out.println("hour + minute = " + hour + minute); // plus sign used to concatenate(put together) two strings
14 int sum = hour + minute; // plus sign used to add two numbers
15 System.out.println("sum = " + sum);
16 }
17
18 }
What is the output?
Integer versus ‘regular’ Division
Example similar to textbook chapter 2.6
int min = 59;
System.out.print("Fraction of the hour that has passed: ");
System.out.println(min / 60);
double minute = 59.0;
System.out.print("Fraction of the hour that has passed: ");
System.out.println(minute / 60.0);
What is the output?
</end>