Chapter 1. Understanding Java

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

L1:CSC © Dr. Basheer M. Nasef Lecture #1 By Dr. Basheer M. Nasef.
Introduction To Java Objectives For Today â Introduction To Java â The Java Platform & The (JVM) Java Virtual Machine â Core Java (API) Application Programming.
Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
Lab#1 (14/3/1431h) Introduction To java programming cs425
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to.
Object Orientated Programming
IC211 Object Oriented Programming Overview of Java.
Introduction to Java Programming, 4E
For more Lectures and Notes Visit
Session-02. Objective In this session you will learn : What is Class Loader ? What is Byte Code Verifier? JIT & JAVA API Features of Java Java Environment.
L EC. 01: J AVA FUNDAMENTALS Fall Java Programming.
01 Introduction to Java Technology. 2 Contents History of Java What is Java? Java Platforms Java Virtual Machine (JVM) Java Development Kit (JDK) Benefits.
JAVA v.s. C++ Programming Language Comparison By LI LU SAMMY CHU By LI LU SAMMY CHU.
CSCI 224 Introduction to Java Programming. Course Objectives  Learn the Java programming language: Syntax, Idioms Patterns, Styles  Become comfortable.
M1G Introduction to Programming 2 4. Enhancing a class:Room.
CIS 068 JAVA vs. C++ Main Differences. CIS 068 JAVA vs C++ Java is (an interpreted) write once, run anywhere language. –The biggest potential stumbling.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Programming Languages Machine.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Computers,
CPCS  Problem Solving in Everyday Life  Types of Problems  Problem Solving with Computers  Difficulties with Problem Solving 0-3.
Java Introduction Lecture 1. Java Powerful, object-oriented language Free SDK and many resources at
1.  At the end of this slide, student can:  Explore tools, features, properties and interface of the Textpad.  Creating a new project.  Open and run.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
University of Houston-Clear Lake Proprietary© 1997 Evolution of Programming Languages Basic cycle of improvement –Experience software difficulties –Theory.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Programs.
Introduction to Java Programming with Forte Y. Daniel Liang.
Introduction to Object Oriented Programming CMSC 331.
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to.
Classes, Interfaces and Packages
Lesson 1 1 LESSON 1 l Background information l Introduction to Java Introduction and a Taste of Java.
1 Introduction to Java. 2 * 10% Assignments/ class participation * 10% Pop Quizzes * 05% Attendance * 25% Mid Term * 50% Final Term.
ITP 109 Week 2 Trina Gregory Introduction to Java.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 1 Introduction.
Introduction to JAVA Programming
Introduction to Java Programming, 4E Y. Daniel Liang.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Fundamental of Java Programming (630002) Unit – 1 Introduction to Java.
CIS 068 JAVA vs. C++ Main Differences CIS 068.
Object Oriented Programming in
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
1. Introduction To JAVA.
Chapter 1 Introduction to Computers, Programs, and Java
Chapter No. : 1 Introduction to Java.
Introduction to Advanced Java Programming
Internet and Java Foundations, Programming and Practice
Java programming lecture one
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Introduction Enosis Learning.
Introduction Enosis Learning.
Unit 41 – Programing in Java
Java Programming Language
Chapter 1 Introduction to Computers, Programs, and Java
Object Oriented Programming
More Object-Oriented Programming
Units with – James tedder
From C++ to Java Java history: Oak, toaster-ovens, internet language, panacea What it is O-O language, not a hybrid (cf. C++) compiled to byte-code, executed.
(Computer fundamental Lab)
Java History, Editions, Version Features
Introducing Java.
Outcome of the Lecture Upon completion of this lecture you will be able to understand Fundamentals and Characteristics of Java Language Basic Terminology.
Presentation transcript:

Chapter 1. Understanding Java Introduction to Java Object-Oriented Programming Java for C++ Programmers

1. Introduction to Java Why do people use Java? Java Platform Compiling and running Characteristics of Java Java Version Java Edition

1.1 Why do people use Java? 기존 프로그램 개발 환경 컴파일러(윈도우 용) 윈도우 용 실행파일 소스 코드 컴파일러(윈도우 용) 컴파일러(유닉스 용) 컴파일러(리눅스 용) 윈도우 용 실행파일 유닉스 용 실행파일 리눅스 용 실행파일 기존 프로그램 개발 환경

Why do people use Java? [자바 개발 환경] WORA(Write Once, Run Anywhere) JVM(윈도우 용) JVM( 유닉스 용) JVM( 리눅스 용) 소스 코드 컴파일러(윈도우 용) 컴파일러(유닉스 용) 컴파일러(리눅스 용) 바이트 코드 [자바 개발 환경] WORA(Write Once, Run Anywhere)

The Java Approach Compilation into ByteCode Java Source Interpretation by the Java Virtual Machine Any Computer Javac Compiler Byte Code Java Virtual Machine

