1. Variables, Functions, and Loops

Douglas Blank, Bryn Mawr College, CS206, Spring 2016

Topics:

  • Variables and Types
  • Primitive Java Types
  • What is a byte?
  • What is a nibble?
  • What is a bit?
  • History of Java
  • Functions and Classes
  • Loops

1.1 Variables and Types

To create a variable, there is always an associated "type", and sometimes a "value."

TYPE NAME;
TYPE NAME = VALUE;

The first form:

  • creates a variable with a specified type

The second form does two things:

  • creates a variable with a specified type
  • initializes the variable with the given value
In [1]:
int x;                   // These do not initialize
String s;
boolean first_time;
byte b;
double total;
|  Added variable x of type int

|  Added variable s of type String

|  Added variable first_time of type boolean

|  Added variable b of type byte

|  Added variable total of type double

In [3]:
void println(Object thing) {
    System.out.println(thing);
}

void print(Object thing) {
    System.out.print(thing);
}
|  Added method println(Object)


|  Added method print(Object)

In [6]:
{
    println(x);
    println(s);
    println(first_time);
    println(b);
    println(total);
}
0
null
false
0
0.0

In [7]:
int x = 5;
String s = "Hello";
boolean first_time = true;
byte b = 0;
double total = 0.0;
|  Modified variable x of type int with initial value 5

|  Modified variable s of type String with initial value "Hello"

|  Modified variable first_time of type boolean with initial value true

|  Modified variable b of type byte with initial value 0

|  Modified variable total of type double with initial value 0.0

Expressions and Statements

Java is composed of "expressions", "statements" and "blocks".

  • An expression is something that evaluates to a value
  • A statement is a line that ends in a semicolon
  • A block is a group of statements that starts and stops curly-braces
In [8]:
// expressions

1 + 2
true
(1 + 2) * 3

|  Expression value is: 3
|    assigned to temporary variable $20 of type int

|  Expression value is: true
|    assigned to temporary variable $21 of type boolean

|  Expression value is: 9
|    assigned to temporary variable $22 of type int

Out[8]:
9
In [9]:
// statements

int x = 1 + 2 * 3;
x = 1 /2;

|  Modified variable x of type int with initial value 7

|  Variable x has been assigned the value 0

In [ ]:
// blocks

{
    printf("Hello");
    printf("World");
}

1.1.5 Bits and Bytes

What's a bit?

  • 8 bits - "binary digit"
  • has the value 0 or 1

All data, numbers, music, movies, etc. are at the bottom made up of 0s and 1s.

We group bits into groups:

  • 8 bits are called a byte
  • 4 bits is called a nibble

We can represent integers by allowing each position in a byte to represent the numbers of powers of two:

2 to the 7 2 to the 6 2 to the 5 2 to the 4 2 to the 3 2 to the 2 2 to the 1 2 to the 0 Integer
128 64 32 16 8 4 2 1
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1
1 1 1 1 1 1 1 1 255
In [10]:
import java.lang.Math;

(Math.pow(2, 7) * 1 +  
 Math.pow(2, 6) * 1 +  
 Math.pow(2, 5) * 1 +  
 Math.pow(2, 4) * 1 +  
 Math.pow(2, 3) * 1 +  
 Math.pow(2, 2) * 1 +  
 Math.pow(2, 1) * 1 +  
 Math.pow(2, 0) * 1)

|  Expression value is: 255.0
|    assigned to temporary variable $26 of type double

Out[10]:
255.0
In [11]:
double bin2int(int b8, int b7, int b6, int b5, 
                    int b4, int b3, int b2, int b1) {
    return (Math.pow(2, 7) * b8 +  
             Math.pow(2, 6) * b7 +  
             Math.pow(2, 5) * b6 +  
             Math.pow(2, 4) * b5 +  
             Math.pow(2, 3) * b4 +  
             Math.pow(2, 2) * b3 +  
             Math.pow(2, 1) * b2 +  
             Math.pow(2, 0) * b1);
}
|  Added method bin2int(int,int,int,int,int,int,int,int)

In [14]:
bin2int(0, 1, 0, 1, 0, 1, 1, 1)
|  Expression value is: 87.0
|    assigned to temporary variable $30 of type double

Out[14]:
87.0
In [15]:
int bin2int(int b8, int b7, int b6, int b5, 
                    int b4, int b3, int b2, int b1) {
    double d = (Math.pow(2, 7) * b8 +  
                 Math.pow(2, 6) * b7 +  
                 Math.pow(2, 5) * b6 +  
                 Math.pow(2, 4) * b5 +  
                 Math.pow(2, 3) * b4 +  
                 Math.pow(2, 2) * b3 +  
                 Math.pow(2, 1) * b2 +  
                 Math.pow(2, 0) * b1);
    return ((int)d);
}
|  Replaced method bin2int(int,int,int,int,int,int,int,int)
|    Update overwrote method bin2int(int,int,int,int,int,int,int,int)

