For-Each Loop in Java
• The for-each loop in Java (also called the enhanced for loop) was
introduced in Java 5 to simplify iteration over arrays and collections.
• It is cleaner and more readable than the traditional for loop and is
commonly used when the exact index of an element is not required.
import java.io.*;
class Geeks {
public static void main(String[] args) {
int arr[] = { 1, 2, 3, 4, 5 }; Output
for (int e : arr) {
System.out.print(e + " "); 1 2 3 4 5
}
}
}
Java Recursion
• Recursion is the technique of making a function call itself. This
technique provides a way to break complicated problems down into
simple problems which are easier to solve.
• Recursion may be a bit difficult to understand. The best way to figure
out how it works is to experiment with it.
public class Main {
public static void main(String[] args) {
int result = sum(10);
System.out.println(result);
}
public static int sum(int k) {
if (k > 0) {
return k + sum(k - 1);
} else {
return 0; 10 + sum(9)
} 10 + ( 9 + sum(8) )
10 + ( 9 + ( 8 + sum(7) ) )
} ...
} 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum(0)
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0
instanceof Keyword in Java
• In Java, instanceof is a keyword used for checking if a reference
variable contains a given type of object reference or not. Following is
a Java program to show different behaviors of instanceof. Henceforth
it is known as a comparison operator where the instance is getting
compared to type returning boolean true or false as in Java we do not
have 0 and 1 boolean return types.
class Parent { if (cobj instanceof Object)
} System.out.println(
class Child extends Parent { "cobj is instance of Object");
} else
class GFG { System.out.println(
public static void main(String[] args) "cobj is NOT instance of Object");
{ }
Child cobj = new Child(); }
if (cobj instanceof Child)
System.out.println("cobj is instance of Child");
else
System.out.println(
"cobj is NOT instance of Child");
if (cobj instanceof Parent)
System.out.println(
"cobj is instance of Parent"); Output
else cobj is instance of Child
System.out.println( cobj is instance of Parent
"cobj is NOT instance of Parent"); cobj is instance of Object
Nested Classes in Java
• it is possible to define a class within another class, such classes are known
as nested classes.
class OuterClass
{
...
class NestedClass
{
...
}
}
class OuterClass {
Java Inner Classes int x = 10;
class InnerClass {
int y = 5;
• In Java, it is also possible }
to nest classes (a class }
within a class). The
purpose of nested classes public class Main {
public static void main(String[] args) {
is to group classes that OuterClass myOuter = new OuterClass();
belong together, which OuterClass.InnerClass myInner = myOuter.new
makes your code more InnerClass();
readable and System.out.println(myInner.y + myOuter.x);
}
maintainable. class }
OuterClass
// Outputs 15 (5 + 10)
Static class in Java
• Java allows a class to be defined within another class. These are
called Nested Classes. Classes can be static which most developers
are aware of, henceforth some classes can be made static in Java.
Java supports Static Instance Variables, Static Methods, Static Block,
and Static Classes. The class in which the nested class is defined is
known as the Outer Class. Unlike top-level classes, Nested classes
can be Static. Non-static nested classes are also known as Inner
classes.
https://www.geeksforgeeks.org/java/static-class-in-java/
Static class in Java
• Java allows a class to be defined within another class. These are
called Nested Classes. Classes can be static which most developers
are aware of, henceforth some classes can be made static in Java.
Java supports Static Instance Variables, Static Methods, Static Block,
and Static Classes. The class in which the nested class is defined is
known as the Outer Class. Unlike top-level classes, Nested classes
can be Static. Non-static nested classes are also known as Inner
classes.
https://www.geeksforgeeks.org/java/static-class-in-java/
Java Anonymous Class
• An anonymous class in Java is an inner class which is declared
without any class name at all. In other words, a nameless inner class
in Java is called an anonymous inner class. Since it does not have a
name, it cannot have a constructor because we know that a
constructor name is the same as the class name.
new(argument-list)
{
// Anonymous class body
}
https://www.tutorialspoint.com/java/java_anonymous_class.htm
Java Singleton Class
• Singleton pattern is one of the simplest design patterns in Java. This
type of design pattern comes under creational pattern as this pattern
provides one of the best ways to create an object.
• This pattern involves a single class which is responsible to create an
object while making sure that only single object gets created. This
class provides a way to access its only object which can be accessed
directly without need to instantiate the object of the class.
• Singleton design pattern saves memory because only one object
instance is created and it also provides global access to its instance.
https://www.tutorialspoint.com/java/java_singleton_class.htm
class Singleton {
private static Singleton singleton = new Singleton( );
private Singleton() { }
/* Static 'instance' method */
public static Singleton getInstance( ) {
return singleton;
}
/* Other methods protected by singleton-ness */
protected void demoMethod( ) {
System.out.println("demoMethod for singleton");
}}
public class Tester { Output
public static void main(String[] args) { demoMethod for singleton
Singleton tmp = Singleton.getInstance( );
tmp.demoMethod( );
}}
enum in Java
• Enumerations or Java Enum serve the purpose of representing a
group of named constants in a programming language. Java Enums
are used when we know all possible values at compile time, such as
choices on a menu, rounding modes, command-line flags, etc.
enum Color {
RED,
GREEN,
BLUE;
}
public class Test {
// Driver method
public static void main(String[] args) {
Color c1 = Color.RED;
System.out.println(c1);
} Output
} Red
Java - Enum Constructor
• Java enum is a special kind of class that represents a group of
pre-defined constant values and can be used in switch expressions for
comparison, to be used as constants in application code.
• By default, an enum does not require any constructor and its default
value is the same as its declaration.
enum WEEKDAY { MONDAY, TUESDAY, WEDNESDAY,
THRUSDAY, FRIDAY, SATURDAY, SUNDAY }
System.out.println(WEEKDAY.MONDAY);
enum WEEKDAY {
MONDAY("Day 1");
private final String description;
WEEKDAY(String description) {
this.description = description;
}
}
Java enum Strings
In Java, we can get the string representation of enum constants using
the toString() method or the name() method. For example
enum Size {
SMALL, MEDIUM, LARGE, EXTRALARGE
}
class Main {
public static void main(String[] args) {
System.out.println("string value of SMALL is " + Size.SMALL.toString());
System.out.println("string value of MEDIUM is " + Size.MEDIUM.name());
}
}
Reflection in Java
Reflection is an API that is used to examine or modify the behavior of
methods, classes, and interfaces at runtime. The required classes for
reflection are provided under java.lang.reflect package which is
essential in order to understand reflection.
https://www.geeksforgeeks.org/java/reflection-in-java/