330 likes | 496 Views
Java. 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?. OOP Basics.
E N D
Java 234319 Course
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? 234319 Course
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… 234319 Course
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); When will the problem be detected? 234319 Course
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 < 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. 234319 Course
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. 234319 Course
Saying Hello! class Hello { public static void main(String[] args) { System.out.println(“Hello World”); } } 234319 Course
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 234319 Course
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 234319 Course
Saying Hello! class Hello { public static void main(String[] args) { System.out.println(“Hello World”); } } • The method isstatic • Same meaning as in C++ • Can be accessed without a class instance • static can be applied both to methods and member variables 234319 Course
Saying Hello! class Hello { public static voidmain(String[] args) { System.out.println(“Hello World”); } } • We are defining a voidmethod • Meaning it does not return a value • Like void functions in C++, or procedures in Pascal 234319 Course
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 234319 Course
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 234319 Course
Saying Hello! class Hello { public static void main(String[] args) { System.out.println(“Hello World”); } } • System is the name of a class • From thejava.langpackage • This class contains many basic system-related objects • Such as the console I/O objects 234319 Course
Saying Hello! class Hello { public static void main(String[] args) { System.out.println(“Hello World”); } } • The class Systemhas 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 234319 Course
Saying Hello! class Hello { public static void main(String[] args) { System.out.println(“Hello World”); } } • Theoutobject, just like any instance of PrintStream, has a method called println • Can you guess what this method does? 234319 Course
Remember simple problem? • Given a range of positive numbers: • Summarize all numbers in range that divide by 3 or 5. • Print the result. 234319 Course
Version 1 publicclass Sum { privateintsum; public Sum() { sum = 0; } privatevoid sumOfMatching(int begin, intend) { for ( inti = begin ; i < end ; ++i ) if ( i % 3 == 0 || i % 5 == 0 ) sum += i; } privateintgetSum() { return sum; } publicstaticvoid main(String[] args) { Sum s = new Sum(); s.sumOfMatching(1, 100); System.out.println( s.getSum() ); } } 234319 Course
Version 1 Private field publicclass Sum { private int sum; public Sum() { sum = 0; } privatevoid sumOfMatching(int begin, intend) { for ( inti = begin ; i < end ; ++i ) if ( i % 3 == 0 || i % 5 == 0 ) sum += i; } privateintgetSum() { return sum; } publicstaticvoid main(String[] args) { Sum s = new Sum(); s.sumOfMatching(1, 100); System.out.println( s.getSum() ); } } 234319 Course
Version 1 Constructor publicclass Sum { privateintsum; public Sum() { sum = 0; } privatevoid sumOfMatching(int begin, intend) { for ( inti = begin ; i < end ; ++i ) if ( i % 3 == 0 || i % 5 == 0 ) sum += i; } privateintgetSum() { return sum; } publicstaticvoid main(String[] args) { Sum s = new Sum(); s.sumOfMatching(1, 100); System.out.println( s.getSum() ); } } 234319 Course
Version 1 Private methods publicclass Sum { privateintsum; public Sum() { sum = 0; } private void sumOfMatching(int begin, int end) { for ( inti = begin ; i < end ; ++i ) if ( i % 3 == 0 || i % 5 == 0 ) sum += i; } private intgetSum() { return sum; } publicstaticvoid main(String[] args) { Sum s = new Sum(); s.sumOfMatching(1, 100); System.out.println( s.getSum() ); } } 234319 Course
Version 1 publicclass Sum { privateintsum; public Sum() { sum = 0; } privatevoid sumOfMatching(int begin, int end) { for ( inti = begin ; i < end ; ++i ) if ( i % 3 == 0 || i % 5 == 0 ) sum += i; } privateintgetSum() { return sum; } publicstaticvoid main(String[] args) { Sum s = new Sum(); s.sumOfMatching(1, 100); System.out.println( s.getSum() ); } } No unsigned int 234319 Course
Version 1 Main function publicclass Sum { privateintsum; public Sum() { sum = 0; } privatevoid sumOfMatching(int begin, intend) { for ( inti = begin ; i < end ; ++i ) if ( i % 3 == 0 || i % 5 == 0 ) sum += i; } privateintgetSum() { return sum; } public static void main(String[] args) { Sum s = new Sum(); s.sumOfMatching(1, 100); System.out.println( s.getSum() ); } } Can ‘main’ be located in a different class? 234319 Course
Version 2 public class Sum { private intsum= 0; private booleanisMatching(inti) { return i % 3 == 0 || i % 5 == 0; } private void sumOfMatching(int begin, int end) throwsIllegalArgumentException { if ( begin < 0 || end < begin ) throw new IllegalArgumentException(); for ( inti = begin ; i < end ; ++i ) if ( isMatching(i) ) sum += i; } private intgetSum() { … } public static void main(String[] args) { … } } 234319 Course
Version 2 public class Sum { private int sum = 0; private booleanisMatching(inti) { return i % 3 == 0 || i % 5 == 0; } private void sumOfMatching(int begin, int end) throwsIllegalArgumentException { if ( begin < 0 || end < begin ) throw new IllegalArgumentException(); for ( inti = begin ; i < end ; ++i ) if ( isMatching(i) ) sum += i; } private intgetSum() { … } public static void main(String[] args) { … } } Initializing field w/o constructor 234319 Course
Version 2 public class Sum { private intsum= 0; private booleanisMatching(inti) { return i % 3 == 0 || i % 5 == 0; } private void sumOfMatching(int begin, int end) throwsIllegalArgumentException { if ( begin < 0 || end < begin ) throw new IllegalArgumentException(); for ( inti = begin ; i < end ; ++i ) if ( isMatching(i) ) sum += i; } private intgetSum() { … } public static void main(String[] args) { … } } Extract method 234319 Course
Version 2 public class Sum { private intsum= 0; private booleanisMatching(inti) { 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 ( inti = begin ; i < end ; ++i ) if ( isMatching(i) ) sum += i; } private intgetSum() { … } public static void main(String[] args) { … } } Exception declaration 234319 Course
Version 2 public class Sum { private intsum= 0; private booleanisMatching(inti) { return i % 3 == 0 || i % 5 == 0; } private void sumOfMatching(int begin, int end) throwsIllegalArgumentException { if ( begin < 0 || end < begin ) throw new IllegalArgumentException(); for ( inti = begin ; i < end ; ++i ) if ( isMatching(i) ) sum += i; } private intgetSum() { … } public static void main(String[] args) { … } } Throwing exception 234319 Course
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 isMatchingthe way we want to. 234319 Course
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; } } 234319 Course
Version 3 public abstract class Matcher { public abstract booleanisMatching( inti ); } public class TwoNumDivMatcherextends Matcher { private intdiv1; private intdiv2; publicTwoNumDivMatcher(int div1, int div2 ) { this.div1 = div1; this.div2 = div2; } public booleanisMatching( inti ) { return i % div1 == 0 || i % div2 == 0; } } We must override ‘isMatching’(Why?) 234319 Course
Version 3 – cont. public class Sum { private intsum = 0; privatevoidsumOfMatching(int begin, int end, Matcher m) throwsIllegalArgumentException { if ( begin < 0 || end < begin ) thrownewIllegalArgumentException(); for ( inti = begin ; i < end ; ++i ) if ( m.isMatching(i) ) sum+= i; } public static void main(String[] args) { Sum s = new Sum(); s.sumOfMatching( 1, 100, newTwoNumDivMatcher(3, 5) ); System.out.println( s.getSum() ); } … 234319 Course
Version 3 – cont. public class Sum { private intsum = 0; privatevoidsumOfMatching(int begin, int end, Matcher m) throwsIllegalArgumentException { if ( begin < 0 || end < begin ) thrownewIllegalArgumentException(); for ( inti = 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() ); } … 234319 Course