The Java programming language

(in slow motion)

Use the arrow keys to navigate. Press 'm' to see all the slides.

In the last class...



Please fill the survey to stop disasters from happening :)

Variables

Imagine we want to write a program about apples...


We will use variables to manipulate information about them

Variables will store information (about apples in our example)

Variables in Java have types

Primitive types: int, float, double, char, boolean
Object types: String
The int type is for whole numbers (integer numbers)
The float and double types are for real numbers
The char type is for single text characters (a letter, a punctuation symbol, etc)
The String type is for sequences of text characters (a name, an address, a short story, etc)
The boolean type is for assigning yes/no answers
Is the apple on the left fresh? Is the apple on the right fresh?

Declaring variables

To use variables in Java we need to declare them

TYPE variable_name;

TYPE variable_name = INITIAL_VALUE;



Sidenote:

Comments are ignored by the computer

Declaring variables

The int type

Declaring variables

The float type

Declaring variables

The double type

Declaring variables

The char type


char values go in single quotes

Declaring variables

The String type


String values go in double quotes

Declaring variables

The boolean type

Declaring variables

The boolean type

Operations with variables

We can assign values to variables


Operations with variables

We can accumulate values in variables


Operations with variables

We can do mathematical operations with number types

Binary: variable_1 OPERATOR variable_2

Unary:OPERATORvariable_1, variable_1OPERATOR

Operations with variables


Operations with variables


Operations with variables

We can also do some operations with strings

Operations with variables

We can also do some operations with strings

Logical Expressions

Statements for which we can answer yes or no

Determine the truth value of a statement: true or false


Logical Expressions

Comparisons are a kind of logical expressions

Logical Expressions

Comparisons are a kind of logical expressions






Logical Expressions

We can combine multiple logical expressions


R: The box contains red apples
G: The box contains green apples

What is the truth value of R && G for each box?

What is the truth value of R || G for each box?

Conditional statements

We can use the truth values of logical expressions to conditionally execute code

Example: A computer program for putting apples in the appropriate shelf space

Conditional statements

if the apple is rotten
then it goes in the garbage bin

else it goes in one of the shelves

Conditional statements

if the apple is rotten
then it goes in the garbage bin

else if it is not rotten
then it goes in one of the top shelves

Let's write that Java program!

The example code is available here!

Resources

/