the java language BY SA introduced in 1995 by Sun Microsystems.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

the javascript language BY SA.
the c language BY SA 1972 by Ken Thompson and Dennis Ritchie.
Basic -2 Classes and Objects. Classes and Objects A class is a complex data TYPE An object is an instance of a class. Example: Class: Person Objects:
COSC 2006 Data Structures I Instructor: S. Xu URL:
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Web Application Development Slides Credit Umair Javed LUMS.
Programming with Java. Problem Solving The purpose of writing a program is to solve a problem The general steps in problem solving are: –Understand the.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
A Brief Introduction to Java Programming the World Wide Web.
Outline Java program structure Basic program elements
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
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.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Lecture objectives  Differences between Java and C#  Understand and use abstract classes  Casting in Java: why is it important?  Master exceptions.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
The Java Programming Language
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Types in programming languages1 What are types, and why do we need them?
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Introduction to Java Programming by Laurie Murphy Revised 09/08/2016.
Java Programming Language Lecture27- An Introduction.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Modern Programming Tools And Techniques-I
Intro to Java L. Grewe.
JAVA MULTIPLE CHOICE QUESTION.
Inheritance and Polymorphism
Internet and Java Foundations, Programming and Practice
Computer Science II Exam 1 Review.
Overloading and Constructors
null, true, and false are also reserved.
Java Programming Language
Polymorphism.
Introduction to Java Programming
Chapter 1: Computer Systems
Java Programming Language
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Presentation transcript:

the java language

BY SA

introduced in 1995 by Sun Microsystems

imperative, object-oriented C-style syntax statically-typed (mostly) no pointers JVM (Java Virtual Machine) garbage collection exceptions

SE (standard edition) EE (enterprise edition) ME (micro edition)

Java 6(2006) Java 7(2010?)

class (data type definition) field (data member) method (function member) object / instance (piece of data)

class Moose { Rat r; Hamster h; void foo() {…} }

encapsulation (methods act as “interface” to object’s fields)

object.field object.method(args) apple.banana apple.banana.orange nadine.ed() nadine.ed().laura nadine.ed().laura.dale()

object.field object.method(args) apple.banana (apple.banana).orange nadine.ed() (nadine.ed()).laura ((nadine.ed()).laura).dale ()

type name; new type(args) Moose m; // null m = new Moose(); Moose m = new Moose();

