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

Similar documents
Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Introduction to Programming Using Java (98-388)

Index COPYRIGHTED MATERIAL

Software Development With Java CSCI

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

B.V. Patel Institute of BMC & IT, UTU 2014

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

2 rd class Department of Programming. OOP with Java Programming

Objectives. Problem Solving. Introduction. An overview of object-oriented concepts. Programming and programming languages An introduction to Java

Programming. Syntax and Semantics

Introduction to. Android Saturday. Yanqiao ZHU Google Camp School of Software Engineering, Tongji University. In courtesy of The Java Tutorials

Course information. Petr Hnětynka 2/2 Zk/Z

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

Java Primer 1: Types, Classes and Operators

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Assumptions. History

Chapter 1 Introduction to Java

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

THE CONCEPT OF OBJECT

Chapter 1 Introduction to Computers, Programs, and Java

Course Outline. Introduction to java

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

Introduction to Programming (Java) 2/12

CS260 Intro to Java & Android 03.Java Language Basics

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

Chapter 4 Defining Classes I

Course information. Petr Hnětynka 2/2 Zk/Z

Advanced Object-Oriented Programming Introduction to OOP and Java

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

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

Full file at

WA1278 Introduction to Java Using Eclipse

Use the static method range of class EnumSet (declared in package java.util) to access a range of an enum s constants.

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

History Introduction to Java Characteristics of Java Data types

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Java Programming Tutorial 1

Chapter 6 Introduction to Defining Classes

Application Development in JAVA. Data Types, Variable, Comments & Operators. Part I: Core Java (J2SE) Getting Started

CS 231 Data Structures and Algorithms, Fall 2016

CS260 Intro to Java & Android 02.Java Technology

Cpt S 122 Data Structures. Introduction to C++ Part II

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

OOSD. Introduction to JAVA. Giuseppe Lipari Scuola Superiore Sant Anna Pisa. September 12, 2011

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

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

Java TM Introduction. Renaud Florquin Isabelle Leclercq. FloConsult SPRL.

Chapter. Focus of the Course. Object-Oriented Software Development. program design, implementation, and testing

Introduction to Java Programming CPIT 202. WEWwwbvxnvbxmnhsgfkdjfcn

OOSD. Introduction to JAVA. Giuseppe Lipari Scuola Superiore Sant Anna Pisa. September 29, 2010

Lecture 1: Overview of Java

3. Java - Language Constructs I

Java Programming. Atul Prakash

Language Fundamentals Summary

History of Java. Java was originally developed by Sun Microsystems star:ng in This language was ini:ally called Oak Renamed Java in 1995

PROGRAMMING FUNDAMENTALS

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

Key Differences Between Python and Java

Lexical Considerations

Chapter 3: Operators, Expressions and Type Conversion

15CS45 : OBJECT ORIENTED CONCEPTS

Question. 1 Features of Java.

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.


Java Overview An introduction to the Java Programming Language

Introduction to Java. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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

CSC 1214: Object-Oriented Programming

1Z Java SE 5 and 6, Certified Associate Exam Summary Syllabus Questions

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Short Notes of CS201

Pace University. Fundamental Concepts of CS121 1

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Programming Language Concepts: Lecture 2

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

A First Look At Java. Didactic Module 13 Programming Languages - EEL670 1

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

1 Shyam sir JAVA Notes

B2.52-R3: INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING THROUGH JAVA

CS201 - Introduction to Programming Glossary By

A variable is a name that represents a value. For

A class can have references to objects of other classes as members Sometimes referred to as a has-a relationship. Software Engineering Observation 8.

CMSC 132: Object-Oriented Programming II

Games Course, summer Introduction to Java. Frédéric Haziza

Programming II (CS300)

Operators. Java operators are classified into three categories:

Review of the C Programming Language for Principles of Operating Systems

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

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

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

Lexical Considerations

Introduction to Java

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

Transcription:

