Download presentation
Presentation is loading. Please wait.
Published byDarren Kennedy Modified over 9 years ago
1
Java 1234319 CourseWinter 2009/10
2
Introduction 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/10234319 Course
3
OOP 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/10234319 Course
4
No 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/10234319 Course When will the problem be detected?
5
Java 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 9) BOOM! else a[i] = 7; BOOM is actually an IndexOutOfBoundsException instance being thrown. C/C++ compilers cannot add such checks to generated code. 5Winter 2009/10234319 Course
6
Java 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/10234319 Course
7
Saying Hello! class Hello { public static void main(String[] args) { System.out.println(“Hello World”); } 7Winter 2009/10234319 Course
8
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/10234319 Course
9
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/10234319 Course
10
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/10234319 Course
11
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/10234319 Course
12
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/10234319 Course
13
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 String s 13Winter 2009/10234319 Course
14
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/10234319 Course
15
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 the System class –It is also public, otherwise we would have been unable to access it 15Winter 2009/10234319 Course
16
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/10234319 Course
17
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/10234319 Course
18
Version 1 public 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/10234319 Course
19
Version 1 public 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
20
Version 1 public 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
21
Version 1 public 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
22
Version 1 public 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
23
Version 1 public 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
24
Version 2 public 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/10234319 Course
25
Version 2 public 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
26
Version 2 public 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
27
Version 2 public 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
28
Version 2 public 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
29
More 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/10234319 Course
30
Version 3 public 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/10234319 Course
31
Version 3 public 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
32
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/10234319 Course
33
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/10234319 Course
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.