Adv.O O P Java: Bject Riented Rogramming in
Adv.O O P Java: Bject Riented Rogramming in
Adv.O O P Java: Bject Riented Rogramming in
Object Oriented
Programming in JAVA
Introduction
• Welcome to the course Advance Object
Oriented Programming in JAVA. This course
will cover a core set of computer science
concepts needed to create a modern software
application using Java.
• Widespread acceptance.
How is Java different from C…
• C Language:
– Major difference is that C is a structure oriented language and
Java is an object oriented language and has mechanism to
define classes and objects.
– Java does not support pointer type
– Java does not have preprocessor, so we cant use #define,
#include and #ifdef statements.
– Java does not include structures, unions and enum data types.
– Java does not include keywords like goto, sizeof and typedef.
– Java adds labeled break and continue statements.
– Java adds many features required for object oriented
programming.
How is Java different from C++…
• C++ language
Features removed in java:
Java doesn’t support pointers to avoid unauthorized access
of memory locations.
Java does not include structures, unions and enum data
types.
Java does not support operator over loading.
Preprocessor plays less important role in C++ and so
eliminated entirely in java.
Java does not perform automatic type conversions that
result in loss of precision.
Cont…
Java does not support global variables. Every method
and variable is declared within a class and forms part of
that class.
Java does not support inheritance of multiple super
classes by a sub class (i.e., multiple inheritance). This is
accomplished by using ‘interface’ concept.
It is not possible to declare unsigned integers(Unsigned
can hold a larger positive value, and no negative value.
Unsigned uses the leading bit as a part of the value,
while the signed version uses the left-most-bit to
identify if the number is positive or negative) in java.
In java objects are passed by reference only. In C++
objects may be passed by value or reference.
Cont …
New features added in Java:
Though C++ and java supports Boolean data type, C++ takes
any nonzero value as true and zero as false. True and false in
java are predefined literals that are values for a boolean
expression.
Java has replaced the destructor function with a finalize()
function.
C++ supports exception handling that is similar to java's.
However, in C++ there is no requirement that a thrown
exception be caught.
Characteristics of Java
• Java is simple • Java is architecture-neutral
• Java is object-oriented • Java is portable
• Java is distributed • Java’s performance
• Java is interpreted • Java is multithreaded
• Java is robust • Java is dynamic
• Java is secure
• Simple
According to Sun, Java language is simple because syntax is based on C++ (so easier for programmers to
learn it after C++). It removed many confusing and/or rarely-used features e.g., explicit pointers,
operator overloading etc. No need to remove unreferenced objects because there is Automatic Garbage
Collection in java.
• Object-oriented
Object-oriented means we organize our software as a combination of different types of objects that
incorporates both data and behavior. Object-oriented programming(OOPs) is a methodology that simplify
software development and maintenance by providing some rules.
• Platform Independent
A platform is the hardware or software environment in which a program runs.
There are two types of platforms software-based and hardware-based. Java provides software-based platform.
The Java platform differs from most other platforms in the sense that it is a software-based platform that runs
on the top of other hardware-based platforms. It has two components:
• Secured
Java is secured because:
No explicit pointer
Java Programs run inside virtual machine sandbox
3/24/2019 Object Oriented Programming using JAVA 16
• Robust
Robust simply means strong. Java uses strong memory management. There are lack of pointers
that avoids security problem. There is automatic garbage collection in java. There is exception
handling and type checking mechanism in java. All these points makes java robust.
• Architecture-neutral
There is no implementation dependent features e.g. size of primitive types is fixed.
In C programming, in data type occupies 2 bytes of memory for 32-bit architecture and 4 bytes of memory for
64-bit architecture. But in java, it occupies 4 bytes of memory for both 32 and 64 bit architectures.
• High-performance
Java is faster than traditional interpretation since byte code is "close" to native code still somewhat slower than a compiled language (e.g.,
C++)
• Distributed
We can create distributed applications in java. RMI and EJB are used for creating distributed applications. We may access files by calling the
• Interpreted
methods from any machine on the internet.
The compiler takes your .java file and compiles it into a .class file (the .class file contains Java byte code).
The interpreter comes in when your program is run. The JVM (or interpreter) takes your .class file and interprets it.
• Multi-threaded
A thread is like a separate program, executing concurrently. We can write Java programs that deal with many tasks at once by defining
multiple threads. The main advantage of multi-threading is that it doesn't occupy memory for each thread. It shares a common memory area.
Threads are important for multi-media, Web applications etc.
System.out.println("Hello world");
}
}
Compile and Execute
Things to remember
• Name of file must match name of class
– It is case sensitive
• About main()
– “main” is the function from which your program starts
– Why public?
• So that run time can call it from outside
– Why static ?
• it is made static so that we can call it without creating an object
– String concatenated with any other data type such as int will also
convert that datatype to String and the result will be a concatenated
String displayed on console
• For Example
– int i = 4
– int j = 5 ;
– System .out.println (“Hello” + i) // will print Hello 4 on screen
• However
– System,.out..println( i+j) ; // will print 9 on the console
System.out.println("Hello" + i);
System.out.println(i + j);
if (s1 == s2) {
System.out.println(“comparing string using == operator”);
}
if (s1.equals( s2) ) {
System.out.println(“comparing string using equal method”);
}
}
}
Compile and Execute
Taking in Command
Line Arguments
Taking in Command Line Arguments
/* This program will take two arguments Hello World from the command prompt
and prints them to standard console. If you specify less than two arguments
an exception will be thrown */
// The “+” operator here works similar to “<<“ operator in C++. This line is
// equivalent to cout<<“Arguments:”<<i<<“value”<<args[i];
// where cout is replaced by System.out.println, and “<<“ is replaced by + for
// concatenation
• Primitive data types are generally used for local variables, parameters and instance
variables (properties of an object)
• Primitive datatypes are located on the stack and we can only access their value,
while objects are located on heap and we have a reference to these objects
• Also primitive data types are always passed by value while objects are always
passed by reference in java. There is no C++ like methods
– void someMethod(int &a, int & b ) // not available in java
Primitives (cont)
• For all built-in primitive data types java
uses lowercase. E.g int , float etc
There are various ways to read input from the keyboard, the
java.util.Scanner class is one of them.
The Java Scanner class breaks the input into tokens using a
delimiter that is whitespace by default. It provides many methods
to read and parse various primitive values.
Java Scanner class is widely used to parse text for string and
primitive types using regular expression.