u1 java
u1 java
What is Java?
It is an object-oriented language similar to C++, but with advanced and simplified
features. This language is free to access and can run on all platforms.
Multilevel Inheritance:
When a class is derived from a class which is also derived from another class, i.e. a class
having more than one parent class but at different levels, such type of inheritance is
called Multilevel Inheritance.
•Abstraction
abstraction( data hiding) helps to reduce complexity.
}
File: Simple.java
We should use a public keyword before the main() method so that JVM can identify the
execution point of the program. If we use private, protected, and default before the main()
method, it will not be visible to JVM.
• Syntax
type variable = value;
int data=50;//Here data is variable
For example:
int x; // valid
x = 10; // valid because “x” store only one value at a time because it is
the primitive type variable.
class ShortE
{
public static void main(String[] args)
{
short n= 3435;
System.out.println(n); // prints the value present in n i.e. 3435
}
}
•double
•double data type is 64-bit
•This data type is generally used as the default data type for decimal values, generally
the default choice
•Double data type should never be used for precise values such as currency
•Default value is 0.0d
•Example: double d1 = 123.4d
An array declaration has two components: the type and the name.
int intArray[]; or
int[] intArray;
package datatypePrograms;
class ShortExample
{
public static void main(String[] args)
{
short num = 200; // Here, 200 is stored into num which is declared as
short type.
System.out.println(num);
}
}
Java provides a rich set of operators to manipulate variables. We can divide all
the Java operators into the following groups −
•Arithmetic Operators
•Relational Operators
•Bitwise Operators
•Logical Operators
•Assignment Operators
•Misc Operators
No argument Constructors
As the name specifies the no argument constructors of Java does not accept any
parameters instead, using these constructors the instance variables of a method will be
initialized with fixed values for all objects.
Java Parameterized Constructor
A constructor which has a specific number of parameters is called a parameterized
constructor.
Why use the parameterized constructor?
The parameterized constructor is used to provide different values to distinct objects.
However, you can provide the same values also.
Private: The access level of a private modifier is only within the class. It cannot be
accessed from outside the class.
Default: The access level of a default modifier is only within the package. It
cannot be accessed from outside the package. If you do not specify any access
level, it will be the default.
Protected: The access level of a protected modifier is within the package and
outside the package through child class. If you do not make the child class, it
cannot be accessed from outside the package.
Public: The access level of a public modifier is everywhere. It can be accessed
from within the class, outside the class, within the package and outside the
package.
The garbage collection is a part of the JVM and is an automatic process done by JVM. We
do not need to explicitly mark objects to be deleted. However, we can request to the JVM
for garbage collection of an object but ultimately it depends on the JVM to call garbage
collector.
Dr. Tarun Singhal
Advantages of Garbage Collection
•Programmer doesn't need to worry about dereferencing an object.
•It is done automatically by JVM.
•Increases memory efficiency and decreases the chances for memory leak.
An object is able to get garbage collect if it is non-reference. We can make an object non-
reference by using three ways.
1. set null to object reference which makes it able for garbage collection.
class a=new class();
a=null;
2. We can non-reference an object by setting new reference to it which makes it able for
garbage collection.
class a=new class();
clas b=new clas();
a=b;
3. Anonymous object does not have any reference so if it is not in use, it is ready for the
garbage collection.
new class();
Request for Garbage Collection
We can request to JVM for garbage collection however, it is up to the JVM when to start the
garbage collector.
Java gc() method is used to call garbage collector explicitly. However gc() method does not
guarantee that JVM will perform the garbage collection. It only request the JVM for garbage
Dr. Tarun Singhal
collection.
Dr. Tarun Singhal