class Moose { Rat r; Hamster h; Moose() {…} // constructor void foo() {…} }

Moose() { this.r = new Rat(); this.h = new Hamster(); }

Moose(Hamster h, Rat r) { this.r = r; this.h = h; } new Moose(new Hamster(), new Rat())

inheritance AC DE F G B DE CB A CB A

Ted Kate Milton illegal

Object Jack Samantha Brad Ted Kate LisaMilton Oliver

class Terry {…} // extends Object class John extends Ben {…}

Jack Ted Kate Milton illegal

is-a vs. has-a Think before using inheritance.

Object Jack Ted Kate Lisa Mike Hugh Olivia Uri Amber (probably) bad

Hamster h = new Hamster(); Mammal m = h; h.runOnWheel(); // OK m.runOnWheel(); // illegal

void bar(Mammal m) {…} x.bar(new Mammal()) x.bar(new Hamster())

Mammal foo() { if (…) { return new Mammal(); } else { return new Hamster(); }

(type) expression Mammal m = (Mammal) new Hamster(); // upcast

(type) expression Mammal m = new Hamster(); // implicit upcast Hamster h = (Hamster) m; // downcast Hamster h = m; // illegal

Lamp l = (Lamp) new Duck (); // illegal

Object Jack Ted Kate Lisa Mike Ted t; … t.foo(); Kate k; … k.foo(); void foo() overriding (redefining an inherited method)

Nick Diane Object void foo()

Nick Diane void foo() Object

interface Philip { void foo(); } class Diane implements Philip {…} class Nick implements Philip {…}

Philip x; if (…) { x = new Nick(); } else { x = new Diane(); } x.foo(); // legal Nick Diane Object void foo()

Philip x = new Ian(); Nick Diane Object Ian void foo()

1.Compiler checks compile time type. 2.Method invoked depends upon runtime type… 3.…except this always invokes version of own class.

void bar() { this.foo(); } Mary void foo() Leo void foo() void bar() Leo l = new Leo(); l.bar();

1.Compiler checks compile time type. 2.Method invoked depends upon runtime type… 3.…except this always invokes own class’s version… 4.…and super always invokes inherited version.

void ack() { super.foo(); } Ryan void foo() void ack() Heather void foo() Heather h = new Heather(); h.ack();

void foo() { super.foo(); … } Ryan void foo() void ack() Heather void foo()

void foo(Rat r) { r.bar(this); }

void foo(Rat r) { r.bar(super); // illegal }

primitive types byte 1-byte signed integer short 2-byte signed integer int 4-byte signed integer long 8-byte signed integer char 2-byte unsigned integer boolean true and false float single-precision floating-point double double-precision floating-point

BigInteger BigDecimal (arbitrary-precision integer) (arbitrary-precision decimal)

primitive types: variable holds a value == tests equality cast produces a new value reference types: variable holds a reference == tests identity cast appeases compiler

int i = 60; float f = 5.4; i = (int)f; f = (float)i;

int i = 5; byte b = 3; b = (byte)i; i = b; // implicit cast

static member (a member which is not a member) class Ian { static Fran f; // Ian.f static void foo() {…} // Ian.foo() … }

class Ian { static Fran f; // Ian.f static void foo() {…} // Ian.foo() … } Ian i = new Ian(); i.foo(); i.f = new Fran();

1.Compiler checks compile-time type. 2.Method invoked depends upon runtime type… 3.…except this always invokes own class’s version… 4.…and super always invokes inherited version… 5.…and static determined solely by compile-time type.

package name; package shark; package pig.tiger;

shark.Ant h = new shark.Ant();

package shark; Ant h = new Ant(); pig.tiger.Cow h = new pig.tiger.Cow();

package shark; import pig.tiger.Cow; import pig.tiger.Lemur; Lemur l = new Lemur(); Cow c = new Cow();

java.lang java.lang.Object

java.lang.String String s = “hello”; int l = s.length(); // 5 s = s.toUpperCase(); // “HELLO” “Hello, ” + “Ron”  “Hello, Ron” 5 + “ golden rings”  “5 golden rings”

public (everywhere) protected * (same package + subclasses) default (same package) private * (same class) * applicable only to members visibility

overloading (same name, different method) void foo() void foo(Mammal m, Lamp l) byte foo(int a) void foo(Lamp l, Mammal m) Sean s = new Sean(); s.foo(new Lamp(), new Mammal()); s.foo(35); Sean

overloading (same name, different method) void foo() void foo(Mammal m, Lamp l) byte foo(int a) // conflict void foo(Lamp l, Mammal m) float foo(int b) // conflict Sean s = new Sean(); s.foo(35); // ambiguous Sean

overloading (same name, different method) void foo() void foo(Mammal m, Lamp l) byte foo(int a) void foo(Lamp l, Mammal m) char foo(Lamp l, Animal m) Sean s = new Sean(); s.foo(new Lamp(), new Hamster()); Sean

overloading (same name, different method) void foo() void foo(Mammal m, Lamp l) byte foo(int a) void foo(Lamp l, Mammal m) char foo(Lamp l, Animal m) Sean

void foo(int a) (overload) void foo() (override) void foo() Jill Donald

Jill void foo(int a) (overload) char foo() (illegal) Donald void foo()

Moose(Hamster h, Rat r) { this.r = r; this.h = h; } Moose() { this.r = new Rat(); this.h = new Hamster(); } new Moose(new Hamster(), new Rat())

Moose(Hamster h, Rat r) { this.r = r; this.h = h; } Moose() { this(new Hamster(), new Rat()); }

Moose(Hamster h, Rat r) { this.r = r; this.h = h; } Moose() { // not what we want new Moose(new Hamster(), new Rat()); }

Moose(Hamster h, Rat r) { this.r = r; this.h = h; } Moose() { // sensical, but not legal Java Moose(new Hamster(), new Rat()); }

Moose(Hamster h, Rat r) { this.r = r; this.h = h; } Moose() { this(new Hamster(), new Rat()); }

Moose(Hamster h, Rat r) { super(); // invoke constructor of Moose’s parent this.r = r; this.h = h; } Moose() { this(new Hamster(), new Rat()); }

Object Jack Ted Kate new Kate()

1.Constructors invoked only via new, this(), or super(). 2.Calls to this() and super() only can be first line. 3.The first line is always this() or super(). 4.Default first line is super() with no arguments. constructor rules

// infinite recursion Moose(Hamster h, Rat r) { this(); } Moose() { this(new Hamster(), new Rat()); }

1.Constructors invoked only via new, this(), or super(). 2.The first line is always this() or super(). 3.Calls to this() and super() only go in first line. 4.Default first line is super() with no arguments. 5.A this() call cannot be recursive. 6.To return, always just use return ; constructor rules

Object Throwable Exception RuntimeException Error exceptions

Cat c; … c.meow(); // exception if c is null

Cat c; … try { c.meow(); // exception if c is null } catch (NullPointerException e) { … }

Object Throwable Exception RuntimeException* Error* *unchecked

// illegal void bar() { if (…) { throw new Leo(); // checked exception }

// OK void bar() throws Leo { if (…) { throw new Leo(); // checked exception }

// OK void bar() { try { if (…) { throw new Leo(); // checked exception } } catch (Leo e) { … }

void bar() throws FooException, AckException {…} void bar() throws Exception {…}

arrays (fixed-sized, homogenous collection) Apple ref Apple[] Apple object Fuji object

Mammal[] m; m = new Mammal[3]; m[2] = new Mammal(); m[0] = new Cat(); int i = m.length; // 3

Object Lisa Jerry Natalie Wyatt Object[] Lisa[] Jerry[] Natalie[] Wyatt[]

Cat[] c = new Cat[3]; Mammal[] m = c; Object[] o1 = c; Object o2 = c;

Mammal[] m = new Cat[4]; m[0] = new Mammal(); // exception

Mammal[] m = new Cat[4]; m[0] = new Cat(); Cat mittens = m[0]; // compile error

Mammal[] m = new Cat[4]; m[0] = new Cat(); Cat mittens = (Cat) m[0]; // OK

Object Lisa Jerry Natalie Wyatt Object[][] Lisa[][] Jerry[][] Natalie[][] Wyatt[][]

Apple[] ref Apple[][] Apple[] object Fuji[] object

Cat[][] c = new Cat[3][]; c[1] = new Cat[5]; c[1][0] = new Cat();

Cat[][] c = new Cat[3][]; c[1] = new Cat[5]; (c[1])[0] = new Cat();

Object Lisa Jerry Natalie Wyatt Object[][][] Lisa[][][] Jerry[][][] Natalie[][][] Wyatt[][][]

Apple[][] ref Apple[][][] Apple[][] object Fuji[][] object

Object Object[][][]Object[][] Object[] Object[][][][]

int int[]

int[] ref int[][] int[] object

Object char[][][]byte[][]int[]float[]

public class HelloWorld { public static void main(String[] args) { System.out.println(“Hello, world!”); } Hello, world!

inner classes generics abstract classes wrapper classes enumerations annotations assert final