java 1234319 coursewinter 2009/10. introduction object oriented, imperative programming language....

33
Java Java 1 234319 Course Winter 2009/10

Upload: darren-kennedy

Post on 22-Dec-2015

232 views

Category:

Documents


0 download

TRANSCRIPT

JavaJava

1234319 CourseWinter 2009/10

IntroductionIntroduction

• Object oriented, imperative programming language.• Developed: 1990.

• Inspired by C++ programming language.• Improvements:

– Syntax– General purpose libraries– Garbage collection– Platform independence with ‘Java Virtual Machine’

• Performance?

2Winter 2009/10 234319 Course

OOP BasicsOOP Basics

• Java programs define classes, and objects that are instantiated from them.– All methods (functions) are class members. – Variables are class members, or local function variables.– No multiple inheritance

• But can implement multiple interfaces (learn in OOP course).

– Classes can be nested.

• These definitions allow us to hide away the technical details inherent to imperative programming.– set value to X, read value from Y, etc…

3Winter 2009/10 234319 Course

No Pointers in JavaNo Pointers in Java

• Another way to hide away the technical details– No pointers arithmetic, no pointers to functions, no

memory management, etc.

• Things you could do in C++ int a[10], b[10];

for (int i = 0; i < 20; i++) {

a[i] = 0;

}

• But also int a[10];

for (int i = 0; i <= 10; i++)

a[i] = some_function(i);

4Winter 2009/10 234319 Course

When will the problem be detected?

Java has a safe array accessJava has a safe array access• Yet another way to hide away the gory details• Every line like

a[i] = 7;

is compiled into:

if (i < 0 || i > 9) BOOM!

else a[i] = 7;

• BOOM is actually an IndexOutOfBoundsExceptioninstance being thrown.

• C/C++ compilers cannot add such checks to generated code.

5Winter 2009/10 234319 Course

Java performanceJava performance• Historically, Java's performance is considered slow.• Two reasons:

– Code runs on Virtual Machine– Garbage collector

• In reality, modern JVMs are very powerful, and can optimize code aggressively.

• Java programs often have slow start-up times, but after that, performance is often on-par with C++ code.

6Winter 2009/10 234319 Course

Saying Hello!Saying Hello!class Hello {

public static void main(String[] args) {

System.out.println(“Hello World”);

}

}

7Winter 2009/10 234319 Course

Saying Hello!Saying Hello!class Hello {

public static void main(String[] args) {

System.out.println(“Hello World”);

}

}

• The class keyword is used to define classes• By convention, class names begin with capital letter• All code and variables in Java reside in classes• If no parent was explicitly specified, the new class is

derived from the java.lang.Object class

8Winter 2009/10 234319 Course

Saying Hello!Saying Hello!class Hello {

public static void main(String[] args) {

System.out.println(“Hello World”);

}

}

• The method is public– Same meaning as in C++

• Unlike C++, in Java the access level is specified explicitly before each and every member variable and method

9Winter 2009/10 234319 Course

Saying Hello!Saying Hello!class Hello {

public static void main(String[] args) {

System.out.println(“Hello World”);

}

}

• The method is static– Same meaning as in C++– Can be accessed without a class instance

• static can be applied both to methods and member variables

10Winter 2009/10 234319 Course

Saying Hello!Saying Hello!class Hello {

public static void main(String[] args) {

System.out.println(“Hello World”);

}

}

• We are defining a void method– Meaning it does not return a value– Like void functions in C++, or procedures in Pascal

11Winter 2009/10 234319 Course

Saying Hello!Saying Hello!class Hello {

public static void main(String[] args) {

System.out.println(“Hello World”);

}

}

• The method is called “main”• Unlike in C++, there’s nothing special about the name

“main” by itself• It is only in combination with the other properties of this

method that it gets some special meaning– As the program’s starting point, of course

12Winter 2009/10 234319 Course

Saying Hello!Saying Hello!class Hello {

public static void main(String[] args) {

System.out.println(“Hello World”);

}

}

• Function accepts a single formal argument• The argument’s name is “args”• The argument’s type is an array of Strings

13Winter 2009/10 234319 Course

Saying Hello!Saying Hello!class Hello {

public static void main(String[] args) {

System.out.println(“Hello World”);

}

}

• System is the name of a class– From the java.lang package

• This class contains many basic system-related objects– Such as the console I/O objects

14Winter 2009/10 234319 Course

