Download presentation
Presentation is loading. Please wait.
1
Chapter 1. Understanding Java
Introduction to Java Object-Oriented Programming Java for C++ Programmers
2
1. Introduction to Java Why do people use Java? Java Platform
Compiling and running Characteristics of Java Java Version Java Edition
3
1.1 Why do people use Java? 기존 프로그램 개발 환경 컴파일러(윈도우 용) 윈도우 용 실행파일
소스 코드 컴파일러(윈도우 용) 컴파일러(유닉스 용) 컴파일러(리눅스 용) 윈도우 용 실행파일 유닉스 용 실행파일 리눅스 용 실행파일 기존 프로그램 개발 환경
4
Why do people use Java? [자바 개발 환경] WORA(Write Once, Run Anywhere)
JVM(윈도우 용) JVM( 유닉스 용) JVM( 리눅스 용) 소스 코드 컴파일러(윈도우 용) 컴파일러(유닉스 용) 컴파일러(리눅스 용) 바이트 코드 [자바 개발 환경] WORA(Write Once, Run Anywhere)
5
The Java Approach Compilation into ByteCode Java Source Interpretation
by the Java Virtual Machine Any Computer Javac Compiler Byte Code Java Virtual Machine
6
Why do people use Java? Platform Independence Productivity
JVM과 Java API가 구현된 곳은 어디서나 수행가능 Productivity Garbage Collection C++라이브러리보다 풍부한 자바 표준 API Reuse of Component-based code Java Beans Component로 가능하게 됨
7
1.2 Java Platform 자바 애플릿. 자바 애플리케이션 자바 플랫폼 자바 API 자바 VM (네이티브 시스템)OS
하드웨어 자바 플랫폼 자바 API 자바 VM 자바 애플릿. 자바 애플리케이션
8
Java Platform Component
9
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.
10
1.3 Compiling and Running HelloWorld.java HelloWorld.class
javac HelloWorld.java HelloWorld.java compile run source code java HelloWorld HelloWorld.class bytecode
11
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.
12
Compiling and Running Simple Application: Hello World
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); }
13
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.
14
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.
15
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.
16
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).
17
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.
18
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.
19
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.
20
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.
21
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.
22
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.
23
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.
24
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
25
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.
26
1.7 Basic requirements for developing java program
The application will developed using Java SE 6 SDK. You can download it from We will be using Eclipse development environment. Download it from and install it. The description of all classes in Java SE 6 SDK can be found at:
27
Downloading Java SE
28
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.
29
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.
30
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.
31
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
32
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
33
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.
34
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.
35
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)
36
Java for C++ programmers
37
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
38
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.
39
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.
40
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.
41
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.
42
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
43
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.
44
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.
45
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
46
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.