Object Oriented Programming 2nd Year Computer & System Engineering Department
Object Oriented Programming 2nd Year Computer & System Engineering Department
2nd Year
Computer & System Engineering
Department
Global variables:
– These are discouraged in Java. It is impossible to create a global
variable that is outside of all classes. The only way to create a
global variable is by declaring public static variable in a class.
GOTO:
– Java has no goto statement. It reserves the goto keyword to cover
the only sensible uses of it.
Pointers:
– Java has no way to manipulate pointers (mem addresses) directly.
You cannot convert an integer to a pointer, you cannot
dereference an arbitrary memory address.
Differences between Java and C++
Memory Allocation:
– Java has no malloc() or free() functions. Since every complex data
structure is an object, they are all allocated memory via the new
operator. As soon as there is no reference of any kind to an object,
it is collected- automatically - for reuse by the system (garbage
collection/ recycle).
Data types:
– Hardware-dependent data types such as int and char* lead to non-
portable code. Java uses a standard size irrespective of the H/W.
No header files.
No preprocessor.
Differences between Java and C++
Naming conventions
– public methods and variables have a leading lowercase
(nextItem, currentValue, getTime)
– private and local identifiers have lower case with mixed
underscore (next_time, current_value, temp_position)
– final variables that represent contants use all upper case
(GREEN, FRIDAY, MAXSTUDENT)
– class names starts with upper case (String, HelloWorld,
MaterialClass)
Special case: Primitive Types
if-else
if (Boolean-expression) statement [else statement]
return
Iteration
while(Boolean-expression) statement
do statement while(Boolean-expression);
for(initialization; Boolean-expression; step) statement
Example:
for(int i = 0, j = 1; i < 10 && j != 11; i++, j++)
Inside the body of any of the iteration statements you can
control the flow of the loop by using break and continue.
Execution Control
switch(integral-selector) {
case integral-value1 : statement; [break;]
case integral-value2 : statement
statement; [break;]
case integral-value3 : statement; [break;]
case integral-value4 : statement; [break;]
case integral-value5 : statement; [break;]
// ...
default: statement;
}
switch is “fall through”
Classes
Constructors
– String s = new String(); //creates empty string
– char ch[] = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’}
– String s = new String(ch); //creates “abcdef”
– String s1 = new String(ch,2,3); // creates “cde”
– byte ascii[]={65,66,67,68,69,70};
– String s2 = new String(ascii,0); //”ABCDEF” with hiByte=0
– String s3 = new String(ascii,0,2,3); //” CDE” with hiByte=0
– Special syntax
String s = “abcd”;
– Member method: length()
String s = “abcd”; System.out.println(s.length());
Sample of Java Classes:
Concatenation
– ‘+’ is overloaded
int age;
String s = “He is” + age + “years old”;
int age;
String s = new StringBuffer(“He is”)
.append(age)
.append(“years old”)
.toString();
String conversion:
Every class should override toString() method to get the required representation of
the output.
Sample of Java Classes:
Character Extraction
String s = “ABCDEF”
char ch = s.charAt(1)
int start = 2;
int end = 5;
char buf[] = new char(end – start);
s.getChars(start, end, buf, 0);
byte bbuf[] = new byte(end – start);
s.getByte (start, end, bbuf, 0); Extracts the chars from
the string and place them
into byte buffer after
dropping the high order
byte.
Sample of Java Classes:
String Comparison
boolean equals(String s)
boolean equalsIgnoreCase(String s)
boolean regionMatches(int offset, String other, int otheroffset, int len)
boolean regionMatches(boolean ignorecase, int offset, String other, int
otheroffset, int len)
boolean startsWith(String s)
boolean endsWith(String s)
int compareTo(String s)
int indexOf(char ch)
int lastIndexOf(char ch)
………
Note: == is used only to compare two object references
Sample of Java Classes:
try{
//block of code
} catch(ExceptionType1 e){
// exception handler for ExceptionType1
}catch(ExceptionType2 e){
// exception handler ExceptionType2
throw(e); // rethrow the exception
…
}finally {
// finally code
}
Main Exception Types
How Exception Works
When java runtime tries to execute the code of try block, and an
abnormal condition occurs, the runtime stops the code and throws an
exception object of the type representing the condition (error).
The flow of code execution is interrupted and the current call-stack is
searched for any compatible exception handler.
If no exception handler is found, the default handler is used which
prints out the Exception object and the stack trace of where the
exception occurred.
The scope of the catch clause is restricted to the immediately
preceding try statement.
The throw statement is used to explicitly throw an exception. The flow
of execution stops immediately after the throw statement, and the next
statement is not reached. The corresponding handler is executed.
Exception Specification (throws)