G52PGP. Lecture oo3 Java (A real object oriented language)

Similar documents
Java Bytecode (binary file)

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Introduction to Java

Introduction to Programming (Java) 2/12

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

Lecture 1: Overview of Java

CHAPTER 1. Introduction to JAVA Programming

Program Fundamentals

Getting Started with Java. Atul Prakash

Computer Components. Software{ User Programs. Operating System. Hardware

C02: Overview of Software Development and Java

Learning objectives. The Java Environment. Java timeline (cont d) Java timeline. Understand the basic features of Java

DM550 Introduction to Programming part 2. Jan Baumbach.

Kickstart Intro to Java Part I

Compiling Techniques

An overview of Java, Data types and variables

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

Fall 2017 CISC124 9/16/2017

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Programming. Syntax and Semantics

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

Java Programming. Manuel Oriol, March 22nd, 2007

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CompSci 125 Lecture 02

Programming Language Concepts: Lecture 2

CSC116: Introduction to Computing - Java

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

II. Compiling and launching from Command-Line, IDE A simple JAVA program

Pace University. Fundamental Concepts of CS121 1

COT 3530: Data Structures. Giri Narasimhan. ECS 389; Phone: x3748

Inf1-OP. Course Overview. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

17 Hello world 18 Type: String: literal 19 Standard API: System: out.println() 20 Hello world 21 Statement 22 Statement: simple statements are ended w

List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs ar

Class Information ANNOUCEMENTS

CS 231 Data Structures and Algorithms, Fall 2016

Variables and Java vs C++

Algorithms and Programming I. Lecture#12 Spring 2015

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Chapter 1 Getting Started

Introduction to Java. Lecture 1 COP 3252 Summer May 16, 2017

The Java programming environment. The Java programming environment. Java: A tiny intro. Java features

CS Exam 1 Review Suggestions

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Lecture 4: Introduction to Java and your Development Environment

Introduction Basic elements of Java

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

Administrative Stuff. Inf1-OP. Additional help? Who to contact for help? Course Overview

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

CSC116: Introduction to Computing - Java

Java Swing Introduction

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

CSC116: Introduction to Computing - Java

G51OOP. Object Oriented Programming Comp Sci University of Nottingham Unit 1 : Introduction

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Heap Arrays and Linked Lists. Steven R. Bagley

CS 61C: Great Ideas in Computer Architecture Introduction to C

Lec 3. Compilers, Debugging, Hello World, and Variables

Lecture 2, September 4

Introduction to Java

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 1

CS11 Java. Fall Lecture 1

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

About this exam review

BM214E Object Oriented Programming Lecture 8

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Introduction to Java

CMSC 150 LECTURE 1 INTRODUCTION TO COURSE COMPUTER SCIENCE HELLO WORLD

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

Introduction to Java Programming

MODULE 02: BASIC COMPUTATION IN JAVA

Praktische Softwaretechnologie

DATA TYPES AND EXPRESSIONS

Who and what can help? Inf1-OP. Lecturer: Timothy Hospedales TA: Natalia Zon

CS 11 java track: lecture 1

Assumptions. History

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Programming Language Concepts: Lecture 1

Computer Components. Software{ User Programs. Operating System. Hardware

Lesson 04: Our First Java Program (W01D4

Index. Course Outline. Grading Policy. Lab Time Distribution. Important Instructions

Class 1: Homework. Intro to Computer Science CSCI-UA.0101 New York University Courant Institute of Mathematical Sciences Fall 2017

Chapter Two Bonus Lesson: JavaDoc

CE221 Programming in C++ Part 1 Introduction

Lecture 02, Fall 2018 Friday September 7

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

COMP6700/2140 JDK Tools

CS 177 Recitation. Week 1 Intro to Java

Getting started with Java

Introduction to Programming Using Java (98-388)

Programming with Java

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

CS 251 Intermediate Programming Java Basics

1.00/1.001 Tutorial 1

Exercise 1: Intro to Java & Eclipse

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

Transcription:

G52PGP Lecture oo3 Java (A real object oriented language) 1

Last lecture Associating functions with data into objects is an alternative way to decompose a program Can then consider each object on its own Adding member functions to a class (or struct) Act on a specific object Get an implicit object pointer called thisas a parameter (equivalent) Means that they can access private data Static member functions Do not get the implicit thispointer Can still access private data (if you provide the pointer) 2

Java This lecture There is no C or C++ in the exam or coursework There is some Java in both I covered C (and some C++) only to link Java to what you have done so far Hopefully this helps you to understand what these things in Java are really doing Some standard Java classes System: Class for things like text output JFrame: top level windows JLabel: GUI component labels within a window 3

Java s Genealogy

Java s Genealogy : C -> C++ ->Java It is not just a coincidence that Java is so similar to C And we can emulate the Java facilities in C Java was designed to be easy for C++ programmers to learn

Simple Text Hello World public class HelloWorld { public static void main(string[] args) { System.out.println( "Hello World!" ); } } What do you think the main() function does? Why is the main function static? What do you think the public modifier does? What do you think System.out.println() does? 6

Simple Text Hello World public class HelloWorld { public static void main(string[] args) { System.out.println( "Hello World!" ); } } Filename must match the class name E.g. HelloWorld.java i.e. only one top-level class per file Exception for inner classes (classes in classes see later) 7

Getting started with Eclipse Either download and install the JDK from the link below, making sure to get the correct version for your system: http://www.oracle.com/technetwork/java/javase/downl oads/index.html Or download the latest version of Eclipse IDE for Java Developers from the Eclipse homepage using the link below: http://www.eclipse.org/downloads/ Then Unzip Eclipse somewhere sensible Then just run eclipse Or use any other Java IDE you wish to use

Example: Create, build and execute Eclipse Create program Compile program Execute program Manually working with Java programs Manually compile program Manually execute program.class files Packages, package structure 9

JDK basic tools javac(compiler).java->.class The Java Language Compiler that you use to compile programs written in Java into bytecodes. Example : javac HelloWorld.java java (Interpreter) executes.class The Interpreter that you use to run programs written in Java. Example : java HelloWorld javadoc(doc. generator).java html Generates documentation in HTML format from Java source code. Example : javadoc HelloWorld.java

Eclipse Create a new project Do this for each coursework File > New > Java Project Use a meaningful name eg G51PGPCW1

Create a new class Create a new class Right click project name Choose Add Choose Java Class You could put it into a package (I didn t) Choose to create the main() function 13

Useful parts of the IDE Package explorer Java files Debug Run Source file Type your code in here Output will appear in here As will compilation results

Debugging views, at a breakpoint Right click in the left margin to set a breakpoint Resume Step into, step over Current code line Call stack Current variable values 15

Java key features (vs C/C++) Simplifies a lot of the things that cause bugs in C++ No pointers? uses object references instead These act like pointers but you can t use the address they hold Unfortunately pointer arithmetic sometimes used for speed in C++ No need to free memory runtime will deal with it Garbage collection is a common cause of program stall Platform independent Creates intermediate byte code rather than platform specific Executed through a virtual machine rather than native executable Some native compilers exist Just-in-time compilation is usually used to allow for optimisations Many tests indicate it s almost as fast as C/C++ Platform independent libraries for most things 16

Java vs C/C++ differences Tries to promote true object oriented programming Removed global functions (can use static members though) Library functions often take or return temporary objects Speed and efficiency is not the primary concern No malloc() The new operator is used instead C++ also uses new and delete, so not unusual It frees memory automatically wonderful feature (if annoyingly slow) No local variable objects Objects only on heap, not the stack Object references (i.e. pointers to objects) can be local variables though No access directly to memory locations Issues for device drivers or CUDA, no optimisation of data layout/caching Some syntactic differences between C++ and Java No use of * to dereference pointers Uses. rather than -> (has no objects on stack, so no other use for.) No ; at the end of class definitions 17

Some Java conventions Use capital first letter for class names E.g. String, Array, Book, Plane, Car, etc Use lower case first letter for variable name and method name E.g. i, counter, computer, input, etc When names have multiple words, capitalise the first letter of subsequent words E.g. mycomputer, largearray, etc Or class names: BookArray, SetOfCars, Braces: Original style guide says to add open brace to end of line, and put closing brace on a new line I usually (except for space reasons on slides) put the open brace on a new line, to aid in debugging (matching the braces) Whichever you do, please be consistent (even if I am not ) 18

Data Basic data types int, float, boolean, double, long, char, etc Objects Object references act like C/C++ struct pointers Need to use new(or a function which does so) to create the object If you didn t use new(or call a function which does so), you don t have an object Assigning one reference to another means make it refer to (point at) the same object Pass an object reference into a function, or return one, means referring to the same object, NOT creating a new one String Hybrid of objects and basic types No need to use new Passed around like objects by reference rather than making copies Support overloaded + operator, to concatenate Stringsto make a new String Think of these as a different or hybrid type of thing Look up the StringBufferobject if you want a bit more speed 19

Example of basic GUI Hello World import java.awt.flowlayout; import java.awt.font; import javax.swing.jframe; import javax.swing.jlabel; public class GUIHelloWorld {// Just the one class public static void main(string[] args) {// Same main function as before static JFrameguiFrame= new JFrame(); // Create a new frame object top level window guiframe.setdefaultcloseoperation(jframe.exit_on_close); // Close window exits guiframe.settitle("hello World!"); // Set a caption/title bar content for the frame guiframe.setlocationrelativeto(null); // centre on screen guiframe.setlayout( new FlowLayout() );// Layout : how to layout components JLabellabel= new JLabel(); // Create a new label object label.settext("hello World (again)!");// Set its message to show label.setfont(new Font("Ariel", Font.BOLD, 30));// Set a font for the label guiframe.add(label); // Add the label to the frame, so that it will show guiframe.pack(); // Resize frame to fit content guiframe.setvisible(true); // Display it until you do it will not appear } } 20

Next lecture We have seen some objects Grouping of data and some functions to act upon it We have seen some basic Java code There is a coursework/lab exercise to do, which will allow you to practise this Next lecture we will look at some more Java code, what we are really doing, and then at how to identify appropriateobjects in our own programs, and the basics of object oriented design 21