Introduction to Visual Basic and Visual C++ Introduction to Java Lesson 13 Overview I154-1-A A @ Peter Lo 2010 1 I154-1-A A @ Peter Lo 2010 2 Overview JDK Editions Before you can write and run the simple Java program in this lesson, you need to install the Java platform on your computer system. 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. I154-1-A A @ Peter Lo 2010 3 I154-1-A A @ Peter Lo 2010 4

Overview of Software Development Process In the Java programming language, all source code is first written in plain text files ending with the.java extension. Those source files are then compiled into.class files by the javac compiler. A.class file does not contain code that is native to your processor; it instead contains bytecodes the machine language of the Java Virtual Machine1 (JVM). The java launcher tool then runs your application with an instance of the Java Virtual Machine. Java Virtual Machine (JVM) Through the JVM, the same application is capable of running on multiple platforms I154-1-A A @ Peter Lo 2010 5 I154-1-A A @ Peter Lo 2010 6 Java Platform The Java platform has two components: The Java Virtual Machine (JVM) The Java Application Programming Interface (API) The API and Java Virtual Machine insulate the program from the underlying hardware My First Program Hello World I154-1-A A @ Peter Lo 2010 7 I154-1-A A @ Peter Lo 2010 8

Java Program Structure In the Java programming language: A program is made up of one or more classes A class contains one or more methods A method contains program statements Program statements can reference local or instance variables. There is no concept of global variable. These terms will be explored in detail throughout the course A Java application always contains a method called main Java Program Structure Class body // comments about the class public class Hello World { } // comments about the method Class header public static void main (String[] args) { Method Method header body } I154-1-A A @ Peter Lo 2010 9 I154-1-A A @ Peter Lo 2010 10 Comments Comments in a program are called inline documentation They should be included to explain the purpose of the program and describe processing steps They do not affect how a program works Java comments can take three forms: // this comment runs to the end of the line /* this comment runs to the terminating symbol, even across line breaks */ /** this is a javadoc comment */ Identifiers Identifiers are the words a programmer uses in a program An identifier can be made up of letters, digits, the underscore character ( _ ), and the dollar sign Identifiers cannot begin with a digit Java is case sensitive - Total, total, and TOTAL are different identifiers By convention, programmers use different case styles for different types of identifiers, such as title case for class names - Lincoln upper case for constants - MAXIMUM I154-1-A A @ Peter Lo 2010 11 I154-1-A A @ Peter Lo 2010 12

