Java includes a class called Class in the java.lang package. In a running Java application, instances of the class Class represent classes and interfaces. Class objects also represent the primitive Java types (boolean, byte, char, short, int, long, float, and double), as well as the keyword void. It has no public builder. The Java Virtual Machine creates class objects automatically (JVM). We cannot extend it because it is a final class.
public static Class forName(String className) throws ClassNotFoundException
Class t = Class.forName("java.lang.Thread")
public Object newInstance() throws InstantiationException, IllegalAccessException
public String getName()
public Class getSuperclass()
public Class[] getInterfaces()
public boolean isInterface()
public String toString()
public Method[] getMethods() throws SecurityException
public ClassLoader getClassLoader()
public Constructor<?>[] getConstructors() throws SecurityException
public class Coderz
{
public static void main(String[] args)
throws ClassNotFoundException
{
// forName method
// it returns the Class object for the class
// with the specified name
Class c = Class.forName("java.lang.Math");
System.out.print("Class represented by c : " + c.toString());
}
}
Class represented by c : class java.lang.Math
public class Coderz
{
public static void main(String[] args)
throws ClassNotFoundException
{
// returns the Class object for the class
// with the specified name
Class c = Class.forName("java.lang.String");
String s = "GeeksForGeeks";
int i = 10;
// checking for Class instance
// isInstance method
boolean b1 = c.isInstance(s);
boolean b2 = c.isInstance(i);
System.out.println("is s instance of String : " + b1);
System.out.println("is i instance of String : " + b2);
}
}
is s instance of String : true
is i instance of String : false
public class Coderz
{
public static void main(String[] args) throws ClassNotFoundException
{
// returns the Class object for the class
// with the specified name
Class c1 = Class.forName("java.lang.Math");
Class c2 = Class.forName("java.util.ArrayList");
// getting package of class
// getPackage method on c1
System.out.println(c1.getPackage());
// getPackage method on c2
System.out.println(c2.getPackage());
}
}
package java.lang
package java.util
import java.lang.reflect.Method;
public class Coderz
{
public static void main(String[] args)
throws SecurityException,ClassNotFoundException
{
// returns the Class Math for the class
// with the specified name
Class c1 = Class.forName("java.lang.Math");
// getMethods method on c1
// it returns array of methods of Object class
Method M[] = c1.getMethods();
System.out.println("Below are the methods of Math class : ");
// iterating through all methods of Math class
for (Method method : M)
{
System.out.println(method);
}
}
}
Below are the methods of Math class :
public static float java.lang.Math.abs(float)
public static long java.lang.Math.abs(long)
public static int java.lang.Math.abs(int)
public static double java.lang.Math.abs(double)
public static double java.lang.Math.sin(double)public static double java.lang.Math.cos(double)
public static double java.lang.Math.tan(double)
public static double java.lang.Math.atan2(double,double)
public static double java.lang.Math.sqrt(double)
public static double java.lang.Math.log(double)
public static double java.lang.Math.log10(double)
public static double java.lang.Math.pow(double,double)
public static double java.lang.Math.exp(double)
public static int java.lang.Math.min(int,int)
public static long java.lang.Math.min(long,long)
public static float java.lang.Math.min(float,float)
public static double java.lang.Math.min(double,double)
public static double java.lang.Math.max(double,double)
public static long java.lang.Math.max(long,long)
public static int java.lang.Math.max(int,int)
public static float java.lang.Math.max(float,float)
public static double java.lang.Math.floor(double)
public static double java.lang.Math.ceil(double)
public static double java.lang.Math.rint(double)
public static int java.lang.Math.addExact(int,int)
public static long java.lang.Math.addExact(long,long)
public static int java.lang.Math.decrementExact(int)
public static long java.lang.Math.decrementExact(long)
public static int java.lang.Math.incrementExact(int)
public static long java.lang.Math.incrementExact(long)
public static int java.lang.Math.multiplyExact(int,int)
public static long java.lang.Math.multiplyExact(long,int)
public static long java.lang.Math.multiplyExact(long,long)
public static long java.lang.Math.multiplyHigh(long,long)public static long java.lang.Math.negateExact(long)
public static int java.lang.Math.negateExact(int)
public static int java.lang.Math.subtractExact(int,int)
public static long java.lang.Math.subtractExact(long,long)
public static double java.lang.Math.fma(double,double,double)
public static float java.lang.Math.fma(float,float,float)
public static double java.lang.Math.copySign(double,double)
public static float java.lang.Math.copySign(float,float)
public static float java.lang.Math.signum(float)
public static double java.lang.Math.signum(double)
public static float java.lang.Math.scalb(float,int)
public static double java.lang.Math.scalb(double,int)
public static int java.lang.Math.getExponent(double)
public static int java.lang.Math.getExponent(float)
public static double java.lang.Math.asin(double)public static double java.lang.Math.acos(double)
public static double java.lang.Math.atan(double)
public static double java.lang.Math.toRadians(double)
public static double java.lang.Math.toDegrees(double)
public static double java.lang.Math.cbrt(double)
public static double java.lang.Math.IEEEremainder(double,double)
public static long java.lang.Math.round(double)
public static int java.lang.Math.round(float)
public static double java.lang.Math.random()public static int java.lang.Math.toIntExact(long)public static long java.lang.Math.multiplyFull(int,int)
public static int java.lang.Math.floorDiv(int,int)
public static long java.lang.Math.floorDiv(long,int)
public static long java.lang.Math.floorDiv(long,long)
public static int java.lang.Math.floorMod(int,int)
public static int java.lang.Math.floorMod(long,int)
public static long java.lang.Math.floorMod(long,long)
public static float java.lang.Math.ulp(float)
public static double java.lang.Math.ulp(double)
public static double java.lang.Math.sinh(double)
public static double java.lang.Math.cosh(double)
public static double java.lang.Math.tanh(double)
public static double java.lang.Math.hypot(double,double)
public static double java.lang.Math.expm1(double)
public static double java.lang.Math.log1p(double)
public static float java.lang.Math.nextAfter(float,double)
public static double java.lang.Math.nextAfter(double,double)
public static double java.lang.Math.nextUp(double)
public static float java.lang.Math.nextUp(float)
public static double java.lang.Math.nextDown(double)
Note: also read about the Reflection in Java
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…