Java language passes all the arrays and objects by reference but will not have an
explicit pointer type.
A D V E R T I S E M E N T
You cannot construct a reference to anonymous memory. In addition
of making the programming easier, this prevents the common errors due to the pointer mismanagement.
structures and unions
The Java language do not support either structures or unions. Instead they use classes or
interfaces to build the composite types.
The Command Line Arguments
Command line arguments passed to the Java application are different in number
and in types than those passed to C or C++ programs.
Number of Parameters passed
In C and C++ when the program is invoked, the system passes two parameters to it:
These arguments are shown below.
argc--the number of arguments passed on the command line
argv-- the pointer to an array of strings that contain these arguments
When we invoke a Java application, the system passes only one parameter to it:
args-- array of Strings (it is just an array and not a pointer to an array) which contain
the arguments
First Command Line Argument
In C and C++ programming, the system passes the whole command line to the program as
its arguments, including a name used to invoke it. Consider for example, if we invoked
the C program like this: Then first argument in the argv parameter is diff.
diff file1 file2
In Java Programming, you always know the name of application because it is the name
of class where the main method is been defined. So, the Java runtime system does not
pass the class name we invoke to main method. Instead, the system passes only the those
items on the command line which appears after the class name. Consider for example, if
we invoked a Java application like this:
java diff file1 file2
The first command line argument is file1.
The Java Strings are First-Class Objects
By convention, the C and C++ strings are the null-terminated array of characters;
there is no such real entity in C and C++ that is a string. The Java strings are
first-class objects.
C Strings Behaves Unpredictably
To illustrate why this is one of the important feature of Java programming language,
let us look at the small example. This C function will copy the contents of str1 into str2.
int myStrCopy(char *str1, char *str2)
{
for ( ; *str1 != '\0'; str1++, str2++)
*str2 = *str1;
}
In the above example, the developer uses the pointer arithmetic to step through both
the strings copying one into the other. While allowing the programmers to inspect
the arbitrary memory locations through pointers is the powerful tool, this power can be
a source of many errors. One of the fruitful source of errors is pointers which stray
off the end of the array. myStrCopy function above has such an error: the for loop in
the function will not check the length of a string str2, and if the string str1 is
longer compared to str2 the string copy
writes right over the end of the string str2. Here is a program which tickles the bug.
main()
{
char *s = "HotJava is Cool!";
char t[] = "Java is Cool!";
The myStrCopy function writes over the end of string str2 thereby corrupting all that
which was stored in the memory after it. NOTE: the %s, %s are the characters that
happened to be stored in a memory location after the string str2 and will probably
be different when we run the program on our machine.
The Java Strings are Predictable
Java strings are the first-class objects deriving either from a String class or a
StringBuffer class. This makes the finding and fixing the whole class of common and
frustrating programming errors such as the one illustrated the below trivial.
Here is the program above (including an error) rewritten in the Java language.
class strcpy
{
public static void main(String args[])
{
String s = "HotJava is Cool!";
StringBuffer t = new StringBuffer("Java is Cool!");
static void myStrCopy(String str1, StringBuffer str2)
{
int i, len = str1.length();
for (i = 0; i < len; i++)
str2.setCharAt(i, str1.charAt(i));
}
}
Notice that this translation will do use the String class, the StringBuffer class and the
methods which are appropriate to obtain a specific characters instead of the
character arrays and the pointers.
Like the C version, Java programming versions of the myStrCopy method loops over
the length of the str1 and never checks length of str2. Thus, when the string str1 is
longer than the string str2, the method will try to obtain the characters beyond the
end of the end of the str2. However, when we run the Java language version, we will see
the following runtime error message.
Exception in thread "main" java.lang.StringIndexOutOfRangeException String index out of range: 13
at java.lang.Exception.< init >(Exception.java)
at java.lang.StringIndexOutOfRangeException.< init >(StringIndexOutOfRangeException.java)
at java.lang.StringBuffer.setCharAt(StringBuffer.java)
at strcpy.myStrCopy(strcpy.java:23)
at strcpy.main(strcpy.java:15)
The primary difference between the Java Programming language version of this program and
the C, is that the Java program reliably and obviously will crash, where as the C
program will do something obscure.