TOPIC 2
Java Programming Basic
CSC435:
Object Oriented Programming
OBJECTIVE
● To differentiate between Structured Programming and OOP.
● To understand the syntax of a Java statement.
● To code, compile and execute a Java application.
CONTENTS
01 02
Introduction Basic element of Java Programming
Introduction to Java • Keyword, identifier, data type
Programming • I/O statements, selection statement and repetition statement
03 04
Array of
primitives data Predefined Packages
types
Introduction to Java:
History of JAVA language
• Java was developed by a team led by James Gosling at Sun Microsystems, a company best known for its Sun workstations.
1991 1993 1995 1998
Designed by Sun Microsystems World Wide Web exploded in First public implementation of Java 2 was released and
as an internal corporate popularity – Sun saw the Java as Java 1.0 released. become popular for writing
research project originally potential of using Java to Netscape Navigator Internet application program.
called Green then Oak. It was create Web pages – with browser incorporate by Java
designed for use in embedded interactive and dynamic technology.
consumer electronic content.
applications.
• 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.
Introduction to Java: Characteristics
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
Images revealmaking objects work
large amounts of together.
4. data, so remember:
Distributed useseveral
involves an imagecomputers working together on a network. Java is designed to
instead of a long [Link]
make Your audience
computing easy.
will appreciate it
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.
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.
Object Oriented (OO) vs Structured
Structured programming OOP
▪ Data and functions are kept ▪ Data and functions are combined into a
separately. self-sufficient "object"
▪ Using top-down approach. ▪ Break down a programming task into
o It breaks a program down into objects that expose behavior (methods)
components until they cannot be and data (members or attributes) using
composed anymore. interfaces
▪ Design is not very strong, hard to ▪ Easily understand the system as
understand and difficult to implement. objects rather than procedures.
▪ In structured programming, functions ▪ In OOP, functions are independent and
are dependent and hence reusability is hence and it is reusable.
not possible
Pure OO
Languages
Smalltalk, Eiffel, Actor, Java
Object-Oriented
Programming
Languages
Hybrid OO
Languages
C++, Objective-C, Object-Pascal
Introduction To Java Application
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.
Java Application Program
Java: Sample program
Executing Java programs
❑ The source code file should have a .java extension.
Executing Java programs (cont.)
❑ After you have created [Link] source file, The javac compiler is then
used to compile the source code into bytecode with the following
command:
javac [Link]
❑ If the compiler does not return any message, the new file [Link] 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
Java Keywords
❑ Keywords have 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:
Identifiers
❑ Identifier is a 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.
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.
Java: Basic Data Types
❑ 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)
Java: Basic Data Types (cont.)
Type Conversion (Casting)
Type Conversion (Casting)
String Data Types
String is an Object
String Indexing
Definition: substring
● Assume str is a String object and properly initialized to a string.
● [Link]( 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 [Link](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.
Examples: substring
Definition: length()
● Assume str is a String object and properly initialized to a string.
● [Link]( ) will return the number of characters in str.
● If str is "programming" , then [Link]( ) will return 11 because there are
11 characters in it.
● The original string str remains unchanged.
Examples: length()
Arithmetic Operators
● The following table summarizes the arithmetic operators available in Java.
Arithmetic Expression
Operator Precedence
Operator +
Input Statement- Using Console I/O
Output Statement- Using Console I/O
Sample Program – Using Console I/O
Input Statement Using Dialog Box (GUI interface)
Output Statement Using Dialog Box (GUI
interface)
Assign Statement
Selection statements
if, else statements
switch statement
Looping/repetition statement
The for Loops
The while Loops
The do/while Loops
Special Loop Flow of Control
The break and continue statement
Array of Primitives
Array Instantiation
Array Instantiation
Accessing an Array Element
Accessing an Array Element
Accessing an Array Element
Quiz
Quiz
Independent Function (IF)-Static Method
Function Call
Function Call
Exercise
References
[Link]
[Link]
Wu C. Thomas, An Introduction to Object-Oriented Programming with Java, 5th Edition, McGraw Hill , 2010
Deitel H. M. & Deitel P. J., Java How To Program, Prentice Hall, 8th Edition, 2010