1 of 7

CS 149

Professor: Alvin Chao

Programming is the art of telling another human being what one wants the computer to do

  • Donald Knuth

2 of 7

Compiling a Java Program

3 of 7

Java Primitive Types

4 of 7

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 ]

5 of 7

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?

6 of 7

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?

7 of 7

  • Acknowledgements
  • Parts of this activity are based on materials developed by Helen Hu and Urik Halliday, modified by Chris Mayfield and Nathan Sprague, and licensed under CC BY-NC 4.0 International

</end>