1 Introduction To Java Professor Yihjia Tsai Tamkang University.

Slides:



Advertisements
Similar presentations
Basic Java Constructs and Data Types – Nuts and Bolts
Advertisements

Written by: Dr. JJ Shepherd
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Crash Course in Java Based on notes from D. Hollinger Based in part on notes from J.J. Johns also: Java in a Nutshell Java Network Programming and Distributed.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Programming in Java; Instructor:Alok Mehta Objects, Classes, Program Constructs1 Programming in Java Objects, Classes, Program Constructs.
Netprog 2002 Java Intro1 Crash Course in Java Based on notes from D. Hollinger Based in part on notes from J.J. Johns also: Java in a Nutshell Java Network.
March 2005Java Programming1. March 2005Java Programming2 Why Java? Platform independence Object Oriented design Run-time checks (fewer bugs) Exception.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
CSC3170 Introduction to Database Systems
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java and C++, The Difference An introduction Unit - 00.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
The Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Introduction to java Prepared By:-Pragnesh Patel Lect. In Computer Dept. NSIT,Jetalpur 1.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
1 Introduction to Java. 2 What is Java? A programming language. A platform –A virtual machine (JVM) definition. –Runtime environments in diverse hardware.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
CS 11 java track: lecture 2 This week: more on object-oriented programming (OOP) objects vs. primitive types creating new objects with new calling methods.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
Java for C++ Programmers A Brief Tutorial. Overview Classes and Objects Simple Program Constructors Arrays Strings Inheritance and Interfaces Exceptions.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Features of JAVA PLATFORM INDEPENDENT LANGUAGE JAVA RUNTIME ENVIRONMENT (JRE) JAVA VIRTUAL MACHINE (JVM) JAVA APP BYTE CODE JAVA RUNTIME ENVIRONMENT.
Spring 2006 Special Topics in Computer Engineering: Java Intro 1 Crash Course in Java Based on notes from D. Hollinger Based in part on notes from J.J.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
JAVA MULTIPLE CHOICE QUESTION.
Objectives You should be able to describe: Interactive Keyboard Input
Internet and Java Foundations, Programming and Practice
University of Central Florida COP 3330 Object Oriented Programming
Objects, Classes, Program Constructs
Java Programming Language
Introduction to Java Programming
An overview of Java, Data types and variables
The Building Blocks Classes: Java class library, over 1,800 classes:
Inheritance Inheritance is a fundamental Object Oriented concept
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Presentation transcript:

1 Introduction To Java Professor Yihjia Tsai Tamkang University

2 Basic OOP Notions Java  Instance variables - describe the data in abstract data types (each object instance has its own copy of the data)  Class variables - describe the data in abstract data types all object instances share the value of the data (called static fields in Java)  Messages request for an operation  Methods implementation of a request

3 Data and Methods/Interface DATA Oper 1 Oper 3 Oper 4 Oper 2 Public Private

4 Introduction to Java Basic Ideas  All Java Programs, unlike C++ are built from classes, in this way Java is more strictly an OOP  A class is said to contain fields (members in C++) and methods (member functions in C++)

5 Fundamental Ideas  Java was designed with security and portability in mind  Supports standard length built in types  Java source gets translated into something called Java byte code that is run on something called the Java virtual machine.  Java bytecode is the machine language of the Java virtual machine  The virtual machine assigns each application its own runtime, which isolates applications from each other.

6 Java Infrastructure javac.java java bytecode.class file produced This is the JVM

7 A Code Example class HelloWorld { public static void main(String[] args) { System.out.println( “ Hello mom ” ); } Type this in, compile it, and add Java to your resume ;)

8 Similarities and differences from C++?  The Built In Types  Comments  Named Constants  Flow of Control

9 The Built In Types booleantrue or false char16 bit Unicode 1.1 character byte8 bits short16 bit integer signed int32 bit signed integer long64 bit signed integer float, doubleIEEE 754

10 Comments  Are identical to C++ except for javadoc  // Comment till end of line  /* Comments spanning a line */  /** */ javadoc comment  javadoc produces html documentation on methods, constructors and so on

11 Named Constants class CircleStuff { static final double  = ; } class Suit { public final static int CLUBS = 1; public final static int DIAMONDS = 2; public final static int HEARTS = 3; public final static int SPADES = 4; }

12 Classes and Objects  Java classes have fields and methods  Fields and Methods in Java can have associated visibility  Public  Private  Package  Protected

