0% found this document useful (0 votes)
25 views7 pages

OverloadedOperators StaticClassMembers and GarbageCollectionpdf 2024 09 16 11 48 33

Uploaded by

Ayush Batra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views7 pages

OverloadedOperators StaticClassMembers and GarbageCollectionpdf 2024 09 16 11 48 33

Uploaded by

Ayush Batra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Study Material by Ms Manmeet Kaur

Overloaded Operators in Java


Operator overloading is a programming method where operators are
implemented in user-defined types with specific logic dependent on the types of
given arguments.

Operator overloading makes it easier to specify a user-defined implementation


for operations with one or both operands of a user-defined class or structure.
This allows user-defined types to behave similarly to primitive data types. Java
does not support Operator Overloading Except for one case i.e + operator it
can be used to add number as well as strings.

Example-1,

public class Addition

public static void main(String[] args)

int a = 10;

int b = 20;

int c = a + b;

System.out.println(c); // 30

The Output of above Code will be

30

Example-2,

public class StringAddition

public static void main(String[] args)


Study Material by Ms Manmeet Kaur

String s1 = "Hello";

String s2 = "World";

String s3 = s1 + " " + s2;

The Output of above Code will be

Hello World

In The First Example + Operator Add Two Integer value but in Second it Add
Two String. This is Called Operator Overloading. In Above Example + operator
is overloaded as it perform different action depends on parameter passed.

It is the only operator overloading that Java supports. Aside from that, user-
defined operator overloading is not supported in Java. The handling of + for
concatenating strings is the only component of Java that comes close to operator
overloading in java.

Why Operator Overloading in Java is not Supported?

1. Overloading operators causes faults in the software. Operator overloading


causes programmers to become perplexed. Also, compared to other
languages, dealing with languages that offer operator overloading has a
higher risk of error. Method overloading is a feature of Java. Method
Overloading performs the same function as operator overloading while
removing the possibility of errors.
2. One of the goals of java designers is to have a simple and clear design.
They didn't want to imitate the language, but rather to have a clear, really
object oriented language. Operator overloading would definitely make
design more complex than it would be without it, and it will also slow
down the JVM because it will have to perform extra effort to figure out
what operators actually mean, reducing the opportunity to optimize the
language by guaranteeing operator behavior in Java.
Study Material by Ms Manmeet Kaur

3. Another advantage of not enabling operator overloading in Java is that


you can think of this. The lack of operator overloading has made the
language easier to manage and process, which has led to the development
of tools that process the language, such as an integrated development
environment (IDE) or a refactoring tool.
Study Material by Ms Manmeet Kaur

Static Class Members

What are static members of a Java class?

In Java, static members are those which belongs to the class and you can
access these members without instantiating the class. The static keyword can
be used with methods, fields, classes (inner/nested), blocks.

Static Methods − You can create a static method by using the keyword
static. Static methods can access only static fields, methods. To access static
methods there is no need to instantiate the class, you can do it just using the
class name as –

Example,

public class MyClass {


public static void sample(){
System.out.println("Hello");
}
public static void main(String args[]){
MyClass.sample();
}
}
Output:

Hello

Static Fields − You can create a static field by using the keyword static. The
static fields have the same value in all the instances of the class. These are
created and initialized when the class is loaded for the first time. Just like static
methods you can access static fields using the class name (without
instantiation).

public class MyClass {


public static int data = 20;
public static void main(String args[]){
System.out.println(MyClass.data);
}
Java Arrays with Answers
27
}
Study Material by Ms Manmeet Kaur

Output
20

Static Blocks − These are a block of codes with a static keyword. In general,
these are used to initialize the static members. JVM executes static blocks
before the main method at the time of class loading.

public class MyClass {


static{
System.out.println("Hello this is a static block");
}
public static void main(String args[]){
System.out.println("This is main method");
}
}

Output
Hello this is a static block
This is main method
Study Material by Ms Manmeet Kaur

Garbage Collection
Garbage collection in Java is the process by which Java programs
perform automatic memory management. Java programs compile to
bytecode that can be run on a Java Virtual Machine, or JVM for short.
When Java programs run on the JVM, objects are created on the heap,
which is a portion of memory dedicated to the program. Eventually, some
objects will no longer be needed. The garbage collector finds these
unused objects and deletes them to free up memory.

What is Garbage Collection?


In C/C++, a programmer is responsible for both the creation and
destruction of objects. Usually, programmer neglects the destruction of
useless objects. Due to this negligence, at a certain point, sufficient
memory may not be available to create new objects, and the entire
program will terminate abnormally, causing OutOfMemoryErrors.
But in Java, the programmer need not care for all those objects which are
no longer in use. Garbage collector destroys these objects. The main
objective of Garbage Collector is to free heap memory by
destroying unreachable objects. The garbage collector is the best
example of the Daemon thread as it is always running in the background.
How Does Garbage Collection in Java works?
Java garbage collection is an automatic process. Automatic garbage
collection is the process of looking at heap memory, identifying which
objects are in use and which are not, and deleting the unused objects. An
in-use object, or a referenced object, means that some part of your
program still maintains a pointer to that object. An unused or unreferenced
object is no longer referenced by any part of your program. So the
memory used by an unreferenced object can be reclaimed. The
programmer does not need to mark objects to be deleted explicitly. The
garbage collection implementation lives in the JVM.

Types of Activities in Java Garbage Collection

Two types of garbage collection activity usually happen in Java. These


are:
1. Minor or incremental Garbage Collection: It is said to have occurred
when unreachable objects in the young generation heap memory are
removed.
Study Material by Ms Manmeet Kaur

2. Major or Full Garbage Collection: It is said to have occurred when


the objects that survived the minor garbage collection are copied into
the old generation or permanent generation heap memory are
removed. When compared to the young generation, garbage collection
happens less frequently in the old generation.

You might also like