0% found this document useful (0 votes)
0 views

JavaUnit2

This document serves as an introduction to Java programming, outlining the importance of attendance and engagement in learning. It covers the basics of how computers run programs, the structure of a Java program, and the use of the BlueJ IDE for writing code. Additionally, it explains the distinction between compile-time and run-time errors, and provides a simple example of a Java program that outputs 'Hello World'.

Uploaded by

phdscholar740
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

JavaUnit2

This document serves as an introduction to Java programming, outlining the importance of attendance and engagement in learning. It covers the basics of how computers run programs, the structure of a Java program, and the use of the BlueJ IDE for writing code. Additionally, it explains the distinction between compile-time and run-time errors, and provides a simple example of a Java program that outputs 'Hello World'.

Uploaded by

phdscholar740
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Java Unit 2:

Introduction to Java
Simon Powers
[email protected]

CSCU9P1
Attendance, engagement and success

• Good attendance = good outcomes

• Digital check-in at all your timetabled teaching sessions

• We will follow up to offer support and discuss your engagement with teaching and learning

• Attendance information helps us to support your success


Check in
• On the app, go to the “My Attendance” tile
• The check in code for today is

XV-FE-UZ
Lecture goals
1. Understand how computers run programs
2. Meet your first Java program
3. Meet the BlueJ programming environment
4. Learn about compile-time and run-time errors
Computer programs
• A program is a precise sequence of instructions
• Each instruction is very simple
• There are many different programming
languages
• A program to get a robot to draw a square might
look like:
REPEAT 4 times:
(1) Go forward 100 steps
(2) Turn left 90 degrees
The origin of computer programming
• Early computers were not known for their flexibility
• The program was built into the processor
• Some computers allowed the processor to be conveniently rewired

The Colossus computer


(image from Wikimedia)
The anatomy of a modern computer
• The central processing unit (CPU) performs program control and data
processing

• Storage devices include memory (RAM) and secondary storage


• Solid State Drives
• Hard Disk Drives
• USB Flash Drives

• Input/Output devices allow the user to interact with the computer


• Mouse, keyboard, printer, screen…
Schematic design of a PC

Java for Everyone 2nd ed, Horstmann, 2013

Copyright © 2013 by John Wiley & Sons. All


rights reserved.
When you ‘run’ a program
• Program instructions and data are stored in secondary storage (e.g. SSD)
• When a program is started, it is brought into memory, where the CPU can read it
• The CPU runs the program one instruction at a time. The program may react to
user input
• As directed by these instructions and the user, the CPU reads data, modifies it, and
writes it back to memory, the screen or secondary storage

Java for Everyone 2nd ed, Horstmann, 2013

Copyright © 2013 by John Wiley & Sons. All rights reserved.


Question
Which component(s) in a computer carry out the instructions that make up a
program?
a) CPU b) RAM
c) keyboard d) graphics card

Where might you find the instructions for a computer program when the computer is
switched off?
a) SSD / hard disk b) RAM
Programming languages
• The instructions in a program need to be unambiguous
• So languages like English are not suitable!

“You should bring a board game or an Xbox game and pizza”


Programming languages
• Programming languages solve this problem by establishing well-defined
building blocks, called primitives…

• …and a grammar specifying how they can be combined…

• …into an unambiguous ordered set of instructions, i.e. a program!

• We could write our programs with primitives that directly control the
processor…
A machine
language
program to
print out the
current date
and time
Higher-level languages
• Fortunately we don’t have to write
in the language of the processor….

• We can write in a language closer to


the way we think
• A compiler or interpreter will
convert this to the machine’s
language for us

• Note that each line is still


unambiguous – which is why it can
be converted to machine language
Some high-level languages
• C • COBOL
• C++ • Fortran
• Rust • (Visual) BASIC
• C# • Logo
• Java • Haskell
• Javascript • Erlang
• Python • Prolog
Java
• First developed in 1995, but still widely used

• It encourages you to write programs in a structured


(object-oriented) style

• It has features that make your life easier compared to


earlier languages like C++

• It is platform-independent
Your programming environment
• Programs are usually written in an Integrated Development (IDE).
• We will use the BlueJ IDE in this module
• Later modules will use the Eclipse IDE
• You enter your program in the source code editor, which helps you by:
• Colouring parts of the code
• Showing line numbers
• Auto formatting
• IDEs also have an output window, and a debugger.
The BlueJ IDE
Main BlueJ Window
Editor

Output
Your first program
• Display the message “Hello World” on the screen

• Have a go at typing it out in BlueJ!


• Watch the spelling, case sensitivity, and special characters like {
Compiling and running a Java program
• Java programs are compiled to bytecode (the machine language of the Java Virtual
Machine)

Hello.class Java Virtual


Hello.java javac java
(bytecode) Machine
What does this all mean?
1: Declares a ‘class’ Hello
-- Every Java program has one or
more classes.
3: Declares a method called ‘main’
-- Every Java application has exactly
one ‘ main’ method
-- Entry point where the program
starts
5: Method System.out.println
outputs ‘Hello, World!’
-- A statement must end with a
semicolon (;)
We’ve just called a Java Library method!
• Line 5 shows how to call a ‘method’ from the Java API: System.out.println
• API: Code that somebody else wrote for you!
• Parentheses surround the arguments that you ‘pass’ to a method
• We are passing a String “Hello World”
Note the double quotes which denote a String inside
• You can also print numerical values
System.out.println(3 + 4);
• There is a cousin method, System.out.print(), that doesn’t print a newline
after the output.
The two categories of errors
Compile-time errors Run-time errors (bugs)
• Often syntax errors • Logic errors
• Missing ;s • Program runs but…
• Missing { }s • Gives incorrect output
• Misspelt keywords • Or crashes 
• No .class file is generated by the
compiler
Hello world in other languages
Python C
We’ve covered…
• What a programming language is
• How computers run programs
• A Java program to print “Hello world” to the screen
• Errors!

• Additional reading: Chapter 1 of Java for Everyone.

You might also like