Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

java_chapter_one

Chapter One introduces the basics of computers, programming, and the Java programming language. It covers key concepts such as hardware, software, programming languages, and different programming paradigms, including imperative, declarative, functional, and object-oriented programming. Additionally, it discusses Java's features, its development environment, and provides a simple example of a Java program.

Uploaded by

mulukengashaw21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

java_chapter_one

Chapter One introduces the basics of computers, programming, and the Java programming language. It covers key concepts such as hardware, software, programming languages, and different programming paradigms, including imperative, declarative, functional, and object-oriented programming. Additionally, it discusses Java's features, its development environment, and provides a simple example of a Java program.

Uploaded by

mulukengashaw21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Chapter One(part 1)

Introduction to Computers,
Programs, and Java
by : Addisu A.
Objectives
• To review computer basics and programs
• To understand what programming paradigm is?
• To distinguish the terms API, IDE, and JDK.
• To write a simple Java program .
• To display output on the console .
• To explain the basic syntax of a Java program
• To create, compile, and run Java programs .
What is Computer ?
• is an electronic device designed to process data, perform
calculations, and execute a set of instructions (programs) to
accomplish various tasks.
• Here are the key components and functions of a computer:
1) Hardware:
• Central Processing Unit (CPU): The brain of the computer that performs
calculations and executes instructions.
• Memory (RAM): Temporary storage that holds data and instructions
that the CPU is currently processing.
• Storage: Devices like hard drives (HDDs) or solid-state drives (SSDs) that
store data permanently.
What else?
2)Software:
• Operating System: The system software that manages hardware and
software resources (e.g., Windows, macOS, Linux).
• Applications: Programs that perform specific tasks (e.g., word
processors, web browsers, games).
What is Computer Program?
• Computer programs, known as software, are instructions to the
computer.
• You tell a computer what to do through programs.
• Without programs, a computer is an empty machine.
• Computers do not understand human languages, so you need to use
computer languages to communicate with them.
• Programs are written using programming languages.
• Computer programs are instructions that tell a computer what to do.
• What is software , program and code ?
What is Programming Languages?
• Programming languages are languages that programmers use to write
code/program to instruct a computer.
• Computers do not understand human languages, so programs must be
written in a language a computer can use.
• There are hundreds of programming languages, and they were
developed to make the programming process easier for people.
• Programming languages are classified into three categories. Machine,
Assembly and High- Level Language
Cont...
Machine Language
• Machine language is a set of primitive instructions built into every
computer.
• The instructions are in the form of binary code, so you have to enter
binary codes for various instructions.
• Program with native machine language is a tedious process.
• Moreover the programs are highly difficult to read and modify.
• For example, you might write an instruction in binary like this:
1101101010011010
Cont...
Assembly Language
• Programming in machine language is a tedious process.
• Moreover, programs written in machine language are very
difficult to read and modify.
• For this reason, assembly language was created in the early
days of computing as an alternative to machine languages.
• Assembly language uses a short descriptive word, known as
a mnemonic, to represent each of the machine-language
instructions.
• For example, the mnemonic ADD typically means to add
numbers and SUB means to subtract numbers.
Cont...
Assembly Language
• Assembly languages were developed to make programming
easy.
• Since the computer cannot understand assembly language,
however, a program called assembler is used to convert
assembly language programs into machine code, as shown
in the following Figure.
• For example, to add two numbers, you might write an
instruction in assembly code like this:
ADD 2, 3, result
Cont...
Assembly Language
• Writing code in assembly language is easier than in machine
language.
• However, it is still tedious to write code in assembly
language.
• Writing in assembly requires that you know how the CPU
works.
• Assembly language is referred to as a low-level language,
because assembly language is close in nature to machine
language and is machine dependent.
Cont...
High-Level Language
• Platform independent, which means that you can write a
program in a high level language and run it in different
types of machines.
• The high-level languages are English-like and easy to learn
and program.
• The instructions in a high-level programming language are
called statements.
• For example, the following is a high-level language
statement that computes the
area of a circle with radius 5:
area = 5* 5 * 3.1415;
Cont...
High-Level Language
• There are many high-level programming languages, and
each was designed for a specific purpose.
• Some popular ones are C, C++, C#, VIB and Java.
• But for this course Java is our focus.
• A program written in a high-level language is called a source
program or source code.
• Because a computer cannot understand a source program,
a source program , a source program must be translated
into machine code for
Cont...
High-Level Language
• The translation can be done using another programming
tool called an interpreter or a compiler
• An interpreter reads one statement from the source code,
translates it to the machine code or virtual machine code,
and then executes it right away, as shown in the following.
Cont...
High-Level Language
• A compiler translates the entire source code into a machine-code file,
and the machine-code file is then executed, as shown in figure below.