13 Methods class Point { //.. public void clear() { x = 0; y = 0; } public double distance(Point other) { double xdiff, ydiff; xdiff = x - other.x; ydiff = y - other.y; return Math.sqrt(xdiff * xdiff + ydiff * ydiff); }

14 Packages  Used to avoid name conflicts  Java has adopted a more formal notion of a package that has a set of types and subpackages as members.  Packages are named and can be imported - we'll look at a few examples.  Package names form a hierarchy with parts separated by dots.  When you use part of a package, either you use its fully qualified name or you import all or part of the package.

15 Examples class Date1 { public static void main(String[] args) { java.util.Date now = new java.util.Date(); System.out.println(now); } import java.util.Date; class Date2 { public static void main(String[] args) { Date now = new Date(); System.out.println(now); }

16 Self Reference  Java unlike C++ has no notion of a pointer  There is still a this construct however - an example will suffice class Point { //... public void clear () { this.x = 0; this.y = 0; } //... }

17 Creating Objects  Use new  Invokes constructor  No destructor  Super and this

18 Arrays class Deck { final int DECK_SIZE = 52; Card[ ] = new Card[DECK_SIZE]; //.... Init the array somewhere public void print() { for (int i = 0; i < cards.length; i++) System.out.println(cards[i]); } //... }

19 Strings are Built In Java class Yadayadayada { public static void main (String[ ] args ) { /// yadayadayada } // } There is also a StringBuffer class

20 Tokenizing Strings import java.util.*; String stg = “when the going gets weird the weird turn pro”; StringTokenizer tokens = new StringTokenizer (stg); while (tokens.hasMoreElements() ) { String next = tokens.nextToken(); System.out.println(next); }

21 Inheritance  One of major aspects of OOP is inheritance - lets look at an example in Java  You can inherit, override, or reuse behavior of parent class  Note that Pixel objects can be used by any code designed to work with Point objects.  If a method expects a parameter of type Point, you can hand it a Pixel object instance and it still works just fine.

22 Inheritance class Pixel extends Point { Color color; public void clear() { super.clear(); color = null; } Only public inheritance !

23 Java Doesn't support MI of Implementation interface Lookup { // Return the value associated w/ name or null Object find (String name); } class Foo implements Lookup extends Bar { // Foo must provide all the Lookup methods }

24 Using an Interface  Now lets look at some code that that uses the Lookup interface void processValues (String[] names, Lookup table) { for (int i = 0; i<names.length; i++) { Object value = table.find(names[i]); if (value != null) processValue(names[i], value); }

25 A Class that Implements the Lookup Interface  Now lets look at a class that implements the interface class SimpleLookup implements Lookup { private String[] Names; private Object[] Values; public Object find(String name) { for (int i = 0; i<Names.length; i++) { if (Names[i].equals(name)) return Values[i]; } } //.... }

26 Exceptions  Java uses checked exceptions to manage error handling. Exceptions force a programmer top deal with errors. If a checked exception is not handled, this noticed when the error happens, not latter when problems have potentially compounded.  A method that detects an unusual error condition throws an exception. Exceptions in turn may be caught by code further back on the calling stack - this prior code can handle the exception and continue processing.  Un-handled exceptions are handled by a default handler in the Java implementation which may report the exception and terminate the thread of execution.

27 More General Ideas About Exceptions in Java  Exceptions in Java are objects, with type, methods and fields of data. This representation is handy because an exception can include data or methods to report or recover from certain conditions.  Exceptions are generally extensions of the Exception class which provides a string field to describe the error.

28 class IllegalAverageException extends Exception {}; class MyUtilities { public double averageOf ( double[] vals, int i, int j) throws IllegalAverageException{ try { return (vals[i] + vals[j]) / 2; } catch (IndexOutOfBoundsException e) { throw new IllegalAverageException();} } An Example of Exceptions

29 Text Input  System.in is a java.io.InputStream reads raw bytes BufferedReader in = new BufferedReader( new InputStreamReader(System.in));  InputStreamReader coverts from bytes to Unicode  BufferedReader supports String readLine() returns null at EOF

30 File Text I/O PrintWriter out = new PrintWriter(new FileWriter(fileName)); BufferedReader in = new BufferedReader( new FileReader(fileName));

31 References  Flanagan, D., Java in a Nutshell, 2nd ed., O ’ Reilly, ISBN X.  Harold, E.R., Java Network Programming, 3rd ed., O ’ Reilly, ISBN  Niemeyer, P., J. Knudsen, Learning Java, 3rd ed., O ’ Reilly, ISBN  Meyer, J., T. Downing, Java Virtual Machine, O ’ Reilly, ISBN