Professional Documents
Culture Documents
Object Oriented Programming
Object Oriented Programming
PROGRAMMING
TOPICS TO BE COVERED TODAY
Whitespace
Value, Variable and Data Type
Type Conversion
Scope of variable
Java Input
LEXICAL ISSUES
Whitespace
Java is a free-form language.
4
Whitespace
public class DisplayForecast {
5
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:
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.
Java C++
TYPE OF A VARIABLE
The type int is of 4 bytes in Java.
Therefore, it can hold maximum of 2,147,483,647 value.
-2,147,483,648.
Java does not support unsigned, positive-only integers.
VARIABLE FOR REAL NUMBERS
intcannot hold a real value.
Therefore, a type “double” is used to hold real
values.
Double takes 8 bytes of memory instead of 4
bytes of a double.
Out of the 8 bytes in a double 4 bytes are used to
hold the value before the decimal point and 4
bytes for the value after the decimal point.
CHAR DATA TYPE
In Java, the data type used to store characters is char.
However 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.
This is not the case in Java. Instead, Java uses Unicode to represent
characters.
Unicode defines a fully international character set that can represent all
of the characters found in all human languages.
It is a unification of dozens of character sets, such as Latin, Greek, Arabic,
Cyrillic, Hebrew, Katakana, Hangul, and many more.
For this purpose, it requires 16 bits. Thus, in Java char is a 16-bit type.
BOOLEAN DATA TYPE
Java has a simple type, called boolean, for logical values.
It can have only one of two possible values, true or false.
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
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
x = 5671
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
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);
45
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
46
JAVA INPUT
47
QUIZ
48
What is the output of this program?
Lecture 2
class conversion {
public static void main(String args[])
Object-Oriented Programming
{
double a = 295.04;
int b = 300;
byte c = (byte) a;
byte d = (byte) b;
System.out.println(c + " " + d);
}
}
a)38 43 b)39 44
c)295 300 d) 295.04 300 49
WHAT IS THE ERROR IN THIS CODE?
Lecture 2
byte b = 50;
b = b * 50;
Object-Oriented Programming
b cannot contain value 100, limited by its range