OCJP(Oracle Certified Java Program)



Totel:37 Click: 1 2 3 4 5 6 7 8
SCJP1.5 Interview Questions And Answers

Page 1

Command Line Argument is working similar as all other arrays. The main method is called firstly one the JVM invokes , it must be take a String array parameter.

class TestMain {
public static void main (String [] args) {
System.out.println("First arg is " + args[0]);
}
}

When we invoked command line :
%java TestMain Hello
output : First arg is Hello

main imp thing must be in mind when we use the command line argument .

  1. The length of the args array will always be equal to the number of command-line arguments.
  2. args.length is one, meaning there is one element in the array, and it is at index zero.
  3. length-1, you'll get an ArrayIndexOutOfBoundsException.

The following code is an example of a main method expecting three arguments :

public static void main (String [] args) {
if (args.length < 3) {
System.out.println("Usage: [name] [social security #]
//[IQ] Try again when you have a clue");
}
}

Local arrays basically similar as a object reference,when we declared any array with in a method must be assigned a value before using it.

It means we must declare the array and constructed it In the Local array the most imp thing is array elements are given their default values (0, false, null, u0000, etc.) regardless of whether the array is declared as an instance or local variable.

Objects basically also behave differently when It is declared within a method rather than as instance
variables.

When its with instance variable object references we can got that time its with leaving an object reference uninitialized,and also that time when we reach the code its not null.

But here most imp thing is a null reference is not the same as an uninitialized reference. we have a example where compiler complain about the following code :

import java.util.Date;
public class TimeTravel {
public static void main(String [] args) {
Date date;
if (date == null)
System.out.println("date is null");
}
}


When it is complile we got these type of errors :
%javac TimeTravel.java
TimeTravel.java:5: Variable date may not have been initialized.
If (date == null)
1 error
&

Local Primitive is basically for the time travel simulator,We have code in which year is a integer which is signifies as an automatic variable because it is with in the curly braces of a method.

public class TimeTravel {
public static void main(String [] args) {
int year = 2050;
System.out.println("The year is " + year);
}
}

We know that Array is a type of object. its also declared but not explicitly initialized for the value of Null.just as other object reference instance variable.Here most imp line is " Array elements are always always always given default values, regardless of where the array itself is declared or instantiated".


When we would like to initialize an array and that time object reference elements value is equal to null.

public class BirthDays {
static int [] year = new int[100];
public static void main(String [] args) {
for(int i=0;i<100;i++)
System.out.println("year[" + i + "] = " + year[i]);
}
}


Goto Page:

1 2 3 4 5 6 7 8