An introduction to Java Bruce Eckel, Thinking in Java, 4th edition, PrenticeHall, New Jersey, cf. http://mindview.net/Books/TIJ4 [email protected] José Valente de Oliveira 4-1 http://oreilly.com/pub/a/oreilly/news/languageposter_0504.html [email protected] José Valente de Oliveira 3-2 1 A short history of OO languages [email protected] 3-3 José Valente de Oliveira A (tiny) family of programming languages Fortran Cobol BASIC Algol 60 Simula C C++ Algol 68 Pascal Perl Modula 3 LISP PL/1 ML Scheme Smalltalk Dylan Ada Prolog Java C# Python Ruby 3-4 2 The 70s at XEROX Palo Alto Research Center Graphical User Interface Ethernet First personal computer PostScript Printers Object-Oriented Programming [email protected] José Valente de Oliveira 3-5 Dynabook, 1972 3-6 3 “Don’t worry about what anybody else is going to do… The best way to predict the future is to invent it. Really smart people with reasonable funding can do just about anything that doesn't violate too many of Newton's Laws!” (Alan Kay, 1971) [email protected] José Valente de Oliveira 3-7 Smalltalk Everything is an object Objects communicate by sending and receiving messages Objects build up from other objects How do you do b aa ++ b send the object a the message + b [email protected] José Valente de Oliveira 3-8 4 Smalltalk – A typical method Point >> dist: aPoint “Answer the distance between the receiver and aPoint.” | dx dy | dx := self x – aPoint x. dy := self y – aPoint y. ^(dx*dx + (dy*dy)) sqrt [email protected] 3-9 José Valente de Oliveira Mainstream object-oriented programming languages Smalltalk (Alan Kay, Xerox PARC, 1971) C++ (Bjarne Stroustrup, Bell Labs, 1983) B. Stroustrup Java (James Gosling, Sun Microsystems, 1995) Objective C (Brad Cox, and Tom Love,1980s) C# (Microsoft, 2000) J. Gosling [email protected] José Valente de Oliveira 3-10 5 Long term trends of Tiobe indices as of September 2015 From: http://www.tiobe.com Java main features Sun said that Java is characterized by the following features: Simple Architecture neutral Object-oriented Portable Distributed Robust Secure Efficient Multithreaded Dynamic [email protected] José Valente de Oliveira 3-12 6 Executing Java code [email protected] José Valente de Oliveira 3-13 Write Once Run Anywhere [email protected] José Valente de Oliveira 3-14 7 Overview of the Java platform [email protected] José Valente de Oliveira 3-15 Java platform diagram [email protected] José Valente de Oliveira 3-16 8 Java – from the very beginning // file: First.java public class First { public static void main(String[] args) { System.out.print(“Hello Java"); } } [email protected] José Valente de Oliveira 3-17 Control strucutures All control structures available in C/C++ are also available in Java The same goes to relational and logic operators [email protected] José Valente de Oliveira 3-18 9 Primitive data types Data type byte Range of values -128 .. 127 (8 bits) short -32,768 .. 32,767 (16 bits) int -2,147,483,648 .. 2,147,483,647 (32 bits) long -9,223,372,036,854,775,808 .. ... (64 bits) float +/-10-38 to +/-10+38 and 0, about 6 digits precision double +/-10-308 to +/-10+308 and 0, about 15 digits precision char Unicode characters (generally 16 bits per char) boolean true or false (1 bit) [email protected] José Valente de Oliveira 3-19 public class Average { public static void main (String [] args) { int n; Scanner sc = new Scanner(System.in); At this point I can code in Java just do{ like");I used to code in C. I can even System.out.print("N: n = sc.nextInt(); use functions. I’m not using the power of oop though if (n<=0) System.out.println(“Invalid!"); } while(n<=0); float [] x = readFloatArray(sc, n); // computing the average float sum=0.0f; for(int i=0; i<n; i++ ) sum +=x[i]; float av = sum / n; System.out.println(“Average: " + av ); } [email protected] José Valente de Oliveira 3-20 10 private static float [] readFloatArray(Scanner sc, int n) { float [] fa = new float [n]; for(int i=0; i< fa.length; i++ ) { System.out.print("x"+i+"? "); fa[i] = sc.nextFloat(); } return fa; } } [email protected] José Valente de Oliveira 3-21 To take away today Java: A little bit of History Goals and characteristics First programs [email protected] José Valente de Oliveira 3-22 11