Introduction to Java Programming Lecture 1 Lecturer Maxim

  • Slides: 24
Download presentation
Introduction to Java Programming Lecture 1 Lecturer Maxim Rudenko

Introduction to Java Programming Lecture 1 Lecturer Maxim Rudenko

Amount of Hours Themes Common amount of hours Lectures Practice Laboratory Hours for Seld-study

Amount of Hours Themes Common amount of hours Lectures Practice Laboratory Hours for Seld-study Individual task Semester 6 Module 3 1. Introduction in Java. 12 2 2 - 8 - 2. Packages in Java. 12 2 2 - 8 - 3. Basic Java Syntax. Simple data types. Arrays. 12 2 2 - 8 2 4. Operators in Java. 12 2 2 - 8 2 Module test 2 - - - 2 - Total 50 8 8 0 34 0

Score Theme Points 24 1 Attendance (12 lectures and 12 practices on 1 points)

Score Theme Points 24 1 Attendance (12 lectures and 12 practices on 1 points) 2 Practices (12 practices on 2, 5 points) 30 3 Individual homework 8 4 Module test 38 Total 100

Overview n Introducing Java ¨ Key n features of the language Principles of Object

Overview n Introducing Java ¨ Key n features of the language Principles of Object Oriented Programming ¨ What is OOP? Why is it important? ¨ Basic principles and advantages

Some History n Developed and maintained by Sun Microsystems ¨ Originally called Oak ¨

Some History n Developed and maintained by Sun Microsystems ¨ Originally called Oak ¨ Aimed at producing an operating environment for networked devices and embedded systems ¨ …but has been much more successful n Design objectives for the language ¨ Simple, object-oriented, ¨ Distributed, multi-threaded, and platform neutral ¨ Robust, secure, scaleable

The Virtual Machine n Java is both compiled and interpreted Source code is compiled

The Virtual Machine n Java is both compiled and interpreted Source code is compiled into Java bytecode ¨ Which is then interpreted by the Java Virtual Machine (JVM) ¨ Therefore bytecode is machine code for the JVM ¨ n Java bytecode can run on any JVM, on any platform ¨ n …including mobile phones and other hand-held devices Networking and distribution are core features In other languages these are additional APIs ¨ Makes Java very good for building networked applications, server side components, etc. ¨

Features of the JVM n The Garbage Collector Java manages memory for you, the

Features of the JVM n The Garbage Collector Java manages memory for you, the developer has no control over the allocation of memory (unlike in C/C++). ¨ This is much simpler and more robust (no chance of memory leaks or corruption) ¨ Runs in the background and cleans up memory while application is running ¨ n The Just In Time compiler (JIT) Also known as “Hot Spot” ¨ Continually optimises running code to improve performance ¨ Can approach the speed of C++ even though its interpreted ¨

Features of the JVM n Security Java offers very fine control over what an

Features of the JVM n Security Java offers very fine control over what an application is allowed to do ¨ E. g. Read/write files, open sockets to remote machines, discover information about the users environment, etc ¨ Used in Java Applets to create a “sandbox”. Stops a rogue applet attacking your machine. ¨ Makes Java very safe, an important feature in distributed systems ¨ n Class Loading of bytecode into the virtual machine for execution ¨ Code can be read from a local disk, over a network, or the Internet ¨ Allows downloading of applications and applets on the fly ¨ …and even ‘mobile code’ ¨

Versions of Java n Java Language vs Java Platform Current version of the language

Versions of Java n Java Language vs Java Platform Current version of the language is 1. 4. 1 ¨ Core language plus additional APIs is called the Java 2 platform ¨ Three versions of the Java 2 Platform, targetted at different uses ¨ n Java 2 Micro Edition (J 2 ME) Very small Java environment for smart cards, pages, phones, and set-top boxes ¨ Subset of the standard Java libraries aimed at limited size and processing power ¨ n Java 2 Standard Edition (J 2 SE) ¨ n The basic platform, which this course will cover Java 2 Enterprise Edition (J 2 EE) ¨ For business applications, web services, mission-critical systems

The Java APIs n n Sun are constantly adding new features and APIs The

