Chapter 1 (Part 2) - Introduction to Object-Oriented Programming
BIC20904 - Object Oriented Programming
Content
• Introduction to Java• The Java Language Specification • Java Development Tools• Create, compile and run Java programs• Java Program Development Process• Java Portability• Anatomy of Java Programs
Learning Outcomes
• At the end of this chapter, you should be able to:• Understand the Java programming language.• Know how to create, compile and run Java programs.• Understand the anatomy of Java programs.
Introduction to Java
• Java is an object-oriented programming (OOP) language that is built aroundthe concept of objects.• Java was created by Sun Microsystems team led by Patrick Naughton and
James Gosling (1991). Sun Microsystems was purchased by Oracle in 2010.• Java was originally named as Oak (Gosling liked the look of an oak tree that
was right outside his window at Sun).• Intended to design a small computer language that could be used for
consumer devices/appliances such as the TVs, refrigerators, etc.• It is a difficult task because these devices do not have a lot of power or memory, thelanguage had to be small and generate very tight code. Also, because differentmanufacturers may choose different central processing units (CPUs), it was importantthat the language not be tied to any single architecture.
Introduction to Java (cont.)
• The team developed a two-step translation process to simplify the taskof compiler writing for each class of appliances/machines.• Significance of Java translation process.• Writing a compiler (translation program) for each type of appliances (computers,
mobile phones, electrical machines, etc) would have been very costly.• Instead, they developed intermediate language that is the same for all types of
processors which is called Java bytecode. Java bytecode is not the machinelanguage for any particular computer, it is a program code that has been compiledfrom source code into low-level code which is then to be run with Java VirtualMachine (JVM).
• JVM then translate the bytecode into the machine language for any particularcomputer.
Introduction to Java (cont.)
Introduction to Java (cont.)
• Java is a full-featured, general-purpose programming language that iscapable of developing robust mission-critical applications.• Today it is used for developing stand alone applications, desktops, web
programming, servers, and mobile devices.• Java can be run on the web browser called the applets.• Java can also be used to develop applications on the server side, called Javaservlets or Javaserver pages (JSP).
• Java can be used to develop applications for small hand-held devices, such aspersonal digital assistants and cell phones.
Introduction to Java (cont.)
Java Applet
Java Server Pages
Java for Mobile Application
The Java Language Specification
• Java language specification defines the Java standard and the technicaldefinition of the language that includes the syntax and semantics of the Javaprogramming language.• Java application program interface (API) contains predefined classes and
interfaces for developing Java programs.• There are 3 editions of the Java API:
• Java Standard Edition (Java SE) to develop client-side applications. The applicationscan run standalone or as applets running from a Web browser.
• Java Enterprise Edition (Java EE) to develop server-side applications, such asJavaservlets, JavaServer Pages (JSP), and JavaServer Faces (JSF).
• Java Micro Edition (Java ME) to develop applications for mobile devices, such as cellphones.
The Java Language Specification (cont.)
• In this course, we will use Java SE as an introduction to Javaprogramming.• Currently (October 2021), Java SE 17 is the latest release of Java SE.
Java Development Tools
• Java development tool is a software that provides an integrateddevelopment environment (IDE) for rapidly developing Java programs.• The most popular IDEs for Java are:• Eclipse• Netbeans• IntelliJ IDEA• BlueJ
Create Java Programs
• Java programs are standalone programs that can be executed from any computer witha JVM. Java programs can also be referred as Java source code.• You can use any text editor or IDE to create and edit a Java program.• A Java program should have a .java file extension.• A Java program contains a class with a method named main
• When a Java application program is run, the run-time system automatically invokes the methodnamed main
• All Java application programs start with the main method.• A Java program should be named after the class name. For example, if a Java
program consist a class named Car, therefore the Java program should be saved asCar.java.• However, a Java program can also consist of more than one class. In this case, the
Java program should be named after the class name that has the main methoddeclared inside it.
Create Java Programs (cont.)
HelloApp.java
Create Java Programs (cont.)Computer.java
main method in Computer
Create Java Programs (cont.)
• You can create a Java source file using Windows Notepad.
Compile Java Programs
• A compiler is a program that translates source code into an executable form.• A compiler is run using a Java programs as input.• Syntax errors that may be in the program will be discovered during compilation.• Syntax errors are mistakes that the programmer has made that violate the rules of theprogramming language.
Syntax errors
Compile Java Programs (cont.)
• The compilers for most programming languages translate high-levelprograms directly into the machine language for a particular computer.• Since different computers have different machine languages/architectures, a
different compiler is needed for each one.
• In contrast, the Java compiler translates Java programs into bytecode, amachine language for a fictitious computer called the Java VirtualMachine.• Once compiled to bytecode, a Java program can be used on any computer (with
JVM installed), making it very portable.• Bytecode files end with the .class file extension.
Compile Java Programs (cont.)
• The following command compiles Welcome.java: javac Welcome.java
• If there aren’t any syntax errors, the compiler generates a bytecode file with a .class extension. Thus, the preceding command generates a file named Welcome.class• The bytecode can run on any platform that has a Java Virtual Machine
(JVM).
Run Java Programs
• JVM (Java Virtual Machine) is an abstract machine that enables yourcomputer to run a Java program.• When you run the Java program, Java compiler first compiles your Java
code to bytecode. Then, the JVM translates bytecode into native machinecode (set of instructions that a computer's CPU executes directly).• Java is a platform-independent language. It's because when you write Java
code, it's ultimately written for JVM but not your physical machine(computer). Since JVM executes the Java bytecode which is platform-independent, Java is platform-independent.
Run Java Programs (cont.)
Java Program Development Process
Java Portability
• Portable means that a program may be written on one type of computerand then run on a wide variety of computers, with little or nomodification.• Java bytecode runs on the JVM and not on any particular CPU;
therefore, compiled Java programs are highly portable.• JVMs exist on many platforms:• Windows • Macintosh• Linux• BSD• Etc.
Java Portability (cont.)
• With most programming languages, portability is achieved by compiling a program for each CPU it will run on. • Java provides an JVM for each platform so that programmers do not
have to recompile for different platforms.
Anatomy of Java Programs
• Comments• Package• Reserved words• Modifiers• Statements• Blocks• Classes• Methods• The main method
Anatomy of Java Programs - Comments
• Help the programmers to communicate and understand the program. • Not a programming statement, thus ignored by the compiler.• Comments can be written in single or multiple lines.
Anatomy of Java Programs - Package
• A package in Java is used to group related classes. Think of it as a folderin a file directory. We use packages to avoid name conflicts, and towrite a better maintainable code. Packages are divided into twocategories:• Built-in Packages (packages from the Java API)• User-defined Packages (create your own packages)
• Built-in Packages• The Java API is a library of prewritten classes, that are free to use.• The library is divided into packages and classes. Meaning that we can either
import a single class (along with its methods and attributes), or a whole packagethat contain all the classes that belong to the specified package.
Anatomy of Java Programs – Package (cont.)
• To use a class or a package from the library, we need to usethe import keyword.
• If we find a class we want to use, for example,the Scanner class, which is used to get user input, write thefollowing code:
• In the example above, java.util is a package, while Scanner is aclass of the java.util package.
Anatomy of Java Programs – Package (cont.)
• User-defined Packages• To create our own package, we need to understand that Java uses a file system
directory to store them. Just like folders on our computer:
• To create a package, use the package keyword:The package statement must be the first line in the source code file, before any import statements that may be present.
Anatomy of Java Programs – Reserved words
• Java has a set of keywords that are reserved words that cannot be used as variables, methods, classes, or any other identifiers:
Anatomy of Java Programs – Modifiers
• Java uses certain reserved words called modifiers that specify the properties of the variables, methods,and classes and how they can be used.
• In Java, there can be 4 access modifiers that can be used:• public• default• private• protected
• You will learn this further in Chapter 4.
Anatomy of Java Programs – Statements
• A statement represents an action or a sequence of actions.• Every statement in Java ends with a semicolon (;).• For example, the statement System.out.println("Welcometo Java!"); in a Java program is a statement to display the greeting"Welcome to Java!"
Anatomy of Java Programs – Blocks
• A pair of braces (or brackets) in a program forms a block that groups componentsof a program.• In Java, each block begins with an opening brace ({) and ends with a closing brace (}).
Every class has a class block that groups the data and methods of the class.Similarly, every method has a method block that groups the statements in themethod. Blocks can be nested, meaning that one block can be placed within another,as shown in the following code.
Anatomy of Java Programs – Blocks
• There are two popular styles, next-line style and end-of-line style, as shown below.
Anatomy of Java Programs – Classes
• The class is the essential Java construct.• A class is a template or blueprint for objects. To program in Java, you
must understand classes and be able to write and use them.• A program is defined by using one or more classes.• Class names should be nouns, in mixed case with the first letter of each
internal word capitalized. For example class Car, class MyHome.• Try to keep your class names simple and descriptive. Use whole words-
avoid acronyms and abbreviations (unless the abbreviation is much morewidely used than the long form, such as URL or HTML).
Anatomy of Java Programs – Methods
• Methods are used to perform certain actions, and they are also known as functions.
• Amethod is a block of code which only runs when it is called.• We can pass data, known as parameters, into a method.
• Methods support code reusability which we define the code once, and use it many times.
• Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. For example, run(), runFast(), getBackground().
Anatomy of Java Programs – The main method
• The main method provides the control of program flow. The Javainterpreter executes the application by invoking the main method.• The main method looks like this:
Thank You