OBJECTIVE
● To differentiatebetween Structured Programming and OOP.
● To understand the syntax of a Java statement.
● To code, compile and execute a Java application.
3.
Introduction
Introduction to Java
Programming
Basicelement of Java Programming
• Keyword, identifier, data type
• I/O statements, selection statement and repetition statement
Predefined Packages
Array of
primitives data
types
01 02
04
03
CONTENTS
4.
Introduction to Java:
Historyof JAVA language
Designed by Sun Microsystems
as an internal corporate
research project originally
called Green then Oak. It was
designed for use in embedded
consumer electronic
applications.
1993
World Wide Web exploded in
popularity – Sun saw the
potential of using Java to
create Web pages – with
interactive and dynamic
content.
1995
First public implementation of
Java as Java 1.0 released.
Netscape Navigator Internet
browser incorporate by Java
technology.
1998
Java 2 was released and
become popular for writing
application program.
1991
• Java was developed by a team led by James Gosling at Sun Microsystems, a company best known for its Sun workstations.
• The power of Java is not limited to Web applications, for it is a general-purpose programming language.
• It has full programming features and can be used to develop standalone applications.
5.
Introduction to Java:Characteristics
Images reveal large amounts of
data, so remember: use an image
instead of a long text. Your audience
will appreciate it
No Characteristic Description
1. Simple Java is partially modeled on C++, but greatly simplified and improved
2. Dynamic Java was designed to adapt to an evolving environment. New code can be loaded
on the fly without recompilation.
3. Object Oriented programming in Java is centered on creating objects, manipulating objects, and
making objects work together.
4. Distributed involves several computers working together on a network. Java is designed to
make distributed computing easy.
5. Interpreted Java programs are compiled into the Java Virtual Machine code called bytecode,
and it is machine independent.
6. Robust Reliability because it puts a lot of emphasis on early checking for possible errors.
Java has a runtime exception-handling feature to provide programming support
for robustness.
6.
Introduction to Java:Characteristics
No Characteristic Description
7. Secure As an Internet programming language, Java is used in a networked and distributed
environment. Java implements several security mechanisms to protect your system
against harm caused by stray program.
8. Portable Java is architecture neutral. It can run in any platform without being recompiled. The
Java environment is portable to new hardware and operating systems
9. Performance Java’s performance is sometimes criticized compared to C++. Because Java is
interpreted, the bytecode is not directly executed by the system, but is run through the
interpreter.
10. Multithread a program’s capability to perform several tasks simultaneously.
7.
Object Oriented (OO)vs Structured
Structured programming OOP
▪ Data and functions are kept
separately.
▪ Using top-down approach.
o It breaks a program down into
components until they cannot be
composed anymore.
▪ Design is not very strong, hard to
understand and difficult to implement.
▪ In structured programming, functions
are dependent and hence reusability is
not possible
▪ Data and functions are combined into a
self-sufficient "object"
▪ Break down a programming task into
objects that expose behavior (methods)
and data (members or attributes) using
interfaces
▪ Easily understand the system as
objects rather than procedures.
▪ In OOP, functions are independent and
hence and it is reusable.
Introduction To JavaApplication
Java applications – standalone programs; applications can be executed from any computer with a
Java interpreter.
Java source code is written in plain text, using a text editor.
• This could range from a plain text editor like Notepad, to a programmers' editor such as
TextPad, to a complex integrated development environment (IDE) like BlueJ, NetBeans or
Eclipse.
Classes are the fundamental building blocks of Java programs.
Every Java application must have main method.
In Java, every source file that contain one public class, the name of public class must match the
name of the file containing the file.
Executing Java programs(cont.)
❑ After you have created myProg.java source file, The javac compiler is then
used to compile the source code into bytecode with the following
command:
javac Hello.java
❑ If the compiler does not return any message, the new file Hello.class is
stored in the same directory as the source file, unless specified
otherwise.
❑ To run your myProg application, executes your file with the following
command:
java Hello
14.
Java Keywords
❑ Keywordshave special meaning to the Java technology compiler.
❑ They identify a data type name or program construct name.
❑ The following table list the keywords that are used in the Java programming language:
15.
Identifiers
❑ Identifier isa name given to a variable, class or method.
❑ It start with a letter, underscore ( _ ), or dollar sign ($).
❑ Subsequent characters can be digits.
❑ Identifiers are case-sensitive and have no maximum length.
❑ Examples:
student, mySchool, student_name, subject1
$money, _a
❑ An identifier cannot be a keyword, but it can contain a keyword as part
of its name.
16.
Java: Data Types
❑There are two categories of data types
❑ Primitive (basic) data types (PDT).
❑ Reference data type (RDT).
❑ The Java programming language defines eight PDT, which can be considered in four categories:
❑ Logical – boolean
❑ Character – char
❑ Integral –Integer byte, short, int, long
❑ Floating point – double and float
❑ RDT referencing to an address which consist more than one value
❑ Example: class, array, interface, String
❑ Class types are used for more complex types, including all the types you declare yourself.
❑ Class types are used to create objects.
17.
Java: Basic DataTypes
❑ Numerical data type
Integers – int, long, short and byte
Floating-point – float and double
Example:
int num;
float no1 = 10.5f;
double no2 = 6.7;
❑ Character data type
Example:
char code = ‘a’; // differentiate with int code = ‘a’;
❑ Boolean data type
Example:
boolean lightOn;
lightOn = true;
..
if (lightOn) // or if (lightOn==true)
Definition: substring
● Assumestr is a String object and properly initialized to a string.
● str.substring( i, j ) will return a new string by extracting characters of str from
position i to j-1 where 0 i length of str, 0 j length of str, and i j.
● If str is “programming” , then str.substring(3, 7) will create a new string
whose value is “gram” because g is at position 3 and m is at position 6.
● The original string str remains unchanged.
Definition: length()
● Assumestr is a String object and properly initialized to a string.
● str.length( ) will return the number of characters in str.
● If str is "programming" , then str.length( ) will return 11 because there are
11 characters in it.
● The original string str remains unchanged.