The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1



Similar documents
Java CPD (I) Frans Coenen Department of Computer Science

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

CSE 452: Programming Languages. Acknowledgements. Contents. Java and its Evolution

Chapter 1 Java Program Design and Development

An Overview of Java. overview-1

Java applets. SwIG Jing He

1. Overview of the Java Language

Contents. Java - An Introduction. Java Milestones. Java and its Evolution

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Java Interview Questions and Answers

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

LAB4 Making Classes and Objects

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Moving from CS 61A Scheme to CS 61B Java

C# and Other Languages

Introduction to Object-Oriented Programming

Chapter 1 Fundamentals of Java Programming

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

C Compiler Targeting the Java Virtual Machine

Java Programming Fundamentals

CS 209 Programming in Java #1

Crash Course in Java

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

History OOP languages Year Language 1967 Simula Smalltalk

Java (12 Weeks) Introduction to Java Programming Language

Characteristics of Java (Optional) Y. Daniel Liang Supplement for Introduction to Java Programming

CSC 551: Web Programming. Spring 2004

Fundamentals of Java Programming

Syllabus for CS 134 Java Programming

Appendix A Using the Java Compiler

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

INTRODUCTION TO JAVA PROGRAMMING LANGUAGE

Java Crash Course Part I

Lecture 1 Introduction to Android

Part I. Multiple Choice Questions (2 points each):

Installing Java (Windows) and Writing your First Program

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

Section 1.4. Java s Magic: Bytecode, Java Virtual Machine, JIT,

The Java Virtual Machine (JVM) Pat Morin COMP 3002

Introduction to Java Applets (Deitel chapter 3)

What Perl Programmers Should Know About Java

Habanero Extreme Scale Software Research Project

CS 106 Introduction to Computer Science I

ProfBuilder: A Package for Rapidly Building Java Execution Profilers Brian F. Cooper, Han B. Lee, and Benjamin G. Zorn

Introduction to programming

Java Classes. GEEN163 Introduction to Computer Programming