In [16]:
bin2int(1, 1, 1, 1, 0, 0, 1, 1)
|  Expression value is: 243
|    assigned to temporary variable $32 of type int

Out[16]:
243

Fill out the following table:

2 to the 7 2 to the 6 2 to the 5 2 to the 4 2 to the 3 2 to the 2 2 to the 1 2 to the 0 Integer
128 64 32 16 8 4 2 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0
1 0 0 0 1 0 1 0
0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1
7
22
113
256

Signed integers:

  • saves the left-most bit to represent the "sign" of the number: 0 is positive, 1 is negative
  • Pisces is not a sign here. Only 0 and 1

Unsigned integers:

  • All bits are used to represent the number (eg, another power of two)

Challenge

Make a new function bin2signedint that will turn a series of bits into a signed integer.

  • What numbers can you no longer represent?
  • What numbers can you now represent?

Nibbles

Two "nibbles", each have a range between 0 and 15

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15

0001 1010

The first nibble (on the left) is worth 16 value, the second nibble is worth one value

1111 1111

(15 * 16) + 15 = 255

Nibbles in Binary, Decimal, and Hexadecimal

0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
1010 10 A
1011 11 B
1100 12 C
1101 13 D
1110 14 E
1111 15 F

1.1.3 Java Primitive Types

Type Value Size Range
boolean true or false 1 byte true or false
char character (unicode) 2 bytes Any Unicode character, 0 to 65,535
byte integer 1 byte -128 to 127, or 0 to 255
short integer 2 bytes -32,768 to 32,767
int integer 4 bytes -2,147,483,648 to 2,147,483,647
long integer 8 bytes -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
float floating-point 4 bytes 1.4e-45 to 3.4e38 (+/-)
double floating-point 8 bytes 4.94e-324 to 1.79e+308 (+/-)

1.1.4 Type-checking

In [17]:
int x = 1;
x = "hello";  // ERROR!
|  Modified variable x of type int with initial value 1

|  Error:
|  incompatible types: java.lang.String cannot be converted to int
|  x = "hello";  // ERROR!
|      ^-----^

In [ ]:
int y = 1;
y = 2;

1.1.6 T-Shirt and a Joke

1.1.7 Java History

  1. Created by Sun Microsystems team led by James Gosling (1991)
  2. Originally designed for programming home appliances; originally called "Oak"
    • "A solution looking for a problem"
    • Difficult task because appliances are controlled by a wide variety of computer processors
    • Writing a compiler (translation program) for each type of appliance processor would have been very costly
    • Solution: two-step translation process
      • compile for a Virtual Machine (VM)
      • interpreter on VM written for each real machine
  3. Java now owned by Oracle

1.1.8 Functions and Classes

In [ ]:
class SomeType {
    SomeType() {
        // constructor code!
    }
}

SomeType thing = new SomeType();
In [ ]:
class SomeOtherType {
    static void main() {
        System.out.println("Hi!");
    }
}

SomeOtherType.main();

1.1.9 Loops and Ifs

Sometimes called "control blocks" or "flow of control"

  • if-else blocks
  • for loops
  • while loops
  • do-while loops

if-else blocks

if (TEST) {
    STATEMENT;
    ...
} else if (TEST) {
    STATEMENT;
    ...
} else {
    STATEMENT;
    ...
}
if (x == 0) {
    STATEMENT;
    ...
} else if (x == 1) {
    STATEMENT;
    ...
} else {
    STATEMENT;
    ...
}

1.1.10 Incrementors and Decrementors

Straightforward:

i = i + 1;
i = i - 1;

Incrementors and decrementors:

  • optimized
// post increment
i++; 
i--;
// pre increment
++i; 
--i;

Incrementors and decrementors are expressions:

x = i++ + 7;
x = ++i + 7;

1.1.11 for loops

for (INIT; TEST; UPDATE) {
    STATEMENT;
    ...
}
In [ ]:
for (int i = 0; i < 10; i = i+1) {
    System.out.println(i);
}

1.1.12 while loops

INIT;
while (TEST) {
    STATEMENT;
        ...
    UPDATE;
}
In [ ]:
int i = 0;
while (i < 10) {
    System.out.println(i);
    i++;
}

1.1.13 do-while loops

INIT;
do {
   STATEMENT;
   ...
   UPDATE;
} while (TEST);
In [ ]:
boolean getStatus() {
    counter++;
    return (counter != 10);
}

boolean trapped; 
int counter = 0;
do {
   System.out.println("Help!");

   trapped = getStatus();
} while (trapped);