Java - Rahul Oopj
Java - Rahul Oopj
C Polymorphism
Polymorphismrefers to the ability to perform a certain action in different ways. In Java, polymorphism
can take two forms: method overloading and method overriding. Method overloading happens when various methods
with the same name are present in a class. When they are called they are differentiated by the number, order, and
types of their parameters. Method overriding occurs when the child class overrides a method of its parent.
Example
i. The same method name is used several times.
ii. Different methods of the same name can be called from the object.
iii. All Java objects can be considered polymorphic (at the minimum, they are of their own type and instances of
the Object class).
1 The Java Virtual Machine (JVM) is the runtime engine of the Java Platform, which allows any program written in Java or
other language compiled into Java bytecode to run on any computer that has a native JVM.
2 The Java virtual machine is an abstract (virtual) computer defined by a specification. It is a part of java runtime environment.
The garbage-collection algorithm used and any internal optimization of the Java virtual machine instructions (their translation
into machine code) are not specified. The main reason for this omission is to not unnecessarily constrain implementers. Any Java
application can be run only inside some concrete implementation of the abstract specification of the Java virtual machine.[2]
3 Starting with Java Platform, Standard Edition (J2SE) 5.0, changes to the JVM specification have been developed under the Java
Community Process as JSR 924.[3] As of 2006, changes to specification to support changes proposed to the class file format (JSR
202)[4] are being done as a maintenance release of JSR 924. The specification for the JVM was published as the blue book,[5] The preface
states:
1. Object Oriented
In Java, everything is an Object. Java can be easily extended since it is based on the Object model.
2. Platform Independent
Unlike many other programming languages including C and C++, when Java is compiled, it is not compiled into platform
specific machine, rather into platform-independent byte code. This byte code is distributed over the web and interpreted by the Virtual
Machine (JVM) on whichever platform it is being run on.
3. Simple
Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to master.
4. Secure
With Java's secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques are based on
public-key encryption.
5. Architecture-neutral
Java compiler generates an architecture-neutral object file format, which makes the compiled code executable on many
processors, with the presence of Java runtime system.
i. Preventing naming conflicts. For example there can be two classes with name Employee in two
packages, college.staff.cse.Employee and college.staff.ee.Employee
ii. Making searching/locating and usage of classes, interfaces, enumerations and annotations easier
iii. Providing controlled access: protected and default have package level access control. A protected
member is accessible by classes in the same package and its subclasses. A default member (without any
access specifier) is accessible by classes in the same package only.
b) All we need to do is put related classes into packages. After that, we can simply write an import class from
existing packages and use it in our program. A package is a container of a group of related classes where some of
the classes are accessible are exposed and others are kept for internal purpose.
c) We can reuse existing classes from the packages as many time as we need it in our program.
1) java I/O (Input and Output) is used to process the input and produce the output.
2) Java uses the concept of a stream to make I/O operation fast. The java.io package contains all the classes required for
input and output operations.
3) We can perform file handling in Java by Java I/O API.
4) A stream is a sequence of data. In Java, a stream is composed of bytes. It's called a stream because it is like a stream of
water that continues to flow.
5) In Java, 3 streams are created for us automatically. All these streams are attached with the console.
a) A constructor in Java is a block of code similar to a method that's called when an instance of an object is
created. Unlike methods, constructors are not considered members of a class. A constructor is called
automatically when a new instance of an object is created.
b) In Java, we can overload constructors like methods. The constructor overloading can be defined as the concept
of having more than one constructor with different parameters so that every constructor can perform a
different task.
c) Consider the following example
Student(){
System.out.println("this a default constructor");
}
A static method is a method that belongs to a class, but it does not belong to an instance of that class and this method
can be called without the instance or object of that class
.
What is an non-static method ?:
Every method in java defaults to a non-static method without static keyword preceding it . Non-static methods can
access any static method and static variable, without creating an instance of the object.
Below are the various important differences among these:
In non-static method, the method can access static data members and static methods as well as non-
static members and method of another class or same class, also can change the values of any static data
member.
2. Calling process:
In static method, The memory of a static method is fixed in the ram, for this reason we don’t need the
object of a class in which the static method is defined to call the static method. To call the method we
need to write the name of the method followed by the class name.
In non-static method, the memory of non-static method is not fixed in the ram, so we need class object
to call a non-static method. To call the method we need to write the name of the method followed by
the class object name.
1. toLowerCase()
This function changes the case of the given string to the lower case. We can use this function at the time of
string to handle the ignore case with the compareTo() function itself.
2. toUpperCase()
This function remains the same as toLowerCase(), but it changes the case to uppercase.
3. Substring()
In the example code area, we will see the use of this function with the index as an input and the output as well.
For this, we need to have a string upon which we will be performing this operation. We can use the position
from where we want the string in place of the StartIndex, and we can replace the LastIndex with the end index
to where we want the string.
4. trim()
Again, this is a string manipulation function we can use to remove the unwanted space from a given string.
5 Length()
6 charAt()
I. Array
a) Normally, an array is a collection of similar type of elements which has contiguous memory location.
b) Java array is an object which contains elements of a similar data type. Additionally, The elements of an array are
stored in a contiguous memory location. It is a data structure where we store similar elements. We can store only
a fixed set of elements in a Java array.
c) Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd element is stored on 1st
index and so on.
d) Unlike C/C++, we can get the length of the array using the length member. In C/C++, we need to use the sizeof
operator.Difference between JDK, JRE, and JVM
e) In Java, array is an object of a dynamically generated class. Java array inherits the Object class, and implements
the Serializable as well as Cloneable interfaces. We can store primitive values or objects in an array in Java. Like
C/C++, we can also create single dimentional or multidimentional arrays in Java.
f) Moreover, Java provides the feature of anonymous arrays which is not available in C/C++
ii. Vectors
a) The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully
compatible with collections. It is found in the java.util package and implements the List interface, so we can use
all the methods of List interface here.
b) Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains
components that can be accessed using an integer index
c) They are very similar to ArrayList but Vector is synchronized and has some legacy method that the collection
framework does not contain.
d) It also maintains an insertion order like an ArrayList but it is rarely used in a non-thread environment as it
is synchronized and due to which it gives a poor performance in adding, searching, delete and update of its
elements.
e) The Iterators returned by the Vector class are fail-fast. In the case of concurrent modification, it fails and throws
the ConcurrentModificationException.