You are on page 1of 58

OBJECT ORIENTED

PROGRAMMING
SUMMARY OF PREVIOUS LECTURE

Lecture 2
 What is Bytecode?
 Define the slogan “Write Once, Run Anywhere”.

Object-Oriented Programming
 What is JVM?

 Why main() is static method?

 How Java is Object Oriented?

 Why Java is considered robust language?

 What happens if we write Main() instead of main()?

2
TOPICS TO BE COVERED TODAY
 Whitespace
 Value, Variable and Data Type

 Type Conversion

 Arithmetic Expression Evaluation

 Scope of variable

 Java Input
LEXICAL ISSUES
Whitespace
 Java is a free-form language.

 For example, the program could have been written all on


one line or in any other strange way you felt like typing
it.
 In Java, whitespace is a space, tab, or newline.
GOOD WHITESPACING
// Authors: J. P. Cohoon and J. W. Davidson
// Purpose: display a quotation in a console window

5
Whitespace
public class DisplayForecast {

// method main(): application entry point


public static void main(String[] args) {
System.out.print("I think there is a world market for");
System.out.println(" maybe five computers.");
System.out.println(" Thomas Watson, IBM, 1943.");
} Whitespace separates program elements
}
Whitespace between program elements is
ignored by Java
BAD WHITESPACING
 The same program without any whitespacing or
comments:

6
public class DisplayForecast2 { public static void
main (String[] args) { System.out.print("I think
there is a world market for"); System.out.println("
maybe five computers."); System.out.println("
Thomas Watson, IBM, 1943."); } }
LEXICAL ISSUES
Identifiers
 Identifiers are used for class names, method names, and variable
names.
 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.
 Java is case-sensitive, so VALUE is a different identifier than
Value.
 Some examples of valid identifiers are:
AvgTemp count a4 $test this_is_ok
 Invalid variable names include:
2count high-temp Not/ok
LEXICAL ISSUES
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”


 Left to right, the first literal specifies an integer, the next
is a floating-point value, the third is a character constant,
and the last is a string.
 A literal can be used anywhere a value of its type is
allowed.
LEXICAL ISSUES
Comments
 There are three types of comments defined by Java. You
already know 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 */.
LEXICAL ISSUES
Separators
 In Java, there are a few characters that are used as
separators.
 The most commonly used separator in Java is the
semicolon.
LEXICAL ISSUES
The Java Keywords
 There are 49 reserved keywords currently defined in the Java
language

 The keywords const and goto are reserved but not used.
JAVA CLASS LIBRARIES
 The Java environment relies on several built-in class
libraries that contain many built-in methods that provide
support for such things as I/O, string handling,
networking, and graphics.
 The standard classes also provide support for windowed
output.
 Thus, Java as a totality is a combination of the Java
language itself, plus its standard classes.
VARIABLE
 To store a value inside a computer a ‘variable’ is used.
 A variable is a space in the memory to store a value.

 This space is reserved until the variable is required.


WHAT MAKES A VARIABLE
 Variable has three important characteristics:
 Type
 How much memory do a variable need.
 This information is determined by a type.
 Name
 How to differentiate a variable with another variable of the
same type.
 Name refers to the memory location assigned to this variable.
 Value
 What is the value?
 The actual value contained by a variable.
TYPE OF A VARIABLE
 Among other advantages a ‘type’ binds the memory to a
variable name.
 Java is more strictly typed than either language.
 For example, in C/C++ you can assign a floating-point value to an
integer. In Java, you cannot.
 Also, in C there is not necessarily strong type-checking between a
parameter and an argument. In Java, there is.

Java C++
JAVA SIMPLE DATA TYPES
 The simple types represent single values—not complex
objects.
 Although Java is otherwise completely object-oriented, the
simple types are not.
 That’s why Java is 99.99% Object Oriented Programming Language
 The simple types are defined to have an explicit range
and mathematical behavior.
 Languages such as C and C++ allow the size of an integer to vary based
upon the dictates of the execution environment. However, Java is
different.
 Because of Java’s portability requirement, all data types have a strictly
defined range.
TYPE CONVERSION
 Java can perform conversion automatically
 int value can be assigned to long.

 Depends upon type compatibility

 Not all type conversions implicitly allowed.

 Cant assign a long value to int.

 Solution
 Casting
TYPE CONVERSION (CONT.)
 Widening conversion
 Narrow data types are converted into broad data type with out
loss of information
 Both types are compatible.
 Numeric types are not compatible with Boolean and char

 Destination type is larger than source type.

 Example
 byte  int
 int  long
TYPE CONVERSION (CONT.)
 Narrowing conversion
◦ Broader data type is converted into narrower data type with loss
of information
◦ Process is called casting (explicit type conversion)
◦ Target variable = (Target-type) Source variable
 byte b;
 int a=50;
 b=(byte)a;
◦ Truncation??????
◦ Type conversion in expressions
 (f*b) + (i/c) –(d*s) ?????????
TYPE CONVERSION IN EXPRESSIONS
DESCRIPTION

 In the first subexpression, f * b, b is promoted to a float and the


result of the subexpression is float.
 Next, in the subexpression i / c, c is promoted to int, and the result is
of type int. Then, in d * s, the value of s is promoted to double, and
the type of the subexpression
is double.
 Finally, these three intermediate values, float, int, and double, are
considered.
 The outcome of float plus an int is a float. Then the resultant float
minus the last double is promoted to double, which is the type for
the final result of the expression.
MANIPULATING VARIABLES
 Assignment Statement
 In Mathematics the value x = x + 1 is not possible why?
 In Java x = x +1 is possible because “=” is an assignment operator
and not an equality operator.
 Assignment operator means that the contents of the right hand
side is transferred to the memory location of the left hand side.
ASSIGNMENT STATEMENT

x = 5671

5671 is written at the memory location reserved for x


CONSTANTS
 Constants are values which cannot be modified e.g. the
value of Pi
 To declare a constant in Java, we write a keyword “final”
before the variable type.

final double pi = 3.14;


WHAT IS THE RESULT OF THIS
EXPRESSION?
 What is the result of this arithmetic expression

6+2*3/6
a) 7
b) 0.5
c) 13.0
d) 4
MANIPULATING VALUES
 Mathematical Operators
 Common mathematical operators are available in Java for