• The difference between compiling and interpreting is that compiling


translates the high-level code into a target language code as a single
unit and
• Interpreting translates the individual steps in a high-level program
one at a time rather than the whole program as a single unit. Each
step is executed immediately after it is translated.
-List some examples of interpreted and compiled languages ?
What is Programming Paradigm ?
• Solving a programming problem requires choosing the right concepts.
• All but the smallest toy problems require different sets of concepts for
different parts.
• This is why programming languages should support many paradigms.
• A paradigm is a way of doing something .
• A programming paradigm a fundamental style or approach to
programming that defines how developers structure and organize
code, as well as how they think about and solve problems.
Different paradigms provide various ways to conceptualize programming tasks, and
• each has its own principles, methodologies, and best practices.
Cont...
• Features of various programming languages determine
which programming paradigms they belong to.
• Some programming languages fall into only one paradigm,
while others fall into multiple paradigms.
• Each paradigm supports a set of concepts that makes it the
best for a certain kind of problem.
• For example, object-oriented programming is best for
problems with a large number of related data abstractions
organized in hierarchy.
Some Major Types of Programming
1. Imperative or procedural
• It is based on commands that update variables in storage.
• Focuses on explicitly stating the steps that a program must
take to achieve a goal.
• Control flow is an explicit sequence of commands.
• Commands show how the computation takes place, step by
step.
• Each step affects the global state of the computation.
• Some programming languages used in this paradigm are
Fortran,Pascal, Basic, C etc
Cont...
2. Declarative
• Control flow in declarative programming is implicit:
programmer states only what the result should look like, not
how to obtain it.
• Programs state the result you want, not how to get it.
e.g SQL
3. Logical or Rule based
• It is a set of sentences in logical form, expressing facts and rules
about some problem domain.
• Based on formal logic, where programs consist of a set of facts
and rules.
• Major logic programming language families include PROLOG.
Cont...
4. Functional
• In this paradigm we express computations as the
evaluation of mathematical functions.
• The reason is that the paradigm originates from a
purely mathematical discipline: the theory of functions.
• In functional programming control flow is expressed by
combining function calls, rather than by assigning
values to variables.
e.g Haskell, Lisp, Scala.
Cont...
5.Object-Oriented
• The object-oriented paradigm has got great popularity in
the recent years.
• Object-Oriented Programming is a programming
paradigm based on the concept of “Objects”, which may
contain data, in the form of fields, often known as
Attributes; and code, in the form of Methods.
• These properties are very important when programs
become larger and larger.
• Java, C++, C#, python are some examples of object-oriented
Java
What is java?
• Is a popular high-level, object-oriented programming language
• Was developed by a team led by James Gosling at Sun Microsystems.
• Sun Microsystems was purchased by Oracle in 2010.
• Originally called Oak.
• was designed in 1991 for use in embedded chips in consumer
electronic appliances.
• In 1995, renamed Java, it was redesigned for developing Web
applications.
• Currently, Java is owned by Oracle, and more than 3 billion devices run
Java.
• Java runs on a variety of platforms, such as Windows, Mac OS, and the
various versions of UNIX.
When was Java officially released?
Cont...
• Java is a Powerful and versatile programming Language for
developing software running on mobile devices, desktop computers,
and servers.
• Java enables users to develop and deploy applications on
the Internet for servers, desktop computers, and small
hand-held devices.
• The future of computing is being profoundly influenced by
the Internet, and Java promises to remain a big part of that
future.
• Hence, Java is the Internet programming language.
Some Uses of Java
• Applications
• Java applets (running from a Web browser).
• Server-side applications
• Multimedia application
• Etc...
Key Features of Java Technology
1.Simple
• No language is simple, but Java is a bit easier than the
popular object-oriented programming language C++, which
was the dominant software-development language before
Java. Java is partially modeled on C++, but greatly simplified
and improved
• Java is designed to be easy to learn. If you understand the
basic concept of OOP, Java would be easy to master.
Cont...
2. Object-Oriented
• Java is inherently object-oriented.
• Although many object-oriented languages began strictly as
procedural languages, Java was designed from the start to
be object-oriented
• Object-oriented programming (OOP) is a popular
programming approach that is replacing traditional
procedural programming techniques.
• One of the central issues in software development is how to
reuse code. Object-oriented programming provides great
flexibility, modularity, clarity, and re-usability through
encapsulation and inheritance.
Cont...
4.Distributed
• Distributed computing involves several computers working
together on a network. Java is designed to make
distributed computing easy.
• Since networking capability is inherently integrated into
Java, writing network programs is like sending and receiving
data to and from a file.
Cont...
5.Robust
• Robust means reliable.
• Java compilers can detect many problems that would first
show up at execution time in other languages.
• Java has eliminated certain types of error-prone
programming constructs found in other languages.
• Java has a runtime exception- handling feature to provide
programming support for robustness.
6.Secure
• Java implements Several security mechanisms to protect
your system against harm caused by stray programs.
Cont...
7. Architecture-Neutral
• refers to the idea that Java bytecode does not depend on the
underlying hardware architecture.
• Architectural-Neutral means platform-independent.
• With a Java Virtual Machine (JVM), you can write one
program that will run on any platform
• Write once, run anywhere.
Cont...
8.Portable
• Portability refers to the ability of a program to run on different
operating systems and hardware without requiring modification.
• In Java, this is achieved through the use of the Java Virtual Machine
(JVM) and the bytecode format.
• Because Java is architecture neutral, Java programs are portable.
• They can be run on any platform without being recompiled.
9.Performance
• Java’s performance sometimes criticized.
• Because Java is architecture neutral, Java programs are portable. They
can be run on any platform without being recompiled.
• Java developers developed the java Java Hotspot Performance Engine ,
which includes a compiler for optimizing the frequently used code.
• The Hotspot Performance Engine can be plugged into a JVM to
dramatically boost its performance
Cont...
10.Multithreaded
• Multithreading is a program’s capability to perform several tasks
simultaneously.
• Multithread programming Is smoothly integrated in Java, whereas in
other languages you have to call procedures specific to the operating
system enable multithreading.
11.Dynamic
• Java was designed to adapt to an evolving environment.
• New code can be loaded on the fly without recompilation. There is no
need for developers to create, and for users to install, major new
software versions.
• New features can be incorporated needed.
The Java Language Specification, API, JDK
and IDE
• Computer languages have strict rules of usage. If you do not
follow the rules when writing a program, the computer will
be unable to understand it.
• Sun Microsystems, the originator of Java, intends to retain
control the language from losing its unified standards:
The Java language specification and Java API define the
Java standard.
1. Java Language Specification (JLS)
• The JLS is a formal document that describes the syntax and
semantics of the Java programming language.
• It serves as the definitive reference for the language.
• It outlines how Java programs should behave, including
rules for data types, control structures, object-oriented
features, and more.
• Ensures consistency and correctness across different
implementations of the Java language.
2. Java API (Application Programming Interface)
• The Java API is a collection of pre-written classes and
interfaces that provide standard functionality for Java
applications.
• Components:
• Core Libraries: Includes fundamental classes like java.lang,
java.util, and java.io.
• Frameworks: Libraries for specific tasks, such as Swing for GUI
applications, JavaFX for rich client applications, and JDBC for
database connectivity.
• Developers use the API to leverage existing functionality,
speeding up development and reducing errors.
• What is the package , library and framework ?
3. Java Development Kit (JDK)
• JDK is a software development environment that provides the tools needed to
develop Java applications.
• is essential for developing Java applications, containing the necessary tools and
libraries.
• The JDK is a superset of the JRE, and contains everything that is in the JRE, plus
tools such as the compilers and debuggers necessary for developing applets
and applications.
-Java Platform = JDK (because JRE is subset of JDK )
-JRE = JVM + Java Packages of Classes(like util, math, lang, awt, swing etc) +
runtime libraries.
• Components:
• Compiler (javac): Converts Java source code into bytecode.
• Java Runtime Environment (JRE): Allows running Java applications.
• Development Tools: Includes tools for debugging, documentation
generation, and packaging applications.
• NB: If you have the JDK installed, you don’t need to install the JRE separately to
run any Java software
• Why do we need JVM?
4. Integrated Development Environments (IDEs)
• IDEs are software applications(tools) that provide
comprehensive facilities to programmers for software
development.
• Enhance productivity by providing a comprehensive set of
tools for writing, testing, and debugging Java code.
• Editing, compiling, debugging, and online help are integrated in
one graphical user interface.
• Popular Java IDEs:
• Eclipse: A widely used open-source IDE with extensive plugins.
• IntelliJ : A feature-rich IDE known for its intelligent code
assistance.
• NetBeans: An open-source IDE that provides a user-friendly
interface and good support for Java.
JDK Editions
There are three editions of the Java JDK:
Java is a full-fledged and powerful language that can be used in
many ways.
• Java 2 Standard Edition (J2SE) can be used to develop client-side
standalone applications or applets.
• Java 2 Enterprise Edition (J2EE) can be used to develop server-
side applications, such as Java servlets and Java Server Pages.
• Java 2 Micro Edition (J2ME)to develop applications for mobile
devices, such as cell phones.
Simple Java Program
A Java program is executed from the main method in the class.
1 public class Welcome {
2 public static void main(String[] args) {
3 //This program display Welcome to Java!
4 System.out.println("Welcome to Java!");
5 }
6 }
Line 1:
class is a named template or descriptor for a set of objects that share
the same attributes(data Field), behaviors(methods), and relationships.
Every Java program must have at least one class.
A class is a construct that defines data and methods.
Each class has a name and By convention, class names start with an
uppercase letter
Cont...
Line 2
 defines the main method. The program is executed from the main method.
 A class may contain several methods. The main method is the entry point