Saying Hello!Saying Hello!class Hello {

public static void main(String[] args) {

System.out.println(“Hello World”);

}

}

• The class System has a member field called out– of type PrintStream– It is static, so we can access it without an instance of theSystem class

– It is also public, otherwise we would have been unable to access it

15Winter 2009/10 234319 Course

Saying Hello!Saying Hello!class Hello {

public static void main(String[] args) {

System.out.println(“Hello World”);

}

}

• The out object, just like any instance of PrintStream, has a method called println

• Can you guess what this method does?

16Winter 2009/10 234319 Course

Remember simple problem?Remember simple problem?

• Given a range of positive numbers:– Summarize all numbers in range that divide by 3 or 5.– Print the result.

17Winter 2009/10 234319 Course

Version 1Version 1public class Sum {

private int sum;public Sum() {

sum = 0;}private void sumOfMatching(int begin, int end) {

for ( int i = begin ; i < end ; ++i )if ( i % 3 == 0 || i % 5 == 0 )

sum += i;}private int getSum() {

return sum;}public static void main(String[] args) {

Sum s = new Sum();s.sumOfMatching(1, 100);System.out.println( s.getSum() );

}}

18Winter 2009/10 234319 Course

Version 1Version 1public class Sum {

private int sum;public Sum() {

sum = 0;}private void sumOfMatching(int begin, int end) {

for ( int i = begin ; i < end ; ++i )if ( i % 3 == 0 || i % 5 == 0 )

sum += i;}private int getSum() {

return sum;}public static void main(String[] args) {

Sum s = new Sum();s.sumOfMatching(1, 100);System.out.println( s.getSum() );

}}

19Winter 2009/10

Private field

234319 Course

Version 1Version 1public class Sum {

private int sum;public Sum() {

sum = 0;}private void sumOfMatching(int begin, int end) {

for ( int i = begin ; i < end ; ++i )if ( i % 3 == 0 || i % 5 == 0 )

sum += i;}private int getSum() {

return sum;} public static void main(String[] args) {

Sum s = new Sum();s.sumOfMatching(1, 100);System.out.println( s.getSum() );

}}

20Winter 2009/10

Constructor

234319 Course

Version 1Version 1public class Sum {

private int sum;public Sum() {

sum = 0;}private void sumOfMatching(int begin, int end) {

for ( int i = begin ; i < end ; ++i )if ( i % 3 == 0 || i % 5 == 0 )

sum += i;}private int getSum() {

return sum;}public static void main(String[] args) {

Sum s = new Sum();s.sumOfMatching(1, 100);System.out.println( s.getSum() );

}}

21Winter 2009/10

Private methods

234319 Course

Version 1Version 1public class Sum {

private int sum;public Sum() {

sum = 0;}private void sumOfMatching(int begin, int end) {

for ( int i = begin ; i < end ; ++i )if ( i % 3 == 0 || i % 5 == 0 )

sum += i;}private int getSum() {

return sum;}public static void main(String[] args) {

Sum s = new Sum();s.sumOfMatching(1, 100);System.out.println( s.getSum() );

}}

22Winter 2009/10

No unsigned int

234319 Course

Version 1Version 1public class Sum {

private int sum;public Sum() {

sum = 0;}private void sumOfMatching(int begin, int end) {

for ( int i = begin ; i < end ; ++i )if ( i % 3 == 0 || i % 5 == 0 )

sum += i;}private int getSum() {

return sum;}public static void main(String[] args) {

Sum s = new Sum();s.sumOfMatching(1, 100);System.out.println( s.getSum() );

}}

23Winter 2009/10

Main function

Can ‘main’ be located in a different class?

234319 Course

Version 2Version 2public class Sum {

private int sum = 0;

private boolean isMatching(int i) {

return i % 3 == 0 || i % 5 == 0;

}

private void sumOfMatching(int begin, int end)

throws IllegalArgumentException {

if ( begin < 0 || end < begin )

throw new IllegalArgumentException();

for ( int i = begin ; i < end ; ++i )

if ( isMatching(i) )

sum += i;

}

private int getSum() { … }

public static void main(String[] args) { … }

}

24Winter 2009/10 234319 Course