manipulating values e.g. addition(+), subtraction(-),
multiplication(*), division(/), and modulus (%).
 Java has many other operators also which we will study
in due course.
ARITHMETIC EXPRESSION
EVALUATION
 To evaluate an arithmetic expression two concepts needs
to be understood
 Operator Precedence
 Operator precedence controls the order in which operations are
performed
 Operator Associativity
 The associativity of an operator specifies the order in which
operations of the same precedence are performed
OPERATOR PRECEDENCE AND
ASSOCIATIVITY
 Operators Precedence and Associativity for Java is
following

1. *, /, %  Do all multiplications, divisions and remainders


from left to right.
2. +, -  Do additions and subtractions from left to right.
EVALUATING AN EXPRESSION
6+2*3/6
 Three operators are in this expression.
 However, * and / both have the same precedence and + has
lower precedence then these two.
 * and / will be evaluated first but both have the same
precedence level.
 Therefore, operator associativity will be used here to determine
the first to get evaluated i.e. left to right.
 The right most sub expression will be evaluated followed by
the next right one and so on.
SCOPE OF VARIABLE
SCOPE OF VARIABLE

 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.
 The scope defined by a method begins with its opening curly
brace.
OUTPUT???
OUTPUT
ANOTHER EXAMPLE
•Blocks can be nested, In Java you cannot declare a
variable to have the same name as one in an outer
scope.
PRIMITIVE DATA TYPES
 So far the variable types that we have studied are
primitive data types.
 Primitive data types only have a memory space for
storing values.
 However, Object-Oriented Programming is special
because OOP has more variables then just primitive data
types.
OBJECTS
 Objects are one step ahead of primitive data types.
 They contain values and operations both.
 e.g. A String object has a value as well as operations that
could be performed on that value e.g. operations to find its
size, operation to compare its size with another String etc.
CLASS PARTICIPATION
CLASS PARTICIPATION
 1 : What will happen?

float f=65/10+38/10;
System.out.println(f);
CLASS PARTICIPATION
 2: What will be the output?

int i=638;
byte b=(byte)i;
System.out.println(b);
OUTPUT??
 System.out.println (“result: “ + 3/5);
 What does it print?
 result: 0
 System.out.println (“result: “ + 5 % 3);

55
 What does it print?
 result: 2
 System.out.println (“result: “ + 3/5.0);
 What does it print?
 result: 0.6
 System.out.println (“result: “ + 3+4.0);
 What does it print?
 result: 34.0
 System.out.println (“result: “ + (3+4.0));
 What does it print?
 result: 7.0
JAVA INPUT

56
JAVA INPUT

57
Questions?

You might also like