0% found this document useful (0 votes)
64 views51 pages

Object Oriented Programming-Java

The document discusses an introduction to object oriented programming in Java. It covers the history of Java, object oriented concepts, features of Java like being platform independent and robust, and provides a simple example of a first Java program including how to compile and run it.

Uploaded by

Tooba Akhtar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
64 views51 pages

Object Oriented Programming-Java

The document discusses an introduction to object oriented programming in Java. It covers the history of Java, object oriented concepts, features of Java like being platform independent and robust, and provides a simple example of a first Java program including how to compile and run it.

Uploaded by

Tooba Akhtar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 51

Course Title

OBJECT ORIENTED PROGRAMMING

PRESENTED BY: ZAHID HUSSAIN KHASKHELI


DISCLAIMER

• The material used in this presentation i.e., pictures/graphs/text, etc. is solely


intended for educational/teaching purpose, offered free of cost to the students for
use under special circumstances of Online Education due to COVID-19 Lockdown
situation and may include copyrighted material - the use of which may not have
been specifically authorized by Copyright Owners. It’s application constitutes Fair
Use of any such copyrighted material as provided in globally accepted law of many
countries. The contents of presentations are intended only for the attendees of the
class being conducted by the presenter.
INTRODUCTION TO JAVA

• History of java
• Object oriented concepts in Java
• Java Features
• Understanding first Java Program
• To compile and run Java program
• Java installation guide
HISTORY OF JAVA

• Java is an object oriented language developed by a team lead by james gosling in 1991 at
Sun Microsystems- called green team.
• Initially it was called as GreenTalk and its file extension was .gt
• Later on It was started as project named as Oak, but later on changed to Java.
• Oak is a green tree which stood outside james gosling’s office- treated as symbol of
strength and used a national tree by many countries. Like Romania, Germany, France etc.
CONTINUE..

• In 1995 when team decided to register the name Oak, they decided to change the name
because it was already a registered trademark by Oak technologies.
• So the new name Java was suggested by the team among several other names.
• Java is an island of the Indonesia where first coffee was produced.
• Language creators were also keen consumers of coffee
• Java is not an acronym, it is just a name.
• Goal of the green project was to examine upcoming consumer electronics market.
• Purpose was to develop a programming language which will be platform neutral
• Platform ?
• Environment in which program runs- e.g. Operating System- Window OS, Linux etc.
• Provide a platform for executing software on our computer

• Any software capable executing on various platforms is known as platform independent


software.
• This was thee key intention behind the development of Java.
• In January 2010 Sun Microsystems was acquired by oracle corporation.
• That’s why we call Java as product of Oracle.
FLAVORS IN JAVA

• Java is categorized into three flavors:


• 1. Java Standard Edition- simple general-purpose desktop applications
• 2. Java Enterprise Edition- for business purpose ( Desktop base and web base appliations)
• 3. Java Micro Edition-for embedded systems( e.g. Mobile phones, PDAs etc )
JAVA VERSION HISTORY

• Java 1.0 to 1.4 (1996- 2002)


• Java 5 to 14 ( 2004-2020)
JAVA APPLETS AND APPLICATIONS

• Java can be used to create two types of programs: applications and applets.
• An application is a program that runs on your computer, under the operating system of that computer.
• That is an application created by Java is more or less like one created using C or C++.
• An applet is an application designed to be transmitted over the Internet and executed by
• a Java-compatible Web browser. An applet is actually a tiny Java program, dynamically
• downloaded across the network, just like an image, sound file, or video clip.
• The important difference is that an applet is an intelligent program, not just an animation or media file.
• In other words, an applet is a program that can react to user input and dynamically change—not just run the same
animation or sound over and over
JAVA FEATURES-SIMPLE

• Java was designed to be easy for the professional programmer to learn and use
• effectively. Assuming that you have some programming experience, you will not find
• Java hard to master. If you already understand the basic concepts of object-oriented
• programming, learning Java will be even easier. Best of all, if you are an experienced
• C++ programmer, moving to Java will require very little effort. Because Java inherits
• the C/C++ syntax and many of the object-oriented features of C++, most programmers
• have little trouble learning Java.
OBJECT-ORIENTED
JAVA FEATURES-OBJECT-ORIENTED

• Object Oriented programming is a programming style which is associated with the


concepts like:
•  class, object, Inheritance, Encapsulation, Abstraction, Polymorphism.
• The main aim of OOP is to bind together the data and the functions that operate on them
so that no other part of the code can access this data except that function.
ROBUST

• Java frees you from having to worry about many of the most common causes of
programming errors. Because Java is a strictly typed language, it checks your code at
compile time. However, it also checks your code at run time. In fact, many hard-to-track-
down bugs that often turn up in hard-to-reproduce run-time situations are simply
impossible to create in Java.
MULTITHREADED

• Java was designed to meet the real-world requirement of creating interactive,


• networked programs. To accomplish this, Java supports multithreaded programming,
• which allows you to write programs that do many things simultaneously.
ARCHITECTURE NEUTRAL

