Lecture 15 Slides
Download
Report
Transcript Lecture 15 Slides
Programming for Beginners
Lecture 15: More-Advanced Concepts
Martin Nelson
Elizabeth FitzGerald
Session 15 – aims and objectives
Review of the course to date.
Brief introduction to some more advanced topics.
Where to go next?
Chance to wrap up anything that’s not been clear so far.
Recap of the course so far!
We now know LOADS about Java!
Most of the Java concepts we’ve talked about transfer
directly to other languages.
We’ve seen different types of data:
int, double, String, boolean, char, char[], objects,...
We know how to design and structure codes
Pen & paper, pseudocode, testing, successive refinement.
We now know the difference between procedural and
object oriented programming.
We’ve seen a bit of procedural C++ and some scripting
languages.
Structure of a Java program
We’ve seen this many times now...
class myprog
{
public static void main(String[] args)
{
System.out.println(“...”);
}
}
But what does it all mean?
Structure of a Java program
Every Java program is a class.
class myprog
Here is the class’ name.
{
public static void main(String[] args)
{
System.out.println(“...”);
}
}
Every Java application must have a method called main,
which is run as soon as the application is started.
Structure of a Java program
Return type
(void by convention)
Name of the method
public static void main(String[] args)
main has to be public
i.e. Accessible outside
of class myprog
main is static so that
it can be used without
creating an instance of
class myprog
Argument list
Convention: main always receives
an array of Strings.
Using String[] args
class myprog
{
public static void main(String[] args)
{
for(int i=0; i<args.length; i++)
{
System.out.println(args[i]);
}
All arrays have a length
variable representing the
number of elements
}
}
To compile and run:
javac myprog.java
java myprog here is some text
Using String[] args
What if you want to pass in something other than Strings?
main() can only receive Strings!
Need some way of converting Strings to, say, integers...
Can do this using Wrapper classes.
Wrapper classes
We have seen that classes (e.g. Strings) are really useful
as they have loads of associated methods which we can
use.
Primative data types (e.g. int, char, double, etc.) don’t
have such methods.
However, wrapper classes can be used to `wrap’ a
primative variable into an object.
They provide more methods & greater flexibility.
Wrapper classes
int can be wrapped as an Integer object.
double can be wrapped as a Double object.
char can be wrapped as a Character object.
Include methods to convert the numeric value of a String
into an Integer, and then convert an Integer back to an
int.
It is your responsibility to check that this conversion is
sensible.
For more info, see the Core Class documentation, or come
to the next Java course!
Arrays vs. Vectors
Arrays can store a number of variables of the same type
in a logical manner.
We must know the maximum number of variables to store
in advance!
What if we don’t know the maximum number of
variables?
Could just declare a HUGE array, but this wastes memory and
risks causing a bug.
Instead of arrays, we can use Vectors.
Vectors can increase/decrease in size dynamically.
Vectors
Import the java.util.Vector class:
import java.util.Vector;
Instantiate a new Vector:
Vector numbers = new Vector();
Add a new element:
numbers.addElement(3);
Get an element (the 0th element in this case):
numbers.elementAt(0)
Delete an element (the 7th element in this case):
numbers.remove(7);
See the Core Class documentation for more details.
Other uses of Java
Graphical programming
External libraries java.awt and java.swing can be used to create
graphical programs.
The JVM makes program design independent of the operating
system.
Min/Max/Close buttons are automatic, for example!
Applets
Small applications which are distributed over the web.
Byte code is held remotely on a server.
A JVM is built in to most modern web browsers.
Must have a java enabled browser to work!
Memory Issues
When designing code, we should always try to minimise
the amount of memory used.
Not as much of a problem these days as memory is more
abundant.
For long running, high intensity jobs, memory
management is more important.
Numerical simulations and computations, Matlab codes...
Things to think about...
Am I storing data that I don’t need?
Am I looping un-necessarily? (Very expensive)
What do I do with data after I am finished with it?
Memory Issues
In general, the computer’s memory is divided into three
segments:
The Code Segment
The Stack Segment
Where the compiled code itself is stored.
Used for variables which are created as functions are started.
The Heap Segment
Dynamic memory allocation.
This is a bit more complicated in Java!
The Stack
The stack segment is used to store variables which are
created as a function runs.
When a new function starts, a new block of memory is
reserved for that function’s data.
Minimal control of how much memory is allocated.
Memory is cleared in a last in – first out manner.
Can’t release memory of func1 until func2 ends
func2()
main()
func1()
func1()
func1()
main()
main()
main()
main()
The Heap
Memory is allocated dynamically.
Slower than the stack, as memory allocation is more
complicated.
Much more memory can be made available, and memory
which is no longer needed can be released straight away.
Heap data is accessed via its memory address using
pointers.
Heap data survives after
function which created it
ends... until you delete it!
the
Data
Data
Data
Data
Data
Data
Pointer
Data
Pointers & Dynamic Memory
Pointers are used a lot in languages such as C++.
Some people find pointers very complicated.
When using dynamic memory, you must always explicitly
delete data when it is finished with, or the memory
cannot be re-used.
Failure to empty memory is known as a memory leak.
They will often cause your program to crash as memory
runs out!
Pointers & Dynamic Memory
We haven’t seen pointers in Java. Why?
Or, more accurately, everything in java is a pointer, so you never
have to worry about them.
In java, everything is stored on the heap.
Big advantage of Java: Memory management is
automatic.
Java doesn’t have them!
Don’t have to worry about deleting variables when they are
finished with. Not the case with C++.
To find out more about pointers and dynamic memory
allocation, sign up for the forthcoming C++ programming
course...
Forthcoming Courses
C++ Programming
Ten Sessions: 12th April – 17th May 2011
Tutor: Dr. Robert Oates ([email protected])
Object orientation, pointers, efficient use of memory etc.
Introduction to Java Programming
Ten Sessions: 7th June – 7th July 2011
Tutor: Dr. Martin Nelson ([email protected])
Vectors, wrapper classes, graphics, applets
More info: www.nottingham.ac.uk/csc
Course Evaluation
Please fill in the course evaluation survey before you leave.