TesturJavaSkills

Contents
  • Introduction


Introduction

 Need of C++

The use of structured programming language (like C) enabled the programmers to write, for the first time, moderately complex programs fairly easily. However, even with structured programming methods, once a project reaches a certain size, its complexity exceeds what a programmer can manage. By the early 1980's many projects were pushing the structured approach past its limits. To solve this problem, a new way to program was invented, called Object Oriented Programming (OOP). OOP is a programming methodology that helps organize complex programs through the use of Inheritance, Encapsulation and Polymorphism.

Reasons to prefer Java over C++

  • Inconsistent String Use: C++ is hampered by legacy support of C standard libraries that use char* instead of the standard string class. This requires calls to c_str() every time a char* is needed. Java has no such legacy burdens, and all libraries use String consistently when dealing with strings.

  • Easier debugging due to complete stack traces with line numbers.

  • No mysterious failures or randomly changing variables due to array overruns, mismatched object files, and other pointer problems.

  • No memory model means that code that multithreaded code that works on one system may not work on another. Java, by contrast, has a well-defined memory model so that you are guaranteed that code that works on one platform will work on another, even if that platform has a different hardware and operating system.

  • In C++ it is possible to compile code that does comparisons on iterators that fails at runtime. The "<" operator is sometimes legal, sometimes not. Java sidesteps this whole minefield by making the Iterator interface safe: the hasNext() method returns a simple, safe boolean that cannot be used erroneously.


Need of Java

Java is an Object oriented language. The Syntax of Java is similar to C++, but Java avoids Pointers, multiple inheritance, goto statement and operator overloading. It provides efficient features to handle these issues. Features of C++ that slow down application development cycle have been omitted in Java, like Java has a garbage collector, so unlike C++ in Java we don't need to deallocate the memory, or worry about memory fragmentations.


Parts of Java

Sun Microsystems Inc., has divided Java into 3 parts as follows:

1. Java SE: It is the Java Standard Edition that contains basic core Java classes. This edition is used to develop standard applets and applications.

2. Java EE: It is the Java Enterprise Edition and it contains classes that are beyond Java SE. Infact, we need Java SE in order to use many of the classes in Java EE. Java EE. Java EE mainly concentrates on providing business solutions on a network.

3. Java ME: It stands for Java Micro Edition. Java ME is for developers who develop code for portable devices, such as a PDA or a cellular phone. Code on these devices needs to be small in size and should take less memory.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Setting the Classpath in WINDOWS

After installing the Java software jdk1.5.0 in C:\Program Files\ follow the below steps:


Mycomputer--Properties--Advanced--Environmentalvariables:
 
 {FOR WORKING JAVA SET THE PATH LIKE BELOW}
 
 Variablename: JAVA_HOME                
 Variablevalue: C:\Program Files\Java\jdk1.5.0
 
 variablename: CLASSPATH
 Variablevalue: C:\Program Files\Java\jdk1.5.0\bin;.
 
 Variablename: CATALINA_HOME
 Variablevalue:  ..


 
SYSTEMVARIABLES
 
EDIT-PATH
PATH;C:\Program Files\Java\jdk1.5.0\bin;   



1.  What is the difference between a class and an object?                                                                              

Sol: A class is a model for creating objects and does not exist physically. An object is anything that exists physically. Both the class and objects contain variables and methods.

2.  What is the difference between object oriented programming languages and object based programming languages?

Sol: Object oriented programming languages follow all the features of Object Oriented programming System (OOPS). Smalltalk, Simula-67, C++, Java are examples.
         Object based programming languages follow all the features of OOPS, except Inheritance. For eg., JavaScript and VBScript will come under object based programming languages.
 

2.  A Simple Java program: Example.java                                                                                

/* This is a simple java program
To compile:   “javac Example.java"
To run:   “java Example  */
class Example {
//program begins with a call to main()
    public static void main(String args[ ])
    {
        System.out.println("hello world");
    }
}
Note: In Java source file is officially called a 'compilation unit'. This is a text file that contains one or more class definitions.
In Java all the code must reside in a class. By convention, the name of that class should match with the name of the file that holds the program. Java is case-sensitive.

Compiling the program:

c:\>javac Example.java

Java compiler creates the file called Example.class that contains the byte code version of the program.
To run the program: You must use java interpreter, called java. Java byte code is the intermediate representation of your program that contains the instructions the java compiler will execute

c:\>java Example (output-Result of run)
c:\>java -p Example (to see the byte code)

In java, all program activity occurs within xxxe. This is one reason why all java programs are (at least a little bit) object oriented. All java applications begin execution by calling main().
args[]: Receives any command line arguments present when the program is executed.
String: Stores character string.
System.out.println("---------");
System: Is a predefined class that provides to the access to the system
out: Output stream that is connected to the console.
println: Displays the string which is passed to it. It belongs to PrintStream class. So, we should call println() by creating an object to PrintStream class as:

PrintStream obj. println("welcome to Java");
But as it is not possible to create the object to PrintStream class directly, an alternative is given to us, i.e., System.out. Here, System is the class name and out is a static variable in System class. out is called a field in System class. When we call this field, a PrintStream class object will be created internally.

void: Simply tells the compiler that main() does not return a value.


2. Java documentation Comments:

These comments start with /** and end with */. These comments are used to provide description for every feature in a Java program. This description proves helpful in the creationof  .html file called API(Application Programming Interface) document. Java documentation comments should be used before every feature in the program as shown here:

/** description about package */                                            package packagename

package packagename                                                            description about package                                                                                                                                                                       ---------------------------------------

/** description about a class */                                               class classname                                   

class classname                                                                        description about a class

{                                                                                                ---------------------------------------

            class code;

/** description about a method */                             

            void methodname()

            {                                                                                    void methodname

                        method code                                                     description about a method

            }                                                                                    -------------------------------------

}

                Java program                                                                      API documentation

generate API documentation for ur Java program using the command:
            javadoc <filename>.java




Subpages (3): Advanced Java Core Java J2EE