Program No. 1
class A
{
String name;
int rollno;
A(String name, int rollno)
{
this.name = name;
this.rollno = rollno;
}
public String toString()
{
return name + " " + rollno;
}
}
class M1
{
public static void main(String[] args)
{
A a1 = new A("vijay", 101);
A a2 = new A("adam", 102);
System.out.println(a1);
System.out.println(a2);
}
}
o/p:
vijay 101
adam 102
Program No. 2
import java.lang.reflect.*;
class M2
{
public static void main(String[] args) throws Exception
{
int count = 0;
Class c = Class.forName("java.lang.Object");
Method[] m = c.getDeclaredMethods();
for(Method m1 : m)
{
count++;
System.out.println(m1.getName());
}
System.out.println("The number of methods:" + count);
}
}
output:
finalize
wait
wait
wait
equals
toString
hashCode
getClass
clone
notify
notifyAll
The number of methods:11
Program No. 3
class E
{
int i;
public String toString()
{
return "" + this.i;
}
}
class M3
{
public static void main(String[] args)
{
E e1 = new E();
e1.i = 10;
E e2 = new E();
e2.i = 20;
System.out.println(e1);//address of the object instead of content
System.out.println(e2);//hexadecimal repr of mem address
}
}
output:
10
20
Program No. 4
class F
{
int i;
public String toString()
{
return "its a type object with i value as: " + i;
}
}
class M4
{
public static void main(String[] args)
{
F f1 = new F();
f1.i = 20;
System.out.println(f1);
}
}
output:
its a type object with i value as: 20
Program No. 5
class G
{
int i;
public String toString()
{
return "its a type object with i value as :" + i;
}
}
class M5
{
public static void main(String[] args)
{
G g1 = new G();
g1.i = 20;
System.out.println(g1);
String s1 = "description: " + g1;
System.out.println(s1);
}
}
output:
its a type object with i value as :20
description: its a type object with i value as :20
Program No. 6
class B
{
int i, j;
B(int k, int l)
{
this.i = k;
this.j = l;
}
public String toString()
{
return i + ":" + j;
}
}
class M6
{
public static void main(String[] args)
{
B b1 = new B(10, 20);
System.out.println(b1);
}
}
output:
10:20
Program No. 7
class C
{
int i;
public String toString()
{
return "i = " + i;
}
}
class D
{
int j;
C c_ref;
public String toString()
{
return "j = " + j + ", " + c_ref;
}
}
class M7
{
public static void main(String[] args)
{
C c1 = new C();
c1.i = 10;
D d1 = new D();
d1.j = 20;
d1.c_ref = c1;
System.out.println(c1);
System.out.println(d1);
}
}
output:
i = 10
j = 20, i = 10
Program No. 8
import java.util.ArrayList;
class M8
{
public static void main(String[] args)
{
String s1 = "Hello";
Integer obj = 10;
Thread t1 = new Thread();
ArrayList list = new ArrayList();
System.out.println(s1);
System.out.println(obj);
System.out.println(t1);
System.out.println(list);
}
}
output:
Hello
10
Thread[Thread-0,5,main]
[]
Program No. 9
output:
Program No. 6
output:
Program No. 6
output: