Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 2016CMPE212 - Prof. McLeod1

2 Winter 2016CMPE212 - Prof. McLeod2

3 Over Time: Winter 2016CMPE212 - Prof. McLeod3

4 Winter 2016CMPE212 - Prof. McLeod4 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…

5 Winter 2016CMPE212 - Prof. McLeod5 Java History, Cont. 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”! What else happened in the early 90’s?

6 Winter 2016CMPE212 - Prof. McLeod6 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.

7 Winter 2016CMPE212 - Prof. McLeod7 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.

8 Winter 2016CMPE212 - Prof. McLeod8 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. HTML File Applet Remote File Server Browser Local Client JVM Internet Byte code files

9 Winter 2016CMPE212 - Prof. McLeod9 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.

10 Winter 2016CMPE212 - Prof. McLeod10 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.

11 Winter 2016CMPE212 - Prof. McLeod11 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!

12 Winter 2016CMPE212 - Prof. McLeod12 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

13 Winter 2016CMPE212 - Prof. McLeod13 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.

14 Winter 2016CMPE212 - Prof. McLeod14 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 ).

15 Winter 2016CMPE212 - Prof. McLeod15 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.

16 Winter 2016CMPE212 - Prof. McLeod16 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.

17 Winter 2016CMPE212 - Prof. McLeod17 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. Method Declaration (a “Header”)

18 Winter 2016CMPE212 - Prof. McLeod18 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 pre- defined 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) {…}

19 Winter 2016CMPE212 - Prof. McLeod19 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.

20 Winter 2016CMPE212 - Prof. McLeod20 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, …


Download ppt "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."

Similar presentations


Ads by Google