The Java APIs n n Sun are constantly adding new features and APIs The Core Java API is now very large ¨ n Separate set of extension APIs for specific purposes ¨ n Often difficult to keep up with every change E. g. Telephony, Web applications, Game programming All new developments reviewed through Java Community Process (http: //www. jcp. org) Chance for developers to provide feedback on emerging standards and APIs ¨ Useful to keep an eye on what's coming through ¨ n Also a wide range of “open source“ APIs available ¨ E. g. through the Jakarta project (http: //jakarta. apache. org)

Useful Resources n n Useful resources on the web Java home (http: //java. sun.

Useful Resources n n Useful resources on the web Java home (http: //java. sun. com) ¨ n Java Developer Services http: //developer. java. sun. com ¨ n Early access downloads, forums, newsletters, bug database Javaworld (http: //www. javaworld. com) ¨ n Articles, Software and document downloads, Tutorials Java magazine site, good set of articles and tutorials IBM developer. Works (http: //www. ibm. com/developer. Works) ¨ Technology articles and tutorials

Object-Oriented Programming n Understanding OOP is fundamental to writing good Java applications Improves design

Object-Oriented Programming n Understanding OOP is fundamental to writing good Java applications Improves design of your code ¨ Improves understanding of the Java APIs ¨ n There are several concepts underlying OOP: ¨ ¨ ¨ Abstract Types (Classes) Encapsulation (or Information Hiding) Aggregation Inheritance Polymorphism

What is OOP? n n Modelling real-world objects in software Why design applications in

What is OOP? n n Modelling real-world objects in software Why design applications in this way? ¨ We naturally classify objects into different types. ¨ By attempting to do this with software aim to make it more maintainable, understandable and easier to reuse n In a conventional application we typically: ¨ decompose it into a series of functions, ¨ define data structures that those functions act upon ¨ there is no relationship between the two other than the functions act on the data

What is OOP? n How is OOP different to conventional programming? ¨ Decompose the

What is OOP? n How is OOP different to conventional programming? ¨ Decompose the application into abstract data types by identifying some useful entities/abstractions ¨ An abstract type is made up of a series of behaviours and the data that those behaviours use. n Similar to database modelling, only the types have both behaviour and state (data)

Abstract Data Types n Identifying abstract types is part of the modelling/design process The

Abstract Data Types n Identifying abstract types is part of the modelling/design process The types that are useful to model may vary according to the individual application ¨ For example a payroll system might need to know about Departments, Employees, Managers, Salaries, etc ¨ An E-Commerce application may need to know about Users, Shopping Carts, Products, etc ¨ n Object-oriented languages provide a way to define abstract data types, and then create objects from them It’s a template (or ‘cookie cutter’) from which we can create new objects ¨ For example, a Car class might have attributes of speed, colour, and behaviours of accelerate, brake, etc ¨ An individual Car object will have the same behaviours but its own values assigned to the attributes (e. g. 30 mph, Red, etc) ¨

Encapsulation n The data (state) of an object is private – it cannot be

Encapsulation n The data (state) of an object is private – it cannot be accessed directly. The state can only be changed through its behaviour, otherwise known as its public interface or contract This is called encapsulation

Encapsulation n Main benefit of encapsulation Internal state and processes can be changed independently

Encapsulation n Main benefit of encapsulation Internal state and processes can be changed independently of the public interface ¨ Limits the amount of large-scale changes required to a system ¨

What is an OO program? n What does an OO program consist of? A

What is an OO program? n What does an OO program consist of? A series of objects that use each others behaviours in order to carry out some desired functionality ¨ When one object invokes some behaviour of another it sends it a message ¨ In Java terms it invokes a method of the other object ¨ A method is the implementation of a given behaviour. ¨ n OO programs are intrinsically modular Objects are only related by their public behaviour (methods) ¨ Therefore objects can be swapped in and out as required (e. g. for a more efficient version) ¨ This is another advantage of OO systems ¨

Aggregation n Aggregation is the ability to create new classes out of existing classes

Aggregation n Aggregation is the ability to create new classes out of existing classes ¨ n Aggregation allows reuse of existing code ¨ n n “Holy Grail” of software engineering Two forms of aggregation Whole-Part relationships ¨ n Treating them as building blocks or components Car is made of Engine, Chassis, Wheels Containment relationships A Shopping Cart contains several Products ¨ A List contains several Items ¨

Inheritance n Inheritance is the ability to define a new class in terms of

Inheritance n Inheritance is the ability to define a new class in terms of an existing class The existing class is the parent, base or superclass ¨ The new class is the child, derived or subclass ¨ n The child class inherits all of the attributes and behaviour of its parent class It can then add new attributes or behaviour ¨ Or even alter the implementation of existing behaviour ¨ n Inheritance is therefore another form of code reuse

Polymorphism n n Means ‘many forms’ Difficult to describe, easier to show, so we’ll

Polymorphism n n Means ‘many forms’ Difficult to describe, easier to show, so we’ll look at this one in a later lesson In brief though, polymorphism allows two different classes to respond to the same message in different ways E. g. both a Plane and a Car could respond to a ‘turn. Left’ message, ¨ n however the means of responding to that message (turning wheels, or banking wings) is very different for each. Allows objects to be treated as if they’re identical

Summary! n In OO programming we Define classes ¨ Create objects from them ¨

Summary! n In OO programming we Define classes ¨ Create objects from them ¨ Combine those objects together to create an application ¨ n Benefits of OO programming Easier to understand (closer to how we view the world) ¨ Easier to maintain (localised changes) ¨ Modular (classes and objects) ¨ Good level of code reuse (aggregation and inheritance) ¨

Thank you

Thank you