JVM,JRE,Java Compiler FAQs-1
1)How can I write a program that takes command line input?
A: Java programs that take input from the command line declare a special static method called main
, which takes a String
array as an argument and returns void. The example program below loops through any arguments passed to the program on the command line and lists their values.
2)What does public static void main(String[])
mean?
A: This is a special static method signature that is used to run Java programs from a command line interface (CLI). There is nothing special about the method itself, it is a standard Java method, but the Java interpreter is designed to call this method when a class reference is given on the command line, as below.
3)Why are command line arguments passed as a String
?
A: Command line arguments are passed to the application’s main method by the Java runtime system before the application class or any supporting objects are instantiated. It would be much more complex to define and construct arbitrary object types to pass to the main method and primitive values alone are not versatile enough to provide the range of input data that strings can. String arguments can be parsed for primitive values and can also be used for arbitrary text input, file and URL references.
4)Why doesn’t the main
method throw an error with no arguments?
A: When you invoke the Java Virtual Machine on a class without any arguments, the class’ main
method receives a String
array of zero length. Thus, the method signature is fulfilled. Provided the main method does not make any reference to elements in the array, or checks the array length before doing so, no exception will occur.
5)Why do we only use the main
method to start a program?
A: The entry point method main
is used to the provide a standard convention for starting Java programs. The choice of the method name is somewhat arbitrary, but is partly designed to avoid clashes with the Thread
start()
and Runnable
run()
methods, for example.
6)Can the main
method be overloaded?
A: Yes, any Java method can be overloaded, provided there is no final
method with the same signature already. The Java interpreter will only invoke the standard entry point signature for the main
method, with a string array argument, but your application can call its own main
method as required.
7)Can the main
method be declared final?
A: Yes, the static void main(String[])
method can be declared final.
8)I get an exception if I remove the static
modifier from main
!
A: The static void main(String[])
method is a basic convention of the Java programming language that provides an entry point into the runtime system. The main
method must be declared static because no objects exist when you first invoke the Java Virtual Machine (JVM), so there are no references to instance methods. The JVM creates the initial runtime environment in which this static method can be called, if you remove the static
modifier, it will throw a NoSuchMethodException
.
9)How can the static main
method use instance variables?
A: For very simple programs it is possible to write a main
method that only uses static variables and methods. For more complex systems, the main
method is used to create an instance of itself, or another primary class, as the basis of the application. The primary application object reference uses instance methods to create and interact with other objects, do the work and return when the application terminates.
public class SimpleClass { public void doSomething() { // Instance method statements } public static main(final String[] args) { SimpleClass instance = new SimpleClass(); instance.doSomething(); } }
10)main method from another class?
String[] arguments = new String[] {"123"}; MaxFactors.main(arguments);
11)What is the source code for the compiler?
A: The source code for a Java program is also known as a compilation unit, which contains the code for a top level Java class or interface. A Java compilation unit is usually created in the form of a file with a .java
extension and is passed to the compiler as a file path reference.
The Java source file contains a header that declares the type of class or interface, its “visibility” with respect to other classes, its name and any superclass it may extend, or interface it implements. The body of the class contains variable declarations and methods that define the behaviour of the class, and any constructors used to create an instance of the class. A compilation unit may also contain nested inner classes.
12)Why is the source file named after the class?
A: The Java source file naming convention is not a standard specified by the Java language but is a common feature of Java compilers, such as javac
, to help locate source code. The source content of a Java class is known as a compilation unit. By storing the compilation unit in a file that is named after the class, the compiler can locate any supporting classes by name and compile those too.
This convention also extends to package names. Most Java compilers expect source code to be stored in directories whose names match their package hierarchy. Thus the source code for a class named Example
in the package com.domain.util
might be stored in a file with the path c:\src\com\domain\util\Example.java
13)Which class should be compiled first?
A: Sometimes you will find that trial and error will give you the answer you need. If you are using the Sun compiler, javac
, the compiler will compile any other classes that your target class depends on, provided the other source files are in the same directory hierarchy as the first and the sub-directory names reflect the package hierarchy of the classes.
14)What are the steps in compiling a class?
Once you have written the Java source code file, there is only one step required to compile it. To compile the class from the command line, you need to give the path to the compiler program, such as Sun javac
, and the path of the source code file, like this:
15)Where is my compiled class file?
A: If you are sure your class is being compiled, then the class file should be output somewhere! Without any directory argument, your compiler should place the class file in the same directory as your source file. Use the output directory argument to specify where the class files are generated.
16)How can I ensure my compiler will locate the SAX package?
A: One way to ensure your compiler can locate any package it may require is to pass its path to the compiler explicitly using the -classpath
argument.
17)What does this deprecation message mean?
A: The deprecation message you have seen means that the methods you are calling have been marked with a JavaDoc deprecation comment. When a method or class is marked deprecated it is only advisory, not mandatory, but the advice is given for good reason and should be followed. So long as deprecated methods remain in the public API it is possible to use them, so this approach supports legacy code and gives developers time to amend their applications as necessary.
18)What are undefined and undeclared variables?
A: One gets warning messages about undefined and undeclared variables when compiling Java classes that have programming errors, as in the example below.
19)Why doesn’t the compiler warn about stack overflow problems?
A: There are many cases of poor runtime programming that could potentially be identified at compile time, but the number and subtlety of the cases gets increasingly difficult to address. The main purpose of a compiler is to produce executable byte code that is valid according to the rules of the programming language, not to guard against poor programming. Hence most compilers only validate the syntax of the language and the most obvious logical errors in the code at compile time.
20)How do I set environment variables on Windows XP?
A: For Windows 2000/XP systems, the environment settings are edited in a special Control Panel applet called System.
21)How do I set the compiler path?
A: The Java compiler is a program like any other and your operating system needs to know where to find the executable file. The simplest way to do this is to give the full path to the Java compiler in the command, as below for Windows…
22)How do I configure EditPlus to compile Java and capture output?
A: These instructions on configuring the NSGMLS markup validator for EditPlus will help you get started. For Java, the Command field will be the path to your javac.exe
program (or java.exe
to run). The Argument should contain any parameters you want to pass to the compiler and should end with the $(FilePath)
variable that substitutes the current file name. Check the Capture output box to get feedback from the compiler.