Today… • “Hello World” ritual. • Brief History of Java & How Java Works. • Introduction to Java class structure. • But first, next slide shows Java is No. 1 programming language for 2015. (From Tiobe.com). Winter 2016 CMPE212 - Prof. McLeod 1 Winter 2016 CMPE212 - Prof. McLeod 2 Over Time: Winter 2016 CMPE212 - Prof. McLeod 3 A Very Brief History of Java • The language was first developed by James Gosling at Sun Microsystems in 1991. – He was designing a language, called “Oak”, for the “Green Project”. – The Green Project envisaged the centralized control of many processor-based devices in the home. − “Oak” was designed to be a robust, efficient language with maximum portability to different processors. − The Green Project flopped… Winter 2016 CMPE212 - Prof. McLeod 4 Java History, Cont. • What else happened in the early 90’s? • Internet use started to blossom in the early 90’s. Web pages had to do more than just display static text and graphics. • Needed dynamic and interactive content. • But, web pages are viewed on a wide variety of platforms, from Mac’s to Unix to IBM-PC’s. • So any page-embedded language would need to run on all these platforms. • Needed a robust, compact, multiplatform language, so let’s dust off Oak and call it something racy like “Java”! Winter 2016 CMPE212 - Prof. McLeod 5 Java History, Cont. • In 1994, Sun demonstrated the use of Java in small bundles of code embedded in a web page called applets. • Netscape browsers started supporting applets in 1995, starting Java’s rise to fame. • Sun programmers continued to develop a code base for the language, adding many libraries. • They showed that Java could be used for more than just applets, and that full-blown applications could be written in this high-level language. Winter 2016 CMPE212 - Prof. McLeod 6 How Java Works • The Java language standard (the syntax) is identical for all platforms. • A compiler ( part of the “JDK”, or “Java Development Kit” – sometimes called javac.exe) which is designed to run on your development platform, compiles your source code (*.java file) to a byte code file (*.class file). • The byte code file is platform-independent, and is the thing you attach to your web page as an applet. • Every browser written for every platform and OS, can have an embedded code processor called a JVM, or “Java Virtual Machine”, built-in. Winter 2016 CMPE212 - Prof. McLeod 7 How Java Works, Cont. • The JVM takes the byte code and executes it by generating the machine code that will be recognized by the platform that is running the browser. Local Client Remote File Server Browser HTML File Internet Applet Winter 2016 Applet Byte code files CMPE212 - Prof. McLeod JVM 8 How Java Works, Cont. • Of course it did not take long before people took the JVM out of the browser so that they could run stand-alone Java applications. This is the JRE or “Java Runtime Engine” (java.exe). • The concept of write once, run anywhere is very appealing! “Save your $$!” • And, Oracle distributes the JDK’s for free, making development on many platforms inexpensive. Winter 2016 CMPE212 - Prof. McLeod 9 How Java Works, Cont. • However, all IDE’s, including Eclipse, must use the appropriate JDK in the background. • Two components of the JDK are the programs “javac.exe” and “java.exe”. • javac.exe is the byte code compiler, and java.exe is the JRE which executes the byte code file. • “Compilation” is the process of converting the *.java file to a *.class file (the byte code file). This is done by calling javac.exe in the background, and supplying that program with all the required command line parameters. Winter 2016 CMPE212 - Prof. McLeod 10 How Java Works, Cont. • The java.exe program: – – – – – accepts the byte code file, links in any required libraries, creates executable code in memory converts it to machine language and sends it to the CPU. • The java.exe program must know the right machine language commands for just the type of CPU it is designed for! Winter 2016 CMPE212 - Prof. McLeod 11 OOP in Java • A class definition or “object definition”, consists of the definition of instance variables and/or methods. • By convention, instance variables are all declared before the methods: public class ShowStructure { // instance variables or “attributes” here // methods here } // end class ShowStructure Winter 2016 CMPE212 - Prof. McLeod 12 OOP in Java – Cont. • In Java, a class is an Object, and an Object is a class (rather Zen is it not!) • A variable can be used to address an instance of a class, which is also an Object. • Code and attributes cannot be defined outside of a class. • The only code that can exist outside a method are attributes or other (“inner”) class definitions. Winter 2016 CMPE212 - Prof. McLeod 13 Attributes • Also called “class variables” or “instance variables” or “fields”. • Declared within a class at the same level as the method declarations. • These variables are known to all methods within a class (their “scope”). • You can control their privacy and the way they are stored in memory (using public/private/protected and static). Winter 2016 CMPE212 - Prof. McLeod 14 Attribute Declaration • Syntax: [private|public] [static] [final] type attributeName [= literalValue]; • Note that the type part is not optional – this is why java is a “declarative” language. • And, a variable cannot change its type later (“static typing”). • You cannot use a variable unless you have declared it first. Winter 2016 CMPE212 - Prof. McLeod 15 Variable Declaration • Declaring a variable inside a method gives that variable the scope of just inside the method, not outside the method. • Generally a variable is only available inside the block ({…}) in which it is declared. • The syntax for declaration inside a method is the same except you don’t need the [private|public] [static] [final] parts. Winter 2016 CMPE212 - Prof. McLeod 16 Method Declaration (a “Header”) • The syntax for simple method declaration: [private|public] [static] [final] returnType methodName ([parameterList]) {…} • If main invokes methods or uses attributes in the same class as itself then those attributes and methods must also be declared static. Winter 2016 CMPE212 - Prof. McLeod 17 Method Declaration - Cont. • A method must have a returnType. • The returnType can be any single Object or a primitive type. (For example: an int, double, String, an array (like int[], or any other predefined Object.) • If the method does not return anything, then the keyword void is used instead. • The main method does not return any value, so that’s why it is declared as in: public static void main (String[] args) {…} Winter 2016 CMPE212 - Prof. McLeod 18 Aside - The main Method • For the JVM to run an application, it must know where to start. • By design, the starting point is always the execution of the main method. • The JVM expects the main method to be declared exactly as shown – the only thing you can change is the name of the String array, called args above. Winter 2016 CMPE212 - Prof. McLeod 19 Method Declaration - Cont. • parameterList provides a means of passing items, or parameters, into a method. • It is optional. • It can consist of one or many parameters, separated by commas. • Each parameter type must be declared in the parameter list, as in type variableName, type variableName, … Winter 2016 CMPE212 - Prof. McLeod 20