Java.lang - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.lang Classes

Java.lang Classes

shape Description

The java.lang.Object class is the root for every Java class which will have various methods and automatically all methods will be available to all Java classes. In case the class is extending any other class, then it becomes the indirect subclass of the object otherwise it becomes a direct subclass of object. Following is the syntax of java.lang Class class. [java]public class Object[/java] The java.lang.Class class is the basic package class which consists of a set of classes and interfaces that are used to build the code. The class does not have any public constructor and here all the class objects will be constructed by JVM (Java Virtual Machine). Following is the declaration for java.lang.Object class. [java] public final class Class<T> extends Object implements Serializable, GenericDeclaration, Type, AnnotatedElement [/java]

shape Conceptual Figure

Most of the java classes will implement following interfaces. The Serializable is like a marker interface means that it does not has data member and method, the purpose of this interface is to mark all java classes then object of these classes will get certain power. The Comparable interface is utilized to sort the user defined class objects, but the developer can sort elements depends on single data member only such as age, id, address. The CharSequence interface underpins read only access to various char sequences.

Following are the methods of java.lang.Object

shape Table

Following are the regularly using methods of java.lang.Object class.
Methods Description
toString( ) method toString( ) method is used to get the String representation of an object.
hashCode() method A unique number will be generated by JVM for every object is called as hashcode.
equals() method An equivalence of any two objects will be checked by this method.

shape Example 1

Following is an example by using toString( ) method. Now first following code is going to show the problem with out using to.String(). Student.java [java] package com.SPlessons; class Student{ int rollno; String name; String city; Student(int rollno, String name, String city){ this.rollno=rollno; this.name=name; this.city=city; } public static void main(String args[]){ Student s1=new Student(101,"Sam","U.S.A"); Student s2=new Student(102,"Sanjay","U.K"); System.out.println(s1);//compiler writes here s1.toString() System.out.println(s2);//compiler writes here s2.toString() } } [/java] In the above example, if the developer does not use the to.String() method, then the only hash code of the objects will be printed. Output: Now compile the code then result will be as follows. [java] com.SPlessons.Student@1db9742 com.SPlessons.Student@106d69c [/java] Now SPlesson is going to show the same example by using to.String() method. [java] package com.SPlessons; class Student{ int rollno; String name; String city; Student(int rollno, String name, String city){ this.rollno=rollno; this.name=name; this.city=city; } public String toString(){//overriding the toString() method return rollno+" "+name+" "+city; } public static void main(String args[]){ Student s1=new Student(101,"Sam","U.S.A"); Student s2=new Student(102,"Sanjay","U.K"); System.out.println(s1);//compiler writes here s1.toString() System.out.println(s2);//compiler writes here s2.toString() } } [/java] Output: Here JVM internally calls the to.String() method, this method will be override and values of the object will be printed. [java] 101 Sam U.S.A 102 Sanjay U.K [/java]

shape Example 2

Following is an example to hashcode() method . HashCode.java [java] package com.SPlessons; public class HashCode { public static void main(String[] args) { // TODO Auto-generated method stub String s=new String("Hey...Welcome to SPlessons."); System.out.println("The hash code for given String is :" +s.hashCode()); } } [/java] Output: Now compile the code then a unique number will be generated for the given String as follows. [java]The hash code for given String is :1351473765[/java] Following is an example for equals() method. EqualsMethod.java [java] package com.SPlessons; public class EqualsMethod { public static void main(String[] args) { //get an integer, which is an object Integer x=new Integer(2); //get the float, which is an object also Float y=new Float(2f); System.out.println(""+x.equals(y)); System.out.println(""+x.equals(x)); } } [/java] Now in the code, the developer is comparing Integer and Float but first, it returns false because of x.equals(y) i.e both are different classes. Output: Now compile the code result will be as follows. [html] false true [/html]

Following are the methods of java.lang.Class

shape Table

Following are the regularly using methods of java.lang.Class class.
Methods Description
getClasses() Method Gives back an array containing Class objects speaking to all the public classes and interfaces.
getClassLoader() Method To return the class loader for the class.
toString( ) Method toString( ) method is used to get the String representation of an object.
forName() Method Gives back the Class object connected with the class with the given string name.
getName( ) Method Gives back the completely qualified name of the sort indicated by this Class object, as a String.

shape Example

Following is an example to above mentioned methods. ClassDemo.java [java] package com.SPlessons; import java.lang.*; public class ClassDemo { public static void main(String[] args) { try { // returns the Class object associated with this class Class cls = Class.forName("java.lang.String"); // returns the ClassLoader object associated with this Class. ClassLoader cLoader = cls.getClassLoader(); if (cLoader == null) { System.out.println("SPlessons: The default system class was used."); } else { // returns the class loader Class loaderClass = cLoader.getClass(); System.out.println("SPlessons: Class associated with ClassLoader = " + loaderClass.getName()); } } catch (ClassNotFoundException e) { System.out.println(e.toString()); } } } [/java] The toString() method is used to get the String representation of an object that already discussed in the previous example, getName() method gives back the completely qualified name of the sort indicated by this Class object, as a String. Output: Now compile the code, the result will be as follows. [java]SPlessons: The default system class was used.[/java]

Summary

shape Key Points

  • The getResourceAsStream(String) is used to find a resource with specified name.
  • The newInstance() is used to create the new instance of class.
  • The java.lang.Object is the parent class for all the Java classes.