Wednesday, October 26, 2005

Java Class Loading

Learn it here and here

Array Initialization

class MyHello {

public static void main(String[] args) {

A[] arr=new A[5];//Creating array of references of type class A (null values by default)

// arr[1]=new A();//assigning new object to arr[1] reference variable(overrides null value)

System.out.println(arr[1].a);

}

}

class A{

int a;

}

// O/p ==> java.lang.NullPointerException

/*

The program declares the variable and instantiates the object array to a size of 5. While the array variable refers to a valid instance of the array. The contents of the array which should refer to instances of the objects of the type declared, were never created/initialized. An array is simply a collection of references the references may or may not refer to a valid instance on the object of the collection type. As a result, if it's possible that you will get unitialized instances in your array/collection it's usually a good idea to check that the item is not null before you make any method calls on it.

Whenever you are using an array of objects you usually go through a 3 step process where you declare it, instantiate it and you fill it with instances of the type it was declared to hold.

*/

Wednesday, October 19, 2005

Is Java a “Specification” (or) “Language” ?

Sun likes to call Java a "platform", which includes several components: the Java Language, the Java Virtual Machine, and the Java Standard Edition Platform Libraries. Yes, it's a specification rather than an implementation. Some languages are defined by a single implementation -- these tend to be languages with complex but informally described runtime support like Python, Perl, and Ruby. Most "important" languages are defined instead by a specification or standard: and C, C++, and Java are among these. This means that there can be multiple implementations.

The Java language is defined by the Java Language Specification. It's like asking whether something that looks like a horse is a horse or a mammal. It's both!

Java HAS-A specification like every other computer language. Java IS-A computer language. Java is accompanied by a rich set of standard libraries, written in Java. Java is usually executed on a Java virtual machine.

Core Java - 30 Question Test

Java Test

Java Answers


Polymorphism

class ValHold{

public int i = 10;

}

public class ObParm{

public static void main(String argv[]){

ObParm o = new ObParm();

o.amethod();

}

public void amethod(){

int i = 99;

ValHold v = new ValHold();

v.i=30;


another(v,i);

System.out.print( v.i );

}//End of amethod


public void another(ValHold v, int i) {

i=0;

v.i = 20;

ValHold vh = new ValHold();

v = vh;

System.out.print(v.i);

}//End of another

}