Download presentation
Presentation is loading. Please wait.
1
CMPE212 – Reminders Course Web Site:
Winter 2019 CMPE212 4/11/2019 CMPE212 – Reminders Course Web Site: Labs start next week. Meet your TA! Handle questions on: JDK, Eclipse installation? Exercises 1 and 3? Assn 1? Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod
2
Aside - Exercises You should do Exercises 1 and 3 before starting on assignment 1. Exercise 2 is not mandatory, but you may be interested in a comparison of C and Java, especially if you have been mostly coding in C. Winter 2019 CMPE212 - Prof. McLeod
3
Today “Hello World” Ritual! A Brief History of Java. How Java works.
Start class structure. Emphasis on syntax for now. Winter 2019 CMPE212 - Prof. McLeod
4
First Example – “Hello World” in Java
Let us have a quick look at Eclipse, create an empty project and add a program that displays “Hello World” to the console window. public class TestHello { public static void main(String[] args) { System.out.println("Hello"); } Winter 2019 CMPE212 - Prof. McLeod
5
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 2019 CMPE212 - Prof. McLeod
6
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 2019 CMPE212 - Prof. McLeod
7
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 2019 CMPE212 - Prof. McLeod
8
Recent History Early in 2010 Oracle acquired Sun Microsystems. Here’s a statement regarding Java from Oracle: “Oracle plans to accelerate investment in the Java platform for the benefit of customers and the Java community. Java is one of the computer industry’s best-known brands and the Java platform is one of the industry’s most widely deployed technologies. Oracle has been a leader in the Java community since the inception of the Java programming language and already has the world’s largest investment in the Java platform, which provides the foundation for its Oracle Fusion Middleware products and its next-generation enterprise applications. Oracle plans to not only broaden and accelerate its own investment in the Java platform, but also plans to increase the commitment to the community that helps make Java an ubiquitous, innovative platform unified around open standards.” Winter 2019 CMPE212 - Prof. McLeod
9
Slightly More Recent History, Cont.
Oracle seems to be sticking with their promise as demonstrated with the releases of Java 7 & 8. Now, we are using Java 11. The second “incarnation” of JavaFX has also been released, replacing Swing. Now, Oracle is promising a new release of Java every 6 months. Winter 2019 CMPE212 - Prof. McLeod
10
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 2019 CMPE212 - Prof. McLeod
11
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 JVM Applet Applet Byte code files Winter 2019 CMPE212 - Prof. McLeod
12
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. Java now has over 9 million application programmers worldwide. No other language has ever grown this quickly! Winter 2019 CMPE212 - Prof. McLeod
13
How Java Works, Cont. So, Java can be used either to create applets for use in web pages or for stand-alone applications. Most “Integrated Development Environments” or IDE’s will support the development of either kind of program. But applets are no longer in “vogue” due to security concerns. What has replaced applets in web pages? Winter 2019 CMPE212 - Prof. McLeod
14
How Java Works, Cont. All IDE’s, including Eclipse, must use the appropriate JDK in the background. Two components of the JDK are the programs “javac.exe” (or javaw.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 2019 CMPE212 - Prof. McLeod
15
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 2019 CMPE212 - Prof. McLeod
16
Eclipse Source Text Editor text successful Bytecode binary Compiler
Executable not successful binary Linker Errors javac.exe Loader Library binary Output Data Input Data java.exe Winter 2019 CMPE212 - Prof. McLeod
17
Questions: Suppose we have two kinds of errors:
Syntax errors Runtime errors What is the difference? Considering the two step process of running a Java program, at what stages will these errors be caught and who does the catching? Who does the fixing??? Winter 2019 CMPE212 - Prof. McLeod
18
OOP in Java A class or “object definition” or an “object”, consists 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 2019 CMPE212 - Prof. McLeod
19
OOP in Java – Cont. In Java, a class is an Object, and an Object is a class (rather Zen is it not!) And, you can also continue by saying that an instance of a class is also an Object. Code and attributes cannot be defined outside of a class. The only code that can exist outside a method are attribute declarations or other (“inner”) class definitions. Winter 2019 CMPE212 - Prof. McLeod
20
Aside – Class Instances
More on this topic and Objects, later. But, for now: static class members can be used directly, with creating a instance of a class. Otherwise a class cannot be used directly without first creating a copy or an “instance” of that class. The class definition serves as a “blueprint” and the new keyword is used to create an instance of that blueprint. Constructors can be used to initialize attributes at the time of instantiation. Winter 2019 CMPE212 - Prof. McLeod
21
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 2019 CMPE212 - Prof. McLeod
22
Aside – Access Modifiers
public means the attribute or method is available to any external class (as well as inside the class). private means that the attribute or method, the “member”, is only available inside the class in which it is declared. protected means the member is only public to classes in the same package as the class in which the member is declared. Inside a class the public/private/protected thing makes no difference. Winter 2019 CMPE212 - Prof. McLeod
23
Aside - Packages More on this topic later too!
A package is a folder inside the src folder. You associate a class with a package using the package keyword at the top of the class. Organizes code in large projects and makes it easier to import it. Winter 2019 CMPE212 - Prof. McLeod
24
static (First Pass) static means different things depending on where it is used. For now, consider: public static members are available outside the class without the need to instantiate the class. Any static member remains in memory until the program is complete. Since main is static, it can only invoke other static methods when they are in the same class. Winter 2019 CMPE212 - Prof. McLeod
25
Attribute Declaration
Syntax: [private|public] [static] [final] type attributeName [= literal]; Note that the type part is not optional – this is why java is a “declarative” language. And, a variable cannot change its type later, called “static typing”. You cannot use a variable unless you have declared it first. Winter 2019 CMPE212 - Prof. McLeod
26
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. A method’s parameter has the same scope as a variable declared locally to a method. Winter 2019 CMPE212 - Prof. McLeod
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.