Why do people use Java? Platform Independence Productivity JVM과 Java API가 구현된 곳은 어디서나 수행가능 Productivity Garbage Collection C++라이브러리보다 풍부한 자바 표준 API Reuse of Component-based code Java Beans Component로 가능하게 됨

1.2 Java Platform 자바 애플릿. 자바 애플리케이션 자바 플랫폼 자바 API 자바 VM (네이티브 시스템)OS 하드웨어 자바 플랫폼 자바 API 자바 VM 자바 애플릿. 자바 애플리케이션

Java Platform Component

JRE and JDK Two software products in the Java SE Java SE Runtime Environment (JRE) provides the libraries, Java virtual machine, and other components necessary to run applets and applications Java SE Development Kit (JDK) includes the JRE plus command-line development tools such as compilers and debuggers necessary for developing applets and applications.

1.3 Compiling and Running HelloWorld.java HelloWorld.class javac HelloWorld.java HelloWorld.java compile run source code java HelloWorld HelloWorld.class bytecode

Compiling and Running bytecode is an intermediate representation of the program (class). The Java interpreter starts up a new “Virtual Machine”. The VM starts executing the users class by running it’s main() method.

Compiling and Running Simple Application: Hello World public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); }

1.4 Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture-Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic Java is partially modeled on C++, but greatly simplified and improved. Some people refer to Java as "C++--" because it is like C++ but with more functionality and fewer negative aspects.

Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture-Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic Java is inherently object-oriented. Although many object-oriented languages began strictly as procedural languages, Java was designed from the start to be object-oriented. Object-oriented programming (OOP) is a popular programming approach that is replacing traditional procedural programming techniques. One of the central issues in software development is how to reuse code. Object-oriented programming provides great flexibility, modularity, clarity, and reusability through encapsulation, inheritance, and polymorphism.

Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture-Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic Distributed computing involves several computers working together on a network. Java is designed to make distributed computing easy. Since networking capability is inherently integrated into Java, writing network programs is like sending and receiving data to and from a file.

Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture-Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic You need an interpreter to run Java programs. The programs are compiled into the Java Virtual Machine code called bytecode. The bytecode is machine-independent and can run on any machine that has a Java interpreter, which is part of the Java Virtual Machine (JVM).

Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture-Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic Java compilers can detect many problems that would first show up at execution time in other languages. Java has eliminated certain types of error-prone programming constructs found in other languages. Java has a runtime exception-handling feature to provide programming support for robustness.

Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture-Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic Java implements several security mechanisms to protect your system against harm caused by stray programs.

Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture-Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic Write once, run anywhere With a Java Virtual Machine (JVM), you can write one program that will run on any platform.

Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture-Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic Because Java is architecture neutral, Java programs are portable. They can be run on any platform without being recompiled.

Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture-Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic Java’s performance Because Java is architecture neutral, Java programs are portable. They can be run on any platform without being recompiled.

Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture-Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic Multithread programming is smoothly integrated in Java, whereas in other languages you have to call procedures specific to the operating system to enable multithreading.

Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture-Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic Java was designed to adapt to an evolving environment. New code can be loaded on the fly without recompilation. There is no need for developers to create, and for users to install, major new software versions. New features can be incorporated transparently as needed.

1.5 Java Version JDK 1.02 (1995) JDK 1.1 (1996) JDK 1.2 (1998) JDK 1.5 (2004) a. k. a. JDK 5 or Java 5 JDK 1.6 (2006) a. k. a. JDK 6 or Java 6

1.6 Java Edition Java Standard Edition (J2SE) J2SE can be used to develop client-side standalone applications or applets. Java Enterprise Edition (J2EE) J2EE can be used to develop server-side applications such as Java servlets and Java ServerPages. Java Micro Edition (J2ME). J2ME can be used to develop applications for mobile devices such as cell phones. We will use J2SE to introduce Java programming.

1.7 Basic requirements for developing java program The application will developed using Java SE 6 SDK. You can download it from http://java.sun.com/javase/downloads/index.jsp. We will be using Eclipse development environment. Download it from http://www.eclipse.org and install it. The description of all classes in Java SE 6 SDK can be found at: http://java.sun.com/javase/6/docs/api/

Downloading Java SE http://java.sun.com/javase/downloads/index.jsp

2. Object-Oriented Programming Understanding OOP is fundamental to writing good Java applications Improves design of your code Improves understanding of the Java APIs There are several concepts underlying OOP: Abstract Types (Classes) Encapsulation (or Information Hiding) Inheritance Polymorphism There are two ways to approach Java. Either as just another language, I.e. begin with the basic syntax and show how procedural programs can be created, and then teach OO concepts later on. Or you begin with the OO concepts and introduce the syntax along the way. I prefer the latter approach, which this course will take, because understanding OOP is critical to working well with Java. You can only get the most from the language by understanding these features. OOP has several key concepts which we’ll review now, but will investigate in more detail as we learn more about the Java language. At this point we’re just introducing some of the basic terminology.