Version 2Version 2public class Sum {

private int sum = 0;

private boolean isMatching(int i) {

return i % 3 == 0 || i % 5 == 0;

}

private void sumOfMatching(int begin, int end)

throws IllegalArgumentException {

if ( begin < 0 || end < begin )

throw new IllegalArgumentException();

for ( int i = begin ; i < end ; ++i )

if ( isMatching(i) )

sum += i;

}

private int getSum() { … }

public static void main(String[] args) { … }

}

25Winter 2009/10

Initializing field w/o constructor

234319 Course

Version 2Version 2public class Sum {

private int sum = 0;

private boolean isMatching(int i) {

return i % 3 == 0 || i % 5 == 0;

}

private void sumOfMatching(int begin, int end)

throws IllegalArgumentException {

if ( begin < 0 || end < begin )

throw new IllegalArgumentException();

for ( int i = begin ; i < end ; ++i )

if ( isMatching(i) )

sum += i;

}

private int getSum() { … }

public static void main(String[] args) { … }

}

26Winter 2009/10

Extract method

234319 Course

Version 2Version 2public class Sum {

private int sum = 0;

private boolean isMatching(int i) {

return i % 3 == 0 || i % 5 == 0;

}

private void sumOfMatching(int begin, int end)

throws IllegalArgumentException {

if ( begin < 0 || end < begin )

throw new IllegalArgumentException();

for ( int i = begin ; i < end ; ++i )

if ( isMatching(i) )

sum += i;

}

private int getSum() { … }

public static void main(String[] args) { … }

}

27Winter 2009/10

Exception declaration

234319 Course

Version 2Version 2public class Sum {

private int sum = 0;

private boolean isMatching(int i) {

return i % 3 == 0 || i % 5 == 0;

}

private void sumOfMatching(int begin, int end)

throws IllegalArgumentException {

if ( begin < 0 || end < begin )

throw new IllegalArgumentException();

for ( int i = begin ; i < end ; ++i )

if ( isMatching(i) )

sum += i;

}

private int getSum() { … }

public static void main(String[] args) { … }

}

28Winter 2009/10

Throwing exception

234319 Course

More improvementsMore improvements• Like in the Pascal implementation we can

change ‘3’ and ‘5’ to be given as parameters:– To sumOfMatching.– To class itself (via constructor).

• But let’s try something a bit more general and OO…– Define abstract class Matcher with abstract method isMatching.

– Subclass Matcher and implement isMatching the way we want to.

29Winter 2009/10 234319 Course

Version 3Version 3public abstract class Matcher {

public abstract boolean isMatching( int i );

}

public class TwoNumDivMatcher extends Matcher {

private int div1;

private int div2;

public TwoNumDivMatcher( int div1, int div2 ) {

this.div1 = div1;

this.div2 = div2;

}

public boolean isMatching( int i ) {

return i % div1 == 0 || i % div2 == 0;

}

}

30Winter 2009/10 234319 Course

Version 3Version 3public abstract class Matcher {

public abstract boolean isMatching( int i );

}

public class TwoNumDivMatcher extends Matcher {

private int div1;

private int div2;

public TwoNumDivMatcher( int div1, int div2 ) {

this.div1 = div1;

this.div2 = div2;

}

public boolean isMatching( int i ) {

return i % div1 == 0 || i % div2 == 0;

}

}

31Winter 2009/10

We must override ‘isMatching’

(Why?)

234319 Course

Version 3 – cont.Version 3 – cont.public class Sum {

private int sum = 0;

private void sumOfMatching(int begin, int end, Matcher m)

throws IllegalArgumentException {

if ( begin < 0 || end < begin )

throw new IllegalArgumentException();

for ( int i = begin ; i < end ; ++i )

if ( m.isMatching(i) )

sum += i;

}

public static void main(String[] args) {

Sum s = new Sum();

s.sumOfMatching( 1, 100, new TwoNumDivMatcher(3, 5) );

System.out.println( s.getSum() );

}

32Winter 2009/10 234319 Course

Version 3 – cont.Version 3 – cont.public class Sum {

private int sum = 0;

private void sumOfMatching(int begin, int end, Matcher m)

throws IllegalArgumentException {

if ( begin < 0 || end < begin )

throw new IllegalArgumentException();

for ( int i = begin ; i < end ; ++i )

if ( m.isMatching(i) )

sum += i;

}

public static void main(String[] args) {

Sum s = new Sum();

s.sumOfMatching( 1, 100, new TwoNumDivMatcher(3, 5) );

System.out.println( s.getSum() );

}

33Winter 2009/10 234319 Course