Varargs Example in JAVA

import java.util.*;
import java.io.*;
class T
{
static void display(Object ... val)
{
for(Object o:val)
System.out.println(o);
}

public static void main(String args[])throws Exception
{
display();
display(45);
display(10,20.2f,30,"Hello");
display(45,43,67,23);
}
}

Pattern and Matcher Class Example

import java.util.regex.*;
class T
{
public static void main(String args[])
{
//Example of Regular Expression
Pattern p=Pattern.compile("[w|W]{3}[.][a-zA-Z0-9]+[.][a-zA-Z0-9]+");
Matcher m=p.matcher(args[0]);
System.out.println(m.matches());

m=m.usePattern(Pattern.compile("[w]{3}[.][a-zA-Z0-9]+[.][a-zA-Z0-9]+"));
System.out.println(m.matches());

//Example of Split Function
Pattern p1=Pattern.compile("[ @.]");
String str[]=p1.split(args[1]);

for(String s:str)
System.out.println(s);

System.out.println(Pattern.matches("ab", "aaaaab"));
}
}

TimeZone and its Methods in JAVA

import java.util.*;
class Tm 
{
public static void main(String[] args) 
{

TimeZone t1=TimeZone.getDefault();

System.out.println(t1);

System.out.println(t1.getID());

System.out.println(t1.getDisplayName());

System.out.println(t1.getOffset(new Date().getTime())/1000/60 + " " + "Minutes");

}
}

fill Method of Arrays Class in JAVA

import java.util.Arrays;
class Arr1
{
public static void main(String []args)
{
int a[]={1,2,3,4,5};

Arrays.fill(a,6);

System.out.println("After Arrays.fill with 2 Parameters");

for(int p:a)
System.out.println(p);

Arrays.fill(a,2,a.length,7);

System.out.println("After Arrays.fill with 4 Parameters");

for(int p:a)
System.out.println(p);
}
}

Multiple Class Initializer and Initializer Block in JAVA

Multiple Class Initializer and Initializer Block in JAVA

Today My one of the Student ask me questions as per following

1) Can we have Multiple Class Initializer and Initializer Block in One Class?
2) If Yes then What will be the Execution Sequence ?

Following Code will help you find answers.

class A 
{

//Class Initializer Block - 1 
static 
{
System.out.println("1");
}

//Class Initializer Block - 2
static
{
System.out.println("2");
}

//Initializer Block - 1 
{
System.out.println("3");
}


//Initializer Block - 2
{
System.out.println("4");
}

A()
{
System.out.println("5");
}

public static void main(String s[])
{
A a = new A();
}
}

Output :

E:\>java A
1
2
3
4
5

Object Class Functions

Object Class Functions

1) equals
2) toString
3) hashCode

class Cube
{

int h;

Cube()
{
h=3;
}

Cube(int w)
{
this.h=w;
}

}

class Rect extends Cube
{

int l,b;

Rect()
{
l=0;
b=0;
}

Rect(int i)
{
super(i);
this.l=i;
this.b=i;
}

Rect(int i, int j)
{
super(i);
this.l=i;
this.b=j;
}

Rect(int i, int j,int k)
{
super(k);
this.l=i;
this.b=j;
}

//toString() Overloading
public String toString()
{
 return " Length : "+l+" Breath : "+b;
}

//equals() overloading
public boolean equals (Object o)
{
return ((this.l * this.b)==(((Rect)o).l * ((Rect)o).b));
}

public static void main(String s[])
{
Rect r1 = new Rect(6);
Rect r2 = new Rect(12,3,6);
Rect r3 = new Rect(9,4);
Rect r4 = new Rect(6);

System.out.println(r1.equals(r2));
System.out.println(r2.equals(r3));
System.out.println(r3.equals(r1));
System.out.println(r1);
System.out.println(r2+ " " +r3);

//How to Use hashCode() Function
System.out.println(r1.hashCode());
System.out.println(r4.hashCode());
        
}
}

Anonymous Object in JAVA

Anonymous Object in JAVA

class A  {

int k;

A(int m)  {
k=m;
}

int getK() {
  return k;
}

}


class TestDemo {

public static void main(String s[])  {

System.out.println(new A(10).getK());

}

}

Watch Following Video to Learn in Detail :