Our social:

Latest Post

Tuesday, 29 December 2015

Why main() has public static void syntax

The Reason behind

public static void main(String[] arg){}

In the above statement public static void main(String[] arg){} is prototype that is provided by JVM. This static method is given to programmer to write their program execution flow logic like which class we are going to load first, which method execution requires first.  By using main method we are requesting to JVM , hy!!!! jvm you start my program execution in the given fashion.

Now here one question arises that why Main method?
Actually main method is not user defined method, this method is predefined and given by jvm. By following thier prototype we are writing our logic here.
Then another question is why main method has public static void main(){} syntax.

why public?
public is because our program executed by jvm and we all know jvm resides outside of our class and package. In case we define private and protected then, the scope of main() method will be restricted and can not be accessed outside of the package or class will not be unable to execute or load in JVM. that's why we should write main() as public.

why static?
we are using main() as static, why because to execute our program we need some memory in jvm. And as we know that static is special keyword that provide memory of a method at class loading time. So at the time of public class or main method class loading jvm will provide memory to main method. Once main() method gets memory then it will starts execution of our program sequentially.

why void?
main()method not returning any values and there is also no need to return any values. It is only use to execute programs that's why it is declared as void.

NOTE:this is my analyzation and gathered from internet, if i am wrong on any point please correct me through comment then i will try to modify...
                                     Thank you(Smartyy Sunny)

Friday, 25 December 2015

Growable Array Alogrithm programming Example

Tags: How To Create Growable Array, How To Insert Element In Array,How To Insert Add In Array,How To Remove Element In Array,How To Replace Element In Array, Growable Array Algorithm Program.


This is growable Array algorithm program that is able to slove size limitation and type limitation     problem of Array. In this progran we can increase array size if required dynamically" increaseCapacity()" method is responsible to do this work. Here various methods are defined to insert, add, remove and replace to modifying array element


import java.io.*;
class ArrayCollection
{
private Object[] arrayObj=new Object[5];
private int index=0;
public int capacity(){
return arrayObj.length;
}

public int size(){
return index;
}

//Add elements to array
public void add(Object ele){
if(this.size()==this.capacity()){
increaseCapacity();
}
this.arrayObj[index]=ele;
index++;
}

//increase capacity method for arrayObject 

public void increaseCapacity(){
int newCapacity=this.capacity()+(this.capacity()/2);
Object[] tempObj=new Object[newCapacity];

for(int i=0;i<arrayObj.length; i++){
tempObj[i]=arrayObj[i];
}
arrayObj=tempObj;
}

//retriving element from collection

public Object get(int index){
if(index<size()){
return arrayObj[index];
}else{

throw new ArrayIndexOutOfBoundsException(""+index);
}
}

//replacing logic in replace method

public void replace(int index, Object ele){
if(index>=this.size()){
throw new ArrayIndexOutOfBoundsException(""+index);
}else{
this.arrayObj[index]=ele;

}
}

//code removing elements from collection

public void remove(int index){
if(index >=this.size()){
throw new ArrayIndexOutOfBoundsException(""+index);
}else{
for(;index<this.size()-1; index++){
arrayObj[index]=arrayObj[index+1];
}
arrayObj[size()-1]=null;
index--;
}
}

//inserting values in Collection

public void insert(int index, Object ele){
if(index>=size()){
throw new ArrayIndexOutOfBoundsException(""+index);
}
if(size()==capacity()){
increaseCapacity();
}
//System.out.println("hiii");
for(int i=size();i>=index;i--){
arrayObj[i+1]=arrayObj[i];
}
arrayObj[index]=ele;
this.index++;
}
// this section for testing purpose........
public static void main(String[] args) 
{
ArrayCollection ar=new ArrayCollection();
System.out.println("Capacity of this array "+ar.capacity());
System.out.println("Size of this array "+ar.size());

ar.add(1);
ar.add(2);
ar.add(3);
ar.add(4);
ar.add(5);
System.out.println("Size of this array "+ar.size());
//System.out.println("value "+ ar.get(2));
//ar.replace(2,8);
//System.out.println("after replacing index 2 value is"+ ar.get(2));
//ar.remove(2);
//System.out.println("after removing index 2 value is"+ ar.get(2));
//System.out.println(ar.size());
ar.insert(2,9);
System.out.println(ar.get(2));
System.out.println(ar.capacity());
System.out.println(ar.size());
}
//you can also test program according to requirment....
}

You can execute this program directly by just copy from here and paste in notepad and save this file with class name with .java extension. And then simply execute from command prompt...
                                                                                    Thank you(Smartyy Sunny)
                                                                                                         Happy Coding......

Thursday, 17 December 2015

Can we typecast implicit variable "this" and "super" if yes then how, if no then why?

:Yes we can typecast implicit variable "this". this variable is a special referenced type variable that is used to denote current object class reference. It will internally stores the value of current object class reference.
code snapshop:


Now our question is if yes the how it is possible.....
Simple, we have a class called  A that bydefault extending Object class so it is good to take this example..Now i have created an object of class A and i want to store class A type variable value in Object type reference variable.
A a=new A();
Object obj1=(Object)this;
here this variable contains A class information  and that is going to be store in obj1 that is of Object type.
so in this case we are able to typecast implicit variable "this"
full code is looks something like this:

------------------------------------------------------------------------------------------------------------------------
class Atest 
{
void m1(){
Object obj1=(Object)this;//here obj1 is Object type super class ref variable 
                        //and we are passing this variable value(subclass type) to super class
System.out.println(obj1.getClass());
}
public static void main(String[] args) 
{
Atest a1=new Atest();
a1.m1();
}
}

According to my analyzation "super" variable type casting is not possible.....
why, i will post in next post..........

                                                                                                             Thank you(Smartyy Sunny)