How to write a Program in Java

How to write a Program in Java?
1.What is API document?
A.An API document is a .html file that contains description of all the features of a software ,a program or a technology.API document is helpful for the user to understand how to use the software or technology.

2.What is the importance of main method in Java?

main() method is the entry point of any standalone java application. The syntax of main method is public static void main(String args[]).
 main method is public and static so that java can access it without initializing the class. The input parameter is an array of String through which we can pass runtime arguments to the java program.

3.What is classloader?

The classloader is a subsystem of JVM that is used to load classes and interfaces.There are many types of classloaders e.g. Bootstrap classloader, Extension classloader, System classloader, Plugin classloader etc.

4.What is the difference between import and #include statements ?
 #include directive makes the compiler go to the C/C++ standard library and copy the code from the header files into the program. As a result, the program size increases, thus wasting memory and processor’s time.
import statement makes the JVM go to the Java standard library, execute the code there , and substitute the result into the program. Here, no code is copied and hence no waste of memory or processor’s time.hence import is an efficient mechanism than #include.

5.What happens if String args[] is not written in main() method?
A.When main () method is written without String args[] as:
         public static void main()
the code will compile but JVM cannot run the code because it cannot recognise the main() method as the method from where it should start execution of the java program .Remember JVM always looks for main() method with string type array as parameter.

6.What is the difference between print() and println() method?
A.Both methods are used to print data on console, the only difference between above two methods is 'System.out.println' prints data and place the cursor in next line. So the next coming output prints in the next line whereas 'System.out.print' prints data and place the cursor in the same line. So the next coming output prints in the same line.

7.What is the  Structure of a java program?

A:  

//comments

Package statement
import statement
class<Class Name>{
variables
methods
public static void main(String[]args){
Statements
}
}

8.What is difference between path and classpath variables?
PATH is an environment variable used by operating system to locate the executables. That’s why when we install Java or want any executable to be found by OS, we need to add the directory location in the PATH variable. If you work on Windows OS.
  Classpath is specific to java and used by java executables to locate class files. We can provide the classpath location while running java application and it can be a directory, ZIP files, JAR files etc.

9.How to set the java classpath in Windows?
A. In order to set Classpath for Java in Windows (any version either Windows XP,  Windows 2000 or Windows 7) you need to specify value of environment variable CLASSPATH, name of this variable is not case sensitive and it doesn’t matter if name of your environment variable is Classpath, CLASSPATH or classpath in Java.

Here is Step by Step guide for setting Java Classpath in Windows:
i.Go to Environment variable window in Windows by pressing "Windows + Pause “-->Advanced -->Environment variable " or you can go from right click on my computer than choosing properties and then Advanced and then Environment variable this will open Environment variable window in windows.
ii.Now specify your environment variable CLASSPATH and put the value of your JAVA_HOME\lib and also include current directory by including (dot or period sign).
iii.Now to check the value of Java classpath in windows type "echo %CLASSPATH" in your DOS command prompt and it will show you the value of directory which are included in CLASSPATH.

You can also set classpath in windows by using DOS command like :
set CLASSPATH=%CLASSPATH%;JAVA_HOME\lib;
This way you can set classpath in Windows XP, windows 2000 or Windows 7 and 8, as they all come with command prompt.

10.What are the different java popular Editors?

A.To write your Java programs, you will need a text editor. There are even more sophisticated IDEs available in the market. But for now, you can consider one of the following:

Notepad: On Windows machine you can use any simple text editor like Notepad (Recommended for this tutorial), TextPad.
Netbeans:is a Java IDE that is open-source and free which can be downloaded from http://www.netbeans.org/index.html.
Eclipse: is also a Java IDE developed by the eclipse open-source community and can be downloaded from http://www.eclipse.org/.

11.How to save and Execute a java program?
 Step1:After writing the java program we can save tha program.A java program can have any name but execution of the java program must be ".java".
                 Ex:First Program.java
 Step2:Compiling the java program.To compile a java program we require a command prompt and we use "javac"command.
                 Syntax:javac ProgramName/FileName
                  Ex:javac Firstprogram.java
When java program is compiled .javafile (sourcecode)will be converted to .classfile(byte code)
Step3:Executing a java program.To execute a java program we require a command prompt and we use java command.
               Syntax:java ClassName
                Ex:java FirstProgram.

12.Which class is the superclass of all classes?
A.  java.lang.Object is the root class for all the java classes and we don’t need to extend it.

13.What is difference between object oriented programming language and object based programming language?

A. Object based programming languages follow all the features of OOPs except Inheritance. Examples of object based programming languages are JavaScript, VBScript etc.

0 comments:

Post a Comment