Arrays Can Be Jagged
In languages like C and C++, each subarray of a multidimensional array must have
the same dimensions. In Java and C# arrays do not have to be uniform because jagged
arrays can be created as one-dimensional arrays of arrays. In a jagged array the
contents of the array are arrays which may hold instances of a type or references to
other arrays. For this reason the rows and columns in a jagged array need not have
uniform length as can be seen from the following code snippet:
int [][]myArray = new int[2][];
myArray[0] = new int[3];
myArray[1] = new int[9];
No Global Methods
Interfaces, Yes. Multiple Inheritance, No
String
C# has a System.String class which is analogous to the java.lang.String class. Both
classes are immutable meaning that the values of the strings cannot be changed once
the strings have been created. In both instances methods that appear to modify the
actual content of a string actually create a new string to return, leaving the original
string unchanged. Thus the following C# and Java code does not modify the string in
either case
C# Code
String csString = "Apple Jack";
csString.ToLower(); /* Does not modify string, instead returns
lower case copy of string */
Java Code
String jString = "Grapes";
jString.toLowerCase(); /* Does not modify string, instead returns
lower case copy of string */
To create a string-like object that allows modification in C# it is advisable to use the
System.Text.StringBuilder class whereas in Java one would use the
java.lang.StringBuffer class.
NOTE: In C#, the string class can either be written as string or String.
Unextensable classes
Both Java and C# provide mechanisms to specify that a class should be the last one in
an inheritance hierarchy and cannot be used as a base class. In Java this is done by
preceding the class declaration with the final keyword while in C# this is done by
preceding the class declaration with the sealed keyword. Below are examples of
classes that cannot be extended in either language
eg.
C#
sealed class X{}
Java
final class X{}
Exceptions in C# and Java share a lot of similarities. Both languages support the use
of the try block for indicating guarded regions, the catch block for handling thrown
exceptions and the finally block for releasing resources before leaving the method.
Both languages have an inheritance hierarchy where all exceptions are derived from a
single Exception class. Exceptions can be caught and rethrown after some error
handling occurs in both languages. Finally, both languages provide a mechanism for
wrapping exceptions in one another for cases where a different exception is rethrown
from the one that was caught. An example of using the exception wrapping capability
is a three tier application where a SQLException is thrown during database access but
is caught, examined, then an application specific exception is thrown. In this scenario
the application specific exception can be initialized with the original SQLException
so handlers of the application specific exception can access the original exception
thrown if needed. Below are two equivalent code samples that show the similarities
between exceptions in both languages.
NOTE: Although exceptions in both languages support methods for getting a stack
trace, only Java exceptions have methods that allow one to alter the stack trace.
No comments:
Post a Comment