What differs Java from C  PDF Print E-mail
Tag it:
Delicious
Furl it!
Digg
NewsVine
Reddit
YahooMyWeb
Technorati
Articles Reviews Java
Written by Bogdan V   
Wednesday, 21 February 2007

{mos_sb_discuss:34}

Java is a lot like C, which makes it relatively easy for C programmers to learn. But there are a number of important differences between C and Java, such as the lack of a preprocessor, the use of 16-bit Unicode characters, and the exception handling mechanism. This article explains those differences, so that programmers who already know C can start programming in Java right away! 



This article also points out similarities and differences between Java and C++. C++ programmers should beware, though: While Java borrows a lot of terminology and even syntax from C++, the analogies between Java and C++ are not nearly as strong as those between Java and C.

One of the main areas in which Java differs from C, of course, is that Java is an object-oriented language and has mechanisms to define classes and create objects that are instances of those classes.

The Program Structure and Environment

A program in Java consists of one or more class definitions, each of which has been compiled into its own .class file of Java Virtual Machine object code. One of these classes must define a method main(), which is where the program starts running.

Method is an object-oriented term for a procedure or function.

To invoke a Java program, you run the Java interpreter, java, and specify the name of the class that contains the main() method. You should omit the .class extension when doing this.

The main() method that the Java interpreter invokes to start a Java program must have the following prototype:

public static void main(String args

The Java interpreter runs until the main() method returns, or until the interpreter reaches the end of main(). If no threads have been created by the program, the interpreter exits. Otherwise, the interpreter continues running until the last thread terminates.

Command-Line Arguments

The single argument to main() is an array of strings, conventionally named args or argv. The length of this array (which would be passed as the argc argument in C) is available as argv.length, as is the case with any Java array. The elements of the array are the arguments, if any, that appeared on the interpreter command line after the class name.

An Echo Program in Java

public class echo {
    public static void main(String argv[]) {
        for(int i=0; i < argv.length; i++)
            System.out.print(argv[i] + " ");
        System.out.print("\n");
        System.exit(0);
    }
}

Note that main() must be declared to return void. Thus you cannot return a value from your Java program with a return statement in main(). If you need to return a value, call System.exit() with the desired integer value.
 
Note that the handling and interpretation of this exit value are, of course, operating-system dependent. System.exit() causes the Java interpreter to exit immediately, whether or not other threads are running.

Environment

The Java API does not allow a Java program to read operating system environment variables because they are platform-dependent. However, Java defines a similar, platform-independent mechanism, known as the system properties list, for associating textual values with names.

A Java program can look up the value of a named property with the System.getProperty() method:

String homedir = System.getProperty("user.home");
String debug = System.getProperty("myapp.debug");


The Java interpreter automatically defines a number of standard system properties when it starts up. You can insert additional property definitions into the list by specifying the -D option to the interpreter:

% java -Dmyapp.debug=true myapp


User reviews

There are no user reviews for this item.

Add new review




Powered by jReviews

Last Updated ( Saturday, 07 July 2007 )
 
< Prev   Next >