where the program begins execution.
 A method is a construct that contains statements. The main method in this
program contains the System.out.println statement. This statement
displays the string Welcome to Java! on the console (line 4).
 String is a programming term meaning a sequence of characters.
 A string must be enclosed in double quotation marks.
 Every statement in Java ends with a semicolon (;), known as the statement
terminator.
 Reserved words, or keywords, have a specific meaning to the compiler and
cannot be used for other purposes in the program. For example, when the
compiler sees the word class, it understands that the word after class is the
name for the class.
 Other reserved words in this program are public, static, and void.
Cont...
Line 3
 is a comment that documents what the program is and
how it is constructed.
 Comments help programmers to communicate and
understand the program.
 They are not programming statements and thus are
ignored by the compiler.
 In Java, comments are preceded by two slashes (//) on a
line, called a line comment, or enclosed between /* and */
on one or several lines, called a block comment or
paragraph comment. When the compiler sees //, it ignores
all text after // on the same line. When it sees /*, it scans
for the next */ and ignores any text between /* and */.
Creating, Compiling, and Executing a Java Program
• save a Java program in a .java file and compile it into a .class file.
The .class file is executed by the Java Virtual Machine.
• The Java programming-development process consists of
creating/ modifying source code, compiling and executing
programs.
• Like any other programming language, Java has its own syntax,
and you need to write code that obeys the syntax rules.
• The Java compiler will report syntax errors if your program
violates the syntax rules. If there are no syntax errors, the
compiler generates a bytecode file with a .class
• The bytecode is similar to machine instructions but it is
architecture-neutral and can run on any platform that has a JVM.
• This is one of Java's primary advantages: Java bytecode can run
on a variety of hardware platforms
Creating, Compiling, and Executing a Java Program
• A Java compiler translates a Java source file into a Java
bytecode file.
• The following command compiles Welcome.java file
javac Welcome.java
• And then to run the program type the following command
java Welcome
• Finally, the terminal will display the output of the program
Welcome to Java
Cont...
• The following figure describes the process of creating, compiling and
executing program.

Is Java compiled or interpreted language?


End of part one!
Question?

You might also like