Grouping Classes: The Java API Java Programming Foundation A Quick Look to Java API = Application Programming Interface Java = small core + extensive collection of packages A package consists of some related Java classes: Swing: a GUI (graphical user interface) package AWT: Application Window Toolkit (more GUI) util: utility data structures The import statement tells the compiler to make available classes and methods of another package A main method indicates where to begin executing a class (if it is designed to be run as a program) I154-1-A A @ Peter Lo 2010 13 I154-1-A A @ Peter Lo 2010 14 Comments In Java, comments are preceded by: Two slashes (//) in a line, or Enclosed between /* and */ in one or multiple lines. When the compiler sees //, it ignores all text after // in the same line. When it sees /*, it scans for the next */ and ignores any text between /* and */. Reserved Words Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. For example, when the compiler sees the word class, it understands that the word after class is the name for the class. I154-1-A A @ Peter Lo 2010 15 I154-1-A A @ Peter Lo 2010 16

Modifiers References and Primitive Data Types Java uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used. Examples of modifiers are public and static. Other modifiers are private, final, abstract, and protected. A public datum, method, or class can be accessed by other programs. A private datum or method cannot be accessed by other programs. Java distinguishes two kinds of entities Primitive types Objects Primitive-type data is stored in primitive-type variables Reference variables store the address of an object No notion of object (physically) in the stack No notion of object (physically) within an object I154-1-A A @ Peter Lo 2010 17 I154-1-A A @ Peter Lo 2010 18 Primitive Data Types Represent numbers, characters, boolean values Integers: byte, short, int, and long Real numbers: float and double Characters: char Data Type byte short int -128.. 127 (8 bits) -32,768.. 32,767 (16 bits) Range of Values -2,147,483,648.. 2,147,483,647 (32 bits) Equality and Relational Operators Standard algebraic equality or relational operator Java equality or relational operator Sample Java condition Meaning of Java condition Equality operators = == x == y x is equal to y!= x!= y x is not equal to y Relational operators > > x > y x is greater than y < < x < y x is less than y >= x >= y x is greater than or equal to y <= x <= y x is less than or equal to y long float double char -9,223,372,036,854,775,808..... (64 bits) +/-10-38 to +/-10 +38 and 0, about 6 digits precision +/-10-308 to +/-10 +308 and 0, about 15 digits precision Unicode characters (generally 16 bits per char) I154-1-A A @ Peter Lo 2010 boolean True or false 19 I154-1-A A @ Peter Lo 2010 20

Operators subscript [ ], call ( ), member access. pre/post-increment ++ --, boolean complement!, bitwise complement ~, unary + -, type cast (type), object creation new * / % binary + - (+ also concatenates strings) signed shift << >>, unsigned shift >>> comparison < <= > >=, class test instanceof equality comparison ==!= bitwise and & bitwise or logical (sequential) and && logical (sequential) or conditional cond? true-expr : false-expr assignment =, compound assignment += -= *= /= <<= >>= >>>= &= = Introduction to Object All Java programs are built from classes I154-1-A A @ Peter Lo 2010 21 I154-1-A A @ Peter Lo 2010 22 Classes The class is the essential Java construct. A class is a template or blueprint for objects. To program in Java, you must understand classes and be able to write and use them. For now, though, understand that a program is defined by using one or more classes. What is Object? Object is a collection of data along with the functions to work with that data as opposed to purely procedural languages where the data and functions are separate, and the data is passed to the function to work with In Java, all code must be written inside object definitions A class is an object definition, containing the data and function elements necessary to create an object An instance of an object is one object created from the class definition (the process is known as instantiation) The data and function elements are known as members Data members are also known as fields, properties, or attributes, and function members as methods I154-1-A A @ Peter Lo 2010 23 I154-1-A A @ Peter Lo 2010 24

Interpretation vs Compilation JIT Compilers C++ Source (*.c) C++ Compiler Java Source (*.java) Java Compiler Byte-code (*.class) (machine independent) (machine independent) The trade-off in execution speed can be mitigated by adding a just-in-time (JIT) compiler to the virtual machine. Preserves portability, achieves near native performance after first references Compiling on-the-fly makes initial run slower. Java Virtual Machine Client Platform (hardware/operating system) Client Platform (hardware/operating system) (machine dependent) I154-1-A A @ Peter Lo 2010 25 I154-1-A A @ Peter Lo 2010 26 Memory Organization between Procedural Program and OO Program Benefit of Classes Classes simplify programming, because the client can use only the public methods exposed by the class. Such methods are usually client oriented rather than implementation oriented. Clients are neither aware of, nor involved in, a class s implementation. Clients generally care about what the class does but not how the class does it. I154-1-A A @ Peter Lo 2010 27 I154-1-A A @ Peter Lo 2010 28

Method Methods that modify the values of private variables should verify that the intended new values are proper. If they are not, the set methods should place the private variables into an appropriate consistent state. Interface Interfaces change less frequently than implementations. When an implementation changes, implementation-dependent code must change accordingly. Hiding the implementation reduces the possibility that other program parts will become dependent on class-implementation details. I154-1-A A @ Peter Lo 2010 29 I154-1-A A @ Peter Lo 2010 30 Instance Instance variables Can be initialized when they are declared or in a constructor Should maintain consistent (valid) values If a class does not define a constructor the compiler will provide a default constructor public services (or public interface) public methods available for a client to use Controlling Access to Members A class s s public interface public methods a view of the services the class provides to the class s s clients A class s s implementation details private variables and private methods are not accessible to the class s s clients This object-oriented oriented concept is called Encapsulation. I154-1-A A @ Peter Lo 2010 31 I154-1-A A @ Peter Lo 2010 32

Referring to the Current Object s s Members with the this Reference The this reference Any object can access a reference to itself with keyword this Non-static methods implicitly use this when referring to the object s s instance variables and other methods Can be used to access instance variables when they are shadowed by local variables or method parameters A.java file can contain more than one class But only one class in each.java file can be public Notes on Set and Get Methods Set methods Also known as mutator methods Assign values to instance variables Should validate new values for instance variables Can return a value to indicate invalid data Get methods Also known as accessor methods or query methods Obtain the values of instance variables Can control the format of the data it returns I154-1-A A @ Peter Lo 2010 33 33 I154-1-A A @ Peter Lo 2010 34 Notes on Set and Get Methods Predicate methods Test whether a certain condition on the object is true or false and returns the result Example: an isempty method for a container class (a class capable of holding many objects) Predicate methods always return a boolean data type. Clever naming of predicate methods can make your code easier to understand if (mystack.isempty( mystack.isempty()) Encapsulating specific tasks into their own methods simplifies debugging efforts Composition Composition A class can have references to objects of other classes as members Sometimes referred to as a has-a relationship One form of software reuse is composition, in which a class has as members references to objects of other classes. I154-1-A A @ Peter Lo 2010 35 I154-1-A A @ Peter Lo 2010 36

Final Instance Variables Static Class Members Principle of least privilege Code should have only the privilege and access it needs to accomplish its task, but no more final instance variables Keyword final Specifies that a variable is not modifiable (is a constant) final instance variables can be initialized at their declaration If they are not initialized in their declarations, they must be initialized in all constructors I154-1-A A @ Peter Lo 2010 37 static fields Also known as class variables Represents class-wide information Used when: All objects of the class should share the same copy of this instance variable or This instance variable should be accessible even when no objects of the class exist Can be accessed with the class name or an object name and a dot (.)( Must be initialized in their declarations, or else the compiler will initialize it with a default value (0( for ints) I154-1-A A @ Peter Lo 2010 38 Garbage Collection and Method finalize Garbage collection JVM marks an object for garbage collection when there are no more references to that object JVM s garbage collector will retrieve those objects memory so it can be used for other objects finalize method All classes in Java have the finalize method Inherited from the Object class finalize is called by the garbage collector when it performs termination housekeeping finalize takes no parameters and has return type void Static Class Members String objects are immutable String concatenation operations actually result in the creation of a new String object static method gc of class System Indicates that the garbage collector should make a best- effort attempt to reclaim objects eligible for garbage collection It is possible that no objects or only a subset of eligible objects will be collected static methods cannot access non-static class members Also cannot use the this reference I154-1-A A @ Peter Lo 2010 39 I154-1-A A @ Peter Lo 2010 40

Static Import Enumerations static import declarations Enables programmers to refer to imported static members as if they were declared in the class that uses them Single static import import static packagename.classname.staticmembername; static import on demand import static packagename.classname.*;.*; Imports all static members of the specified class I154-1-A A @ Peter Lo 2010 41 enum types Declared with an enum declaration A comma-separated list of enum constants Declares an enum class with the following restrictions: enum types are implicitly final enum constants are implicitly static Attempting to create an object of an enum type with new is a compilation error enum constants can be used anywhere constants can enum constructor Like class constructors, can specify parameters and be overloaded I154-1-A A @ Peter Lo 2010 42 Enumerations (Cont.) static method values Generated by the compiler for every enum Returns an array of the enum s constants in the order in which they were declared static method range of class EnumSet Takes two parameters, the first and last enum constants in the desired range Returns an EnumSet containing the constants in that range, inclusive An enhanced for statement can iterate over an EnumSet as it can over an array I154-1-A A @ Peter Lo 2010 43