Lesson 06: Basics of Software Development (W02D2

9/11/15. What is Programming? CSCI 209: Software Development. Discussion: What Is Good Software? Characteristics of Good Software?

More on Objects and Classes

jmonitor: Java Runtime Event Specification and Monitoring Library

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Habanero Extreme Scale Software Research Project

Tutorial on Writing Modular Programs in Scala

Java Programming Language

Table of Contents. Java CGI HOWTO

ATSBA: Advanced Technologies Supporting Business Areas. Programming with Java. 1 Overview and Introduction

Object Instance Profiling

Programming Languages

Install Java Development Kit (JDK) 1.8

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

Report of the case study in Sistemi Distribuiti A simple Java RMI application

Sources: On the Web: Slides will be available on:

JAVA - INHERITANCE. extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.

Installing Java. Table of contents

AP Computer Science Java Subset

J a v a Quiz (Unit 3, Test 0 Practice)

The Java Virtual Machine and Mobile Devices. John Buford, Ph.D. Oct 2003 Presented to Gordon College CS 311

Teach Yourself Java in 21 Minutes

Chapter 1. Introduction to Computers, Programs, and Java

Java programming for C/C++ developers

Java History. Java History (cont'd)

Chapter 1 Introduction to Computers, Programs, and Java

CSC230 Getting Starting in C. Tyler Bletsch

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Object Oriented Design

McGraw-Hill The McGraw-Hill Companies, Inc.,

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

An Overview of Oracle Forms Server Architecture. An Oracle Technical White Paper April 2000

How To Use A Sas Server On A Java Computer Or A Java.Net Computer (Sas) On A Microsoft Microsoft Server (Sasa) On An Ipo (Sauge) Or A Microsas (Sask

Java Basics: Data Types, Variables, and Loops

Java Application Developer Certificate Program Competencies

Chapter 13 Computer Programs and Programming Languages. Discovering Computers Your Interactive Guide to the Digital World

Applets, RMI, JDBC Exam Review

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Object-Oriented Programming in Java

Progress Report Aspect Oriented Programming meets Design Patterns. Academic Programme MSc in Advanced Computer Science. Guillermo Antonio Toro Bayona

Here's the code for our first Applet which will display 'I love Java' as a message in a Web page

Fundamentals of Programming and Software Development Lesson Objectives

C++ INTERVIEW QUESTIONS

Chapter 13 - Inheritance

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

A Java-based system support for distributed applications on the Internet

Chapter 1. Dr. Chris Irwin Davis Phone: (972) Office: ECSS CS-4337 Organization of Programming Languages

Lab Experience 17. Programming Language Translation

Transcription:

The Java Series Java Essentials I What is Java? Basic Language Constructs Slide 1

What is Java? A general purpose Object Oriented programming language. Created by Sun Microsystems. It s a general purpose language in exactly the same way as C, C++, FORTRAN, Pascal, are general purpose programming language. As any general purpose language Java programs may be small applications, complex systems, etc Slide 2

Why Java? Cross-platform development is typically hard, implies: recompilations, adaptations, etc. This is very inconvenient in: Heterogeneous networked environments. Environments with a large variety of machines. Also, spread programming languages had things to improve: FORTRAN is not Object Oriented. C++ can become cumbersome to learn and debug. Developed by Sun initially as a language to be commonly used in machines, pcs, tvs, vcrs, etc.. Slide 3

The goals It had to be portable: Java programs should be able to run anywhere with no modifications. It had to be Object Oriented, as OO is the accepted paradigm for sustainable software development It had to learn from problems in other languages It had to help programmers in their tasks. It had to be network aware. Slide 4

The Task Sun designed the language from scratch borrowing much of C++ syntax. It s purely Object Oriented. Everything in Java must be defined in some class. Common functionality should be selfcontained: General Purpose libraries (data manipulations, graphics, network, UI, DB, etc.) are part of the language distribution. Programmers don t have to worry about internals of memory management. Slide 5

Means that Portability the same code should run anywhere, without the need to recompile, adapt, etc. when you develop you don t think about the platform your users will have. as a user, you don t need to worry about the platform used by developers. Typically when you make a program you either: Compile the source code to obtain a machine specific binary which can be executed right away. Distribute the source and an interpreter in the user machine executes it. Slide 6

Compilation vs Interpretation When compiling developers distribute binaries: A binary is machine specific. A binary is usually quite efficient because it s machine specific. The compilation process checks for syntactic errors, optimizes code, etc. When interpreting, developers distribute source code: Source code can be made machine independent. Source code is not as efficient as binaries since there is an interpreter which: parses the source code and then executes it. Slide 7

Portability Always means a compromise between compilation and interpretation. Java is neither compiled but interpreted but both A Java program is a set of.java source files. Java source files are compiled producing Java Binaries, called Byte Codes. Byte Codes are machine independent. Programmers distribute compiled byte codes to their users. To run applications, users MUST have a java bytecode interpreter for their machine. Byte-codes are therefore interpreted. Slide 8

Java Portability Portability is achieved at the byte codes. There is a compilation process at the developers and an interpretation process at the user. This way: Developers benefit from compilation (error checking, etc ) Byte codes are more compact and optimized than source code. Portability is not lost which performance is far better than with typically interpreted languages. However, performance is (still) far from pure compiled languages. Slide 9

JDK and JVM A Java Virtual Machine understands and executes bytecodes. In order to actually have portability there has to be a JVM for each different platform. Any piece of software containing a JVM is able to execute Java applications. Sun deploys Java by offering the Java Development Kit (jdk) The JDK contains: A Java compiler to produce byte-codes from java source files. A Java interpreter to execute byte-codes. Libraries providing common functionality (DB, network, UI,..) Debugging utilities. For instance, the programs: javac (the compiler) and java (the interpreter) contain a JVM. Slide 10

Java and the WWW Usually a user uses java from Sun s jdk to execute applications. But some WWW browsers have included a JVM in their application. So they can execute byte-codes. If byte-codes are available through a URL, they may be pointed to from within HTML This way a browser may: Download an HTML page. Obtain an URL to a Java byte-code from an <APPLET> HTML tag. URL-pointed byte-codes are called APPLETS. Download the byte-code. Lend a piece of window to the JVM. Tell their JVM to execute the downloaded byte-code. So the browser renders the HTML page and at the same time we have a java applet running. For this, portability is essential. Slide 11

Evolution of Java Java is maintained and developed by Sun. New versions of Java (improvements in the language, libraries, etc.) are made public through releases of the JDK. Sun usually releases the JDK on Solaris and Windows. Other platforms follow as vendors implement it. During Java evolution, JVM versioning has been quite an issue. The current release of Java is quite stable and can be thought of THE ONE. Slide 12

Remember OO Abstraction + Decomposition + Organization Steps: Define your classes Properties (variables) + behaviour (methods) Inheritance, Polymorphism, Encapsulation Create your instances from the classes definition. We are going to go through these steps. You ll see how to use Java to do this. Slide 13

A few things about Java There is a set of atomic Data Types int, boolean, etc. Apart from that everything is an Object There are operators (=, +, >=, etc.) Control flow statements (if, while, etc.) And a way to invoke operations on an object: int size = 10; mybox = new Box(size); mybox.open(); mybox.printstatus(); Slide 14

Sample Scenario 1 We are going to be using counters. Each counter has its own internal value (properties) We can ask a counter to: Increment its value To print out its value First we have to define a counter class Slide 15

Scenario 1: Counter class public class Counter { int value = 0; public int increment() { value ++; return value; public int getvalue() { return value; Properties: one variable Behaviour: two methods Slide 16

Scenario 1: MyApplication class public class MyApplication { public static void main (String args[]) { Main method is STATIC Specifies the program startup point Counter c1 = new Counter(); Counter c2 = new Counter(); We create two objects System.out.println("Counter 1 has value "+c1.getvalue()); System.out.println("Counter 2 has value "+c2.getvalue()); c1.increment(); c1.increment(); c2.increment(); Invoke methods on objects System.out.println("Counter 1 now has value "+c1.getvalue()); System.out.println("Counter 2 now has value "+c2.getvalue()); Slide 17 Invoke static class methods. System is always available

Scenario1: Compiling and Executing rsplus03> javac *.java rsplus03> java MyApplication Counter 1 has value 0 Counter 2 has value 0 Counter 1 now has value 2 Counter 2 now has value 1 rsplus03> java Counter The interpreter looks for static main method in MyApplication class In class Counter: void main(string argv[]) is not defined Slide 18

Sample Scenario 2 Now, we d also like to have a counter just as the one we have defined but each time the increment method is invoked we get the counter incremented by two. Slide 19

Scenario 2: BigCounter class Only redefines one method Inherits everything from Counter public class BigCounter extends Counter{ public int increment() { value = value + 2; return value; Slide 20

Sce 2:MyApplication class public class MyApplication { public static void main (String args[]) { Counter c1 = new BigCounter(); Counter c2 = new Counter(); This is the ONLY CHANGE in the code System.out.println("Counter 1 has value "+c1.getvalue()); System.out.println("Counter 2 has value "+c2.getvalue()); c1.increment(); c1.increment(); c2.increment(); System.out.println("Counter 1 now has value "+c1.getvalue()); System.out.println("Counter 2 now has value "+c2.getvalue()); Slide 21

Sce 2: BigCounter class (2) Calls twice the method defined in the Counter class. public class BigCounter extends Counter{ public int increment() { super.increment(); return super.increment(); The keyword super refers to the parent class. Slide 22

Sample Scenario 3 Now we want to optionally specify an starting value for counters when we create them. Slide 23

Sce 3: Counter Class public class Counter { public Counter (int startingvalue) { value = startingvalue; We create a constructor Constructors are NOT inherited. We must invoke the with super() public class BigCounter { public BigCounter (int value) { super(value); Slide 24 This constructor does whatever the parent s constructor does.

Sc 3: MyApplication Class public class MyApplication { public static void main (String args[]) { Here we use the constructor Counter c1 = new Counter(3); Counter c2 = new BigCounter(4); This is OO, we use indirectly the parent s constructor System.out.println("Counter 1 has value "+c1.getvalue()); System.out.println("Counter 2 has value "+c2.getvalue()); c1.increment(); c1.increment(); c2.increment(); System.out.println("Counter 1 now has value "+c1.getvalue()); System.out.println("Counter 2 now has value "+c2.getvalue()); Slide 25

Static Methods and Variables A static method or variable is only attached to the class definition and to no object instantiated from that class. Thus: They are accessed through the class name. All objects instantiated from that class see the same static variables. Static methods can be invoked directly from the class name. Slide 26

Sce 4: Sample class public class Sample { static int classvalue = 0; int objectvalue = 10; public static void printclassvalue() { System.out.println( The class value is +classvalue); public static void setclassvalue (int v) { classvalue = v; ; public void printobjectvalue() { System.out.println( This object s value is +objectvalue); public void setobjectvalue(int v) { objectvalue = v; Slide 27

Sce 4: MyApplication class public class MyApplication { public static void main (String args[]) { Sample.printClassValue(); // prints 0; Sample s1 = new Sample(); Sample s2 = new Sample(); s1.setclassvalue(20); s1.setobjectvalue(25); s1.printclassvalue(); // prints 20 s2.printclassvalue(); // prints also 20 s1.printobjectvalue(); // prints 25 s2.printobjectvalue(); // prints 10 Sample.setClassValue(15); s2.printclassvalue(); // prints 15 s1.printclassvalue(); // prints also 15 Slide 28

Scenario 5: Accumulator public class Acumulator { int sum = 0; public void add (int quantity) { sum = sum + quantity; public int getsum () { return sum; Slide 29

Scenario 5: MyApplication public class MyApplication { public static void main (String args[]) { Accumulator a1 = new Accumulator(); System.out.println( Accumulator 1 has value "+a1.getsum()); a1.add(5); System.out.println( Accumulator 1 has value "+a1.getsum()); a1.add(10); System.out.println( Accumulator 1 has value "+a1.getsum()); Slide 30

Scenario 5 But now we d like Counters and Accumulators to do something similar such as printing their value themselves. We d like something like this for all objects: Object.printStatus(); But preserving the structure of classes we have defined. Slide 31

Interfaces An interface is just a set of requirements we may ask a class definition to fulfill. It is a list of methods and variables a class definition must implement. In our case, we want every Counter and Accumulator to have a printstatus method. Slide 32

Sce 5: StatusPrinter interface We use the interface keyword public interface StatusPrinter { public void printstatus(); This is just a list of methods with no implementation. This interface only includes one method. We could also define variables, use the public, protected and private attributes, specify lists of arguments, etc.. Slide 33

Sce 5: Accumulator class public class Acumulator implements StatusPrinter{ int sum = 0; public void add (int quantity) { sum = sum + quantity; public int getsum () { return sum; public void printstatus () { System.out.println ( I m an accumulator \\ My sum is + sum); An we MUST provide an implementation for this method. Otherwise the compilation will fail. Slide 34 We declare the class to follow the requirements specified in the StatusPrinter interface.

Sce 5: Counter class public class Counter implements StatusPrinter{...... public void printstatus() { System.out.println ( I m a Counter. \\ My value is +value); public int getvalue() { return value; Slide 35

Sce 5: MyApplication class public class MyApplication { static void displaystuff (StatusPrinter s) { s.printstatus(); A method which takes just a status printer. No matter what the actual class of s is. public static void main (String args[]) { Accumulator a1 = new Accumulator(); Counter c1 = new Counter(); MyApplication.displayStuff (a1); MyApplication.displayStuff (c1); With interfaces we can make objects look the same in certain situations although they may belong to unrelated classes. Interfaces are independent from the class hierarchy we build up. Also we can overcome some advantages of not having multiple inheritance Slide 36

SUMMARY We have: Seen what Java is. Defined classes. Instantiated objects from the classes defined. Extended a class (creating an inheritance relation). Redefined methods of a parent class. Used object constructors. Played around with static methods and variables. Accessed objects with interfaces Slide 37

SUMMARY These are the building blocks with which to structure your code. Building an OO application is to decide how to distribute and define access to your code by using classes, interfaces, exceptions, etc. With AWT we will use the already made hierarchy of classes to manipulate graphical components. It s a good example to grasp how the OO conceptualization helps structuring the code. Slide 38