The Java programming language

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

In the last class...

Our first introduction to programming

Scratch! (click)

What is a programming language?

A way of translating algorithms into instructions executable by a computer

Syntax with instructions to be transformed into a machine code (i.e. binary), executable by a computer

How does our example's machine code look like?

Trying to understand a piece of machine code ~ Trying to understand a piece of DNA by looking at its atoms (Hofstadter, 1979)

Transforming Java text into machine code

This week

  1. Variables

    Primitive types

    Assignment of values

    Declarations

  2. The structure of a Java program

    Everything lives inside a class

    Methods

    Basic Input and Output

  3. Control statements

    Conditionals

    Loops

  4. Data structures

    Arrays

Variables

Placeholders for data

Variables are how we represent data stored in memory

In Java, variables have types

  1. True/False → boolean
  2. Integer numbers → int
  3. Real numbers → double
  4. Single text characters → char
  5. Words or sentences → String

Variables

Placeholders for data

Variables are how we represent data stored in memory

Variables need to be declared before using them

Variables can be used in operations
  • Binary (variables of compatible types): a+b, a+y, a-b, a/b, a*b, a%b, p|q, p&q
  • Unary: !w, a+, a--
Assignments of values:
  • From other variables a=b, a+=b, a-=b, x=y,
  • From literals a=10, p=false, y=1.5

How much space do variables take?

Variables are how we represent data stored in memory

Data is stored in binary

We use bytes to measure an amount of binary data

1 byte: 8 binary digits, or bits

8 bits can represent numbers from 0000 0000 to 1111 1111

Sizes of variables in Java

  1. boolean → 1 bit (The actual size is not this)
  2. int → 4 bytes
  3. double → 8 bytes
  4. char → 2 bytes
  5. String of n characters → 2*n bytes

What is the range of numbers we can represent with an int variable?

How are real numbers represented with the bits of an double variable?

The structure of a Java program

Methods

Basic input and output

Resources

/