Java_Chapter-1 (3)
Java_Chapter-1 (3)
Compiled by Ermiyas T.
Objective
• Recognize and identify how Java code get compiled and executed.
Overview of Programming Language
• You use word processors to write documents, Web browsers to explore the
Internet, and email programs to send email.
• The answer is that Java enables users to develop and deploy applications on the
Internet for servers, desktop computers, and small hand-held devices.
Types of Programming Paradigms
What is Programming Paradigms
• Procedural programming
• Functional programming
• Declarative programming
• Object-Oriented programming
• Event-Driven programming
Programming paradigms …
Imperative programming
• Say you want to bake a cake. Your imperative program to do this might
look like this: 1- Pour flour in a bowl
2- Pour a couple eggs in the same bowl
3- Pour some milk in the same bowl
4- Mix the ingredients
5- Pour the mix in a mold
6- Cook for 35 minutes
7- Let chill
Programming paradigms …
Imperative programming
• We're being detailed and specific in our instructions, and that's what
imperative programming stands for.
Programming paradigms …
• A pure function is one that relies only on its inputs to generate its result.
• Given the same input, it will always produce the same result. Besides, it
produces no side effects (any change outside the function's
environment).
function filterNums() {
const result = [] // Internal variable
• It's almost the same code, but we wrap our iteration within a function, in
which we also store the result array.
• In this way, we can assure the function doesn't modify anything outside
its scope. It only creates a variable to process its own information, and
once the execution is finished, the variable is gone too.
Programming paradigms …
Declarative Programming
• See that with the filter function, we're not explicitly telling the computer to
iterate over the array or store the values in a separate array.
Object-Oriented Programming
• The core concept of OOP is to separate concerns into entities which are
coded as objects. Each entity will group a given set of information
(properties) and actions (methods) that can be performed by the entity.
– The answer for this question is it depends on the problem you are
trying to solve.
Classe
• A class is a blueprint that defines the variables and the methods common
to all objects of a certain kind.
• The class declares the instance variables necessary to contain the state
of every object.
• After you’ve created the class, you can create any number of objects
from that class.
Object
• An object is a software bundle of variables and related methods.
• An object is also known as an instance. An instance refers to a particular
object.
• The variables of an object are formally known as instance variables
because they contain the state for a particular object or instance.
Class vs Object
• Encapsulation:
• It suggests that we should bundle the data and operations on the data
inside a single unit (class).
• Abstraction
• As a metaphor, think of the remote control of your TV. All the complexity
inside the remote control is hidden from you. It’s abstracted away. You
just work with a simple interface to control your TV. We want our objects
to be like our remote controls.
4 Pillars of Object-Oriented Principle
Inheritance:
Polymorphism
– Overloading methods
– Overriding methods, and
– Dynamic method binding
• Overloaded methods: methods with the same name signature but either
a different number of parameters or different types in the parameter list.
• It was originally called oak, after an oak tree that stood outside Gosling’s
office. Later, it was renamed to green, and was finally renamed to Java,
inspired by Java coffee.
• Used for developing software that run on mobile, desktop, and servers.
• Machin Independence.
Overview of Java Programming
Java API
Editions of Java
• Every Java program must have at least one class. Each class has a
name. By convention, class names start with an uppercase letter.
• The program is executed from the main method that is defined in one of
the classes. A class may contain several methods. The main method is
the entry point where the program begins execution.
Steps for running a Java program
4. Verify: during the loading process java will check for restrictions.
• Java source code is compiled into Java bytecode (.class file) and Java
bytecode is interpreted by the JVM. Your Java code may use the code in
the Java library. To execute a Java program is to run the program’s
bytecode.
Java Software Requirment
• Individual Assignment 1.
• Reserved words
• Comments
// This program prints Welcome to Java!
• Blocks public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Anatomy of Java Program
• Reserved words
• Comments
// This program prints Welcome to Java!
• Blocks public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Anatomy of Java Program
• Reserved words
• Comments
// This program prints Welcome to Java!
• Blocks public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Anatomy of Java Program
• Statement terminator • For example, when the compiler sees the word
class, it understands that the word after class is
• Reserved words
the name for the class.
• Comments
// This program prints Welcome to Java!
• Blocks public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Java Package
– package packageName;
• Any classes declared within that file will belong to the specified package.
• Java uses file system directories to store packages.
• For example, the .class files for any classes you declare to be part of
packageName must be stored in a directory called packageName.
• Remember that case-sensitive, and the directory name must match the
package name exactly.
• Packages are divided into two categories:
– Built-in Packages (packages from the Java API)
– User-defined Packages (create your own packages)
Cont…
Built-in Packages
• The Java API is a library of prewritten classes that are free to use,
included in the Java Development Environment (JDE).
– Either import a single class (along with its methods and attributes), or
– The whole package that contain all the classes that belong to the
specified package.
Built-in Packages …
• You can include as many import statements as are necessary to import
all the classes used by your program.
• This is the general form of the import statement:
– import pkg1.pkg2.Classname; // Import a single class
– import pkg1.pkg2.*; // Import the whole package
• Here, pkg1 is the name of a top-level package, and pkg2 is the name of a
subordinate package inside the outer package separated by a dot (.).
• Example:
– import java.util.Scanner;
– import java.io.*;
Cont …
User-defined Packages
• To create your own package, you need to understand that Java uses a
file system directory to store them.
• Example: MyPackageClass.java
package mypack;
" " Opening and closing Enclosing a string (i.e., sequence of characters).
quotation marks
; Semicolon Marks the end of a statement.
Programming Style and Documentation
• Appropriate Comments
• Naming Conventions
• Block Styles
• Programming Errors
– Syntax Errors
• Detected by the compiler
– Runtime Errors
• Causes the program to abort
– Logic Errors
• Produces incorrect result
Java Application and Applet/Servlet