• A central issue for the Java designers was that of code longevity and portability. One
• of the main problems facing programmers is that no guarantee exists that if you write
• a program today, it will run tomorrow—even on the same machine. Operating system
• upgrades, processor upgrades, and changes in core system resources can all combine
• to make a program malfunction. The Java designers made several hard decisions in the
• Java language and the Java Virtual Machine in an attempt to alter this situation. Their
• goal was “write once; run anywhere, any time, forever.”
A FIRST SIMPLE PROGRAM
JAVA PROGRAM COMPILATION AND EXECUTION
ENTERING THE PROGRAM

• The first thing that you must learn about Java is that the name you give to a source file is
very important. For this example, the name of the source file should be Example.java
• a source file is officially called a compilation unit-a text file that contains one or more class
definitions. The Java compiler requires that a source file use the .java filename extension.
• As you can see by looking at the program, the name of the class defined by the program is
also Example.
• This is not a coincidence. In Java, all code must reside inside a class. By convention, the
name of that class should match the name of the file that holds the program.
COMPILING THE PROGRAM

• To compile the Example program, execute the compiler, javac, specifying the name of
• the source file on the command line, as shown here: C:\>javac Example.java
• The javac compiler creates a file called Example.class that contains the bytecode version
of the program. the Java bytecode is the intermediate representation of your program that
contains instructions the Java interpreter will execute. Thus, the output of javac is not
code that can be directly executed. To actually run the program, you must use the Java
interpreter, called java. To do so, pass the class name Example as a command-line
argument, as shown here: C:\>java Example
A CLOSER LOOK AT THE FIRST SAMPLE
PROGRAM
• Although Example.java is quite short, it includes several key features which are common to
all Java programs. The program begins with the following lines:
• /*This is a simple Java program.
• Call this file "Example.java“ */
• This is a comment. Like most other programming languages, Java lets you enter a remark
into a program’s source file. The contents of a comment are ignored by the compiler.
• Instead, a comment describes or explains the operation of the program to anyone who is
reading its source code.
• The next line of code in the program is shown here:
• class Example {
• This line uses the keyword class to declare that a new class is being defined. Example
• is an identifier that is the name of the class. The entire class definition, including all of
• its members, will be between the opening curly brace ({) and the closing curly brace (}).
• The next line in the program is the single-line comment, shown here:
• // Your program begins with a call to main().
• The next line of code is shown here:
• public static void main(String args[]) {
• This line begins the main( ) method. As the comment preceding it suggests, this is the
• line at which the program will begin executing. All Java applications begin execution by
calling main( ).
• The public keyword is an access specifier, which allows the programmer to control
• the visibility of class members. When a class member is preceded by public, then that
member may be accessed by code outside the class in which it is declared.
• The opposite of public is private, which prevents a member from being used by code
defined outside of its class.
• In this case, main( ) must be declared as public, since it must be called by code outside
of its class when the program is started.
• The keyword static allows main( ) to be called without having to instantiate a particular
instance of the class. This is necessary since main( ) is called by the Java interpreter
before any objects are made.
• The keyword void simply tells the compiler that main( ) does not return a value.
• As you will see, methods may also return values.
• As stated, main( ) is the method called when a Java application begins. Keep in mind that
Java is case-sensitive. Thus, Main is different from main.
• Any information that you need to pass to a method is received by variables specified
within the set of parentheses that follow the name of the method. These variables are
called parameters
• In main( ), there is only one parameter, albeit a complicated one. String args[ ] declares a
parameter named args, which is an array of instances of the class String.
• The last character on the line is the {. This signals the start of main( )’s body. All of the
code that comprises a method will occur between the method’s opening curly brace and
its closing curly brace.
• The next line of code is shown here. Notice that it occurs inside main( ).
• System.out.println("This is a simple Java program."); This line outputs the string “This is
a simple Java program.” followed by a new line on the screen. Output is actually
accomplished by the built-in println( ) method. In this case, println( ) displays the string
which is passed to it. As you will see, println( ) can be used to display other types of
information, too.
• The line begins with System.out. System is a predefined class that provides access to the
system, and out is the output stream that is connected to the console.
LEXICAL ISSUES

• Java programs are a collection of whitespace,identifiers, comments, literals, operators,


separators, and keywords.
• Whitespace: Java is a free-form language- no especial indentation rules.
• In Java, whitespace is a space, tab, or newline.
• Identifiers: An identifier may be any descriptive sequence of uppercase and lowercase
letters, numbers, or the underscore and dollar-sign characters. They must not begin with a
number.
• Again, Java is case-sensitive, so VALUE is a different identifier than Value.
IDENTIFIERS - EXAMPLES

• Valid Examples:
• AvgTemp
• count
• a4
• $test
• this_is_ok
• Invalid variable names include: 2count
• high-temp
• Not/ok
• Literals
• A constant value in Java is created by using a literal representation of it. For example,
• here are some literals:
• 100 98.6 ‘X’ “This is a test”
COMMENTS

• As mentioned, there are three types of comments defined by Java. You have already seen
two: single-line and multiline. The third type is called a documentation comment.
• This type of comment is used to produce an HTML file that documents your program.
• The documentation comment begins with a /** and ends with a */.
• Example : /**  This is  
documentation 
comment 
*/  
DATA TYPES

• Java is strictly typed language :


• Every variable has a type, every expression has a type, and every type is strictly defined.
• Second, all assignments, whether explicit or via parameter passing in method calls, are checked
for type compatibility. There are no automatic conversions of conflicting types as in some
languages. The Java compiler checks all expressions and parameters to ensure that the types are
compatible. Any type mismatches are errors that must be corrected before the compiler will finish
compiling the class.
DATA TYPES-THE SIMPLE TYPES

• Java defines eight simple (or elemental) types of data: byte, short, int, long, char, float, double,
and boolean.
• Integers: This group includes byte, short, int, and long, which are for whole valued.
• Floating-point numbers: This group includes float and double, which represent numbers with
fractional precision.
• Characters :This group includes char, which represents symbols in a character set, like letters
and numbers.
• Boolean: This group includes boolean, which is a special type for representing
• true/false values.
DATA TYPES-THE SIMPLE TYPES

• Integers: Java defines four integer types: byte, short, int, and long. All of these are
signed, positive and negative values.
• Java does not support unsigned, positive-only integers. Many other computer
languages, including C/C++, support both signed and unsigned integers.
DATA TYPES-THE SIMPLE TYPES

• Floating points: Used when evaluating expressions that require fractional precision. For
example, calculations such as square root, or transcendentals such as sine and cosine,
result in a value whose precision requires a floating-point type.
DATA TYPES-THE SIMPLE TYPES

• Characters: Used to store characters is char.


• char in Java is not the same as char in C or C++.
• In C/C++, char is an integer type that is 8 bits wide.
• Java uses Unicode to represent characters- 16 bits.
BOOLEANS

• Java has a simple type, called boolean, for logical values.


• It can have only one of two possible values, true or false. This is the type returned by
all relational operators, such as a < b.
VARIABLES

• Declaring a Variable
• In Java, all variables must be declared before they can be used. The basic form of a
variable declaration is shown here:
• type identifier [ = value][, identifier [= value] ...] ;
• To declare more than one variable of the specified type, use a comma-separated list.
EXAMPLE 2. A SHORT PROGRAM
INSTALLATION OF JAVA

• You will see two folders; JDK and JRE


• JDK – contains programs needed to develop java programs
• It contains : java compiler-javac.exe and java application launcher- java.exe and many other
tools.
• JRE; is run time environment contains JVM and Java library.
• JVM is platform independent-
• JVM is a virtual machine provides means of platform independent – interprets java byte code
into machine code based on operating system and underlying hardware
INTERACTION WITH STUDENTS

• Kindly send your answers only as Yes/No in comments box? Example Q1. Yes or No
• Q1. Have you download and installed Java on your computers?
• Q2. Have you well understood basic structure of Java program?
• Q3. Have you successfully compiled and run basic Java program?
• Q4. Do want to repeat concepts for Q2?
CHAR DATA TYPE

• Char variable can be either declared using single quotes or Unicode representation of the
symbol.
A CLOSER LOOK AT LITERALS

• Integer Literals: Any whole number value is an integer literal. Examples are 1, 2, 3, and
42.
• These are all decimal values, meaning they are describing a base 10.
• There are two other bases which can be used in integer literals, octal (base eight) and
hexadecimal (base 16). Octal values are denoted in Java by a leading zero. Normal
decimal numbers cannot have a leading zero. Thus, the seemingly valid value 09 will
produce an error from the compiler, since 9 is outside of octal’s 0 to 7 range.
• Beginning with JDK 7, you can also specify integer literals using binary.
LITERALS- EXAMPLES

• Octal – 01 to 07. 012(octal) to 10 (Decimal)


• Hexadecimal – 0X or 0x (0 to 9 and A to F) 0X(25D) to 605(Decimal)
• Binary – 0B or 0b 0B1010 to 10(Decimal)
• Exercise : Write a Java program which uses above literals.
ESCAPE SEQUENCE

• Exercise : Write a Java program which uses escape sequences: ( Home Work)
DYNAMIC INITIALIZATION

• Variables to be initialized dynamically, using any expression valid at the time the variable
is declared.
THE SCOPE AND LIFETIME OF VARIABLES

• A block is begun with an opening curly brace and ended by a closing curly brace. A block
defines a scope. Thus, each time you start a new block, you are creating a new scope.
• A scope determines what objects are visible to other parts of your program. It also
determines the lifetime of those objects.
• Most other computer languages define two general categories of scopes: global and local.
• In Java, the two major scopes are those defined by a class and those defined by a method.
• As a general rule, variables declared inside a scope are not visible (that is, accessible) to
code that is defined outside that scope. Scopes can be nested, when this occurs, the outer
scope encloses the inner scope. This means that objects declared in the outer scope will
be visible to code within the inner scope. However, the reverse is not true.
PROGRAM: SCOPE AND LIFE TIME
PROGRAM : LIFE TIME OF A VARIABLE

You might also like