What is OOP? How is OOP different to conventional programming? Decompose the application into abstract data types by identifying some useful entities/abstractions Abstract Data Type (aka Class) made up of a series of behaviours and the data that those behaviours use. template 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. 30mph, Red, etc) Note that this is reminiscent of database modelling: we identify the entities that we want to model and then define their attributes. Here we go a step further and identify the behaviours that those entities should have so we can use that data.

Note that there’s no relationship between data and functions Note that there’s no relationship between data and functions. Data may be reused in several places, so each function has to have knowledge of that data, e.g. what types it contains, where its stored, etc, etc. In an OO model each object maintains its own data. The functions and data are “fused” into an abstract data type. Note that this is more manageable as I can change the structure of any piece of data, e.g. integer to floating point, add/remove new pieces of information without impacting any of the rest of the application. It’s what helps make OO systems more reusable.

Encapsulation 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(Cont’d) 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

Inheritance 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 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 Inheritance is therefore another form of code reuse Define one type in terms of another type. Both motorbikes and cars are types of vehicles.

Polymorphism Means ‘many forms’ 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 ‘turnLeft’ message, however the means of responding to that message (turning wheels, or banking wings) is very different for each.

Summary! In OO programming we Benefits of OO programming Define classes Create objects from them Combine those objects together to create an application 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)

Java for C++ programmers

Comparing C++ and Java(1/10) Java with Interpreter is slower then compiled C++ Everything must be in a class. Only classes. No global methods or data Instead, one can use Static Methods / Static Member within a class No structs, enumerators, or unions. All method definitions are defined in the body of the class. Class aType { void aMethod() { /* method body */ } } No class declaration, only class definition. No scope resolution operator :: Use the dot(.) for everything

Comparing C++ and Java(2/10) To perform the equivalent of #include in C++, use the import keyword in Java Example: import java.awt.*; Standard Primitive Data Types boolean, char, byte, short, int, long, float, double. char is 16-bit Unicode Type checking is much tighter in Java. Static quoted strings are automatically converted into String object. Arrays are quite different in java. Treated as objects. Example: int [ ] score = new int [5]; All arrays are created on the heap.

Comparing C++ and Java(3/10) All objects of non-primitive types can be created only via new. Example: Triangle trg = new Triangle(3,4,5) ; Created on the heap. Note that primitive types are created on stack, without new. No forward declarations are needed. Java has no preprocessor. If you want to use classes in another library, you say import and the name of the library. Java uses packages in place of Namespaces. Default Initialization. Object Handles initialized to null. Primitive class initialized 0 or equivalent.

Comparing C++ and Java(4/10) No pointers in the sense of C++. When you create an object with new, you get back a reference (or handle). Example String s = new String(“how”); Constructors are similar to C++. You get a default constructor if you don’t define one. If you define a non-default constructor, there’s no automatic default constructor defined for you. No destructors in Java. The lifetime of an object is determine by the GC finalize() method (like a destructor) is called during Garbage Collection. Method overloading Works virtually identically to C++ function overloading.

Comparing C++ and Java(5/10) Does not support default arguments. No ‘goto’ in java There is ‘break label’ and ‘continue label’ Singly-rooted class hierarchy, everything derives from “Object”. With GC, memory leaks are much harder to cause. Java has built-in Multi-Threading support Mutual exclusion at object level.

Comparing C++ and Java(6/10) Access Specifiers for each member (public, private, protected) Without an explicit access specifier, the element defaults to “friendly”, which means it is accessible to other elements in the same package but inaccessible outside the package. Everything in a Package is Friendly No inline keyword. Compiler optimization can do it. Inheritance in java has the same effect as in C++, but the syntax is different “extends” keyword to indicate inheritance “super” keyword to specify methods in the base class

Comparing C++ and Java(7/10) Inheritance doesn’t change protection level of the members in the base class . Overridden methods in a derived class cannot reduce the access of method in the base class. For example, if a method is public in the base class and you override it, your overridden method must also be public (the compiler ensures this). “interface” keyword for abstract base class. “implements” keyword for implementing it. No “virtual” keyword, since all non-static methods are dynamically binded. “final” keyword provides some latitude for efficiency tuning- it tells the compiler that this method may not be overridden, and thus that it can be statically bound.

Comparing C++ and Java(8/10) No Multiple Class Inheritance. But, many interfaces can be implemented. All Downcast with run-time check. So there is no bad casts like in C++. derived d = (derived) base ; Exception Specifications are superior to C++. No operator overloading. No const. For compile-time constant value, use “static final”. call by reference for objects.

Comparing C++ and Java(9/10) Built-in support for comment documentation. The source code file can also contain its own doc, which is reformatted to HTML. Standard libraries for certain tasks. C++ relies on non-standard third-party libraries. Example Networking, Database connection, Multithreading, Distributed Objects (RMI and CORBA), Compression, Commerce

Comparing C++ and Java(10/10) Java is more robust Handles are always checked and exceptions are thrown for failures All array accesses are checked for bounds violations Automatic garbage collection prevents memory leaks Clean, relatively fool-proof exception handling Simple language support for multi-threading Bytecode verification of network applets