CS II Lesson 1

Professor Abdul-Quader

L1 Variables / Data Types

Office Hours

  • Mondays and Thursdays, 2:45 - 4:15 PM NSB 3003
  • Rachel (Learning Assistant) will put her office hours on Moodle as well

Errors Discussion

  • Lesson 1 on Replit
  • Remove spaces before System.out.println?
  • Remove line before public static void main?
  • Remove void?
  • Change the name of the class?

Variables

  • What is a variable?
  • What is a data type?

Examples

int x; // declaration of variable x, type int
int a = 1000; // declaration + assignment
// 1000 is called a literal

String name = "Athar";
string name = "Athar"; // compiler error

Variables

Variable: named location in memory.

int x;
  • Associates 4 bytes of memory with the variable \(x\).
  • The type of the variable is int.
  • Can do integer type operations with \(x\).
  • Difference vs p5?

Primitive Data Types

Primitive type:

  • data type that is built in to the language.
  • JVM knows exactly how much memory to allocate.
  • Examples:
    • int: (most common integer type) 32-bit integer
    • long: 64-bit integer
    • short: 16-bit integer
    • byte: 8-bit integer

Other Primitive Types

  • float: 32-bit floating point number (decimal)
  • double: 64-bit floating point number (most common decimal type)
  • boolean: true or false
  • char: 16-bit character (‘a’ or “!”)
  • String?
    • Built-in, but not primitive. Why?
    • class, not primitive; we will study classes later

Exercise

What are the values of \(a\) and \(b\) after the following code executes:

int a, b;
a = 1234;
b = 99;
int t = a;
a = b;
b = t;

Memory Diagrams

Reading code

  • Whenever you read code, make a memory diagram
  • Also called “state table”.
  • Keep track of current values of variables.
  • Particularly important when we get to loops.
  • Reading code is an important standard of this course:
    • Helps you understand code written by others (say, on your dev team)
    • Helps you understand code you wrote yourself, long ago!

Operations

If you read chapter 2: what are some operations that we can do with variables (and/or literals)?

  1. Assignment
  2. Arithmetic operations
  3. Compound operations
  4. String operations

Assignment

Which of the following are legal / illegal:

// 1:
int x;
x = 7;
// 2:
int x;
x = 7.0
// 3:
int x;
7 = x;
// 4:
int x = 7;
int y = x;

Arithmetic operations

Try these out:

int num1 = 5;
int num2 = 10;
System.out.println(num1 + num2);
System.out.println(num1 - num2);
System.out.println(num1 * num2);
System.out.println(num1 / num2);
System.out.println(num1 % num2); // remainder operator

Do any of these output anything different from what you expected?

Arithmetic

  • Arithmetic operations are defined on all numeric types.
  • Default: Java uses the least amount of memory possible.
  • If both sides of an operator are int’s, then the result is stored as an int!
  • What will the following do:
System.out.println(3 / 2);

Question

How do we make sure Java processes “3 / 2” as a decimal expression?

  1. System.out.println(3.0 / 2); or,
  2. System.out.println( (double) 3 / 2);

The second version is called an explicit cast. We tell Java to treat 3 (an integer) as a double.

Expressions / statements

In small groups (two or three, or whatever is convenient at your table). What are all the differences between:

  • x + 7 and
  • y = x + 7;

Based on previous coding knowledge / experience: which of these is a valid statement?

Compound assignment

We often want to assign a variable a value that is based on its current value. For example:

int count = 17;
// some other code happens, requiring us to increase count by 1
count = count + 1;

This is so common that many languages (including Java) have compound assignment operators: +=, -=, *=, /= and %=. \(x += y\) means assign the value of \(x + y\) to the variable \(x\).

Example

On paper: make a memory diagram and determine the final values of \(x\) and \(y\).

int x = 4;
int y = x - 5;
y *= 2;
y += x;

Increment / Decrement

int x = 29;
x++;
int y = x - 5;
--y; // can put before or after

Values of \(x\) and \(y\) after this? Memory diagram.

Trivia: Difference between \(x++\) and \(++x\)?

Doubles

double x = 3; // why does this work?
System.out.println(x);

JVM can automatically convert int to double: no loss of information. Other way around: need an explicit cast:

double euler = 2.718281828;
int e = (int) euler;

Warning

double x = 0.7;
double y = 0.9;
double a = x + 0.1;
double b = y - 0.1;
System.out.println(a);
System.out.println(b);

What do we think this will output? What does it output?

Floating point values often have rounding errors. (Why?) Solution? (Avoid doubles and floats if possible)

Exercise

Chapter 2, Exercise 2

  1. Write a program that creates variables named day, date, month, and year. Assign values to those variables that represent today’s date.
  2. Display the value of each variable on a line by itself. Compile and run your program before moving on.

Continued

  1. Modify the program so that it displays the date in standard American format, for example: Thursday, July 16, 2015.
  2. Modify the program so it also displays the date in European format. The final output should be:
American format:
Thursday, July 16, 2015
European format:
Thursday 16 July 2015

Submit this on Replit before next class!

Homework

  1. Submit the replit assignment.
  2. Try out Chapter 2, Exercise 3. No need to submit. Similar exercise together on Monday.
  3. Read Chapters 2 (variables / data types) and 3 (getting input).

Coming up

By now, we know how to:

  1. Store data in variables
  2. Manipulate that data (arithmetic operations)
  3. Output that data.

Coming up:

  1. Getting data from the user (input)
  2. Comparisons, if-else, and loops
  3. Arrays: lists of data
// reveal.js plugins