C and Java are very different languages. However, there is enough similarity that people might be tempted into reusing existing C code in their Java programs.
This discussion is not designed to help people convert entire programs. Rather it is aimed at those cases where a few functions are needed from an existing C program.
No attempt will be made here to exhort people to rewrite applications from the ground up using object oriented methods. Or even to use object oriented concepts at all. We are purely concerned with getting existing C code working in a Java environment.
The ordering of content in this analysis follows the ISO C Standard, "ISO/IEC 9899:1999 Programming Languages -- C". In the interests of brevity the more esoteric differences have been omitted (in some cases a more detailed analysis is provided here (a 10M pdf)).
The reference used for the Java language was "The Java language specification" by James Gosling, Bill Joy, and Guy Steele, ISBN 0-201-63451-1.
Java programs are intended to give the same result under all implementations.
Any C constructs whose behaviour is undefined, implementation defined, or unspecified will need to be checked. See Annex G of ISO 9899 for a list of these constructs.
Java: "Variables have type, objects have classes"
Java goes to a lot of trouble to make sure that use of a variable or object before a value is assigned to it results in a compile time error. Chapter 16 is devoted to the topic of Definite Assignment.
Java, phase 1: "A translation of Unicode escapes (3.3) in the raw stream of Unicode characters to the corresponding Unicode character. ..."
C, phase 3: "... Each comment is replaced by one space character. ..."
Java is silent on the status of comments.
C, phase 5: "Each source character set member and escape sequence in character constants and string literals is converted to a member of the execution character set."
Java has escape sequences, but is silent on this point.
C, phase 6: "Adjacent character string literal tokens are concatenated and adjacent wide string literal tokens are concatenated."
Java does not concatenate adjacent string literal tokens. The + operator may be used to concatenate strings at runtime.
Java: "The method main must be declared public, static
and void. It must accept a single argument that is an array
of strings."
ie public static void main(String[] args) { /*...*/ )
Java uses 16-bit Unicode to represent characters.
\a
\v
No such minimum limits are given for Java translators.
In Java the classes Character, Integer and Long provide public fields that give information on the maximum and minimum values of integral quantities. Note: there is no class giving information on the short type.
In Java the classes Float and Double provide fields giving some of this information.
C: "... parameters are used to define the model for each floating-point type."
Java: "The Java types float and double are IEEE 754 32-bit single-precision and 64-bit double-precision binary floating-point values, respectively."
Java reserves the keyword goto. However, the goto statement is not supported in Java.
In Java the field modifier static does not affect the visibility of its associated field.
Java: "The meaning of a name in Java depends on the context in which it is used."
C has a number of name spaces:
Java has primitive types and reference types.
C: "An object declared as type char is large enough to store any member of the basic execution character set."
Java: "... and char, whose values are 16-bit unsigned integers representing Unicode characters."
C has signed and unsigned integral types.
Java: "The integral types are byte, short, int and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing Unicode characters."
Java has no long double type.
Java has no enumeration types.
Java has no void type.
Java has no union type.
Java does not have pointer types, it has reference types.
C: Arithmetic types -> Java: Numeric types
C: Arithmetic types + pointer types -> Scalar types
Java: Numeric types + boolean type -> Primitive types
Java has no notion of composite type.
In Java all unsuffixed integer constants have type int. Integer constants ending in the suffix l, or L have type long.
In Java any integer constant greater than 2147403647, or its octal or hexadecimal representation, must have a suffix appended to it.
Java does not support unsigned types (although char types only store values between 0 and 65535).
\?
\a
\v
In C character constants have type int.
In Java they have type char.
C allows implementations to support more than one character in a character constant.
Java requires that a character constant contain a single character.
Java does not implement wide characters using C's notation of L'a'.
\?
\a
\v
In C string literals have type array of char.
In Java they have type String, a class type.
In C adjacent string literals are concatenated in translation phase 6.
Java requires the use of the + operator to concatenate strings.
In C a null terminator is added to the end of string literals.
In Java no terminator is added to string literals.
[ ] -- Java regards these characters as punctuators, which C does also.
( ) -- Java regards these characters as punctuators, which C does also.
. -- Java regards this character as a punctuator.
->
sizeof
, -- Java regards this character as a punctuator, which C does also.
#
##
*
=
: -- Possibly an oversight in the Java specification, case 3:
#
...
In Java a variable of type char holds "... values are 16-bit unsigned integers representing Unicode characters." byte is an 8-bit signed integral type that is nearer to C's programmers use of the char type.
In C, if a value is demoted to a smaller integer type and the value cannot be represented the result is implementation defined.
Java defines the behaviour as discarding all but the lower bits.
Java requires rounding towards zero.
C code that relies on subtle behaviour caused by unsigned int to long conversion may cause conversion headaches.
In Java no such conversions happen.
In Java the expression null (which is also a keyword) has the null type. Java says nothing about the representation of the value of the expression null. The type of this expression is not given any name, although is spoken terms it is referred to as the null type.
Java specifies a left to right evaluation order.
In C bitwise operations on signed types have implementation defined behaviour (because the representation is implementation defined).
Java defines the behaviour (because the representation is defined)..
In Java an array type is required.
In C the index "... shall have integral type, ..."
Java: "Arrays must be indexed by int values; short, byte, or char values may also be used as index values because they are subject to unary numeric promotion (5.6.1) and become int values. An attempt to access an array component with a long index value results in a compile-time error."
In C arrays of char (strings) may be indexed using this operator.
In Java String is a class, the method charAt may be used to access elements. Objects of this class are not modifiable, and so may not be assigned to. However, the StringBuffer type is modifiable (the method setChatAt may be used to set an element).
Easy to understand differences:
Java does not support the -> operator.
Java does not support typedef's, or the concept it provides. Only the primitive types, arrays of those types, or references to those types may appear as type-name (class names may also be used, but C does not have them).
Java does not permit numeric types to be cast to reference types, or vice versa.
C: "... whether the result of the / operator is the largest integer less than or equal to the algebraic quotient or the smallest integer greater than or equal to the algebraic quotient is implementation defined, ..."
Java: "Integer division rounds towards 0."
In C if either operand of the remainder, %, operator is negative the sign of the result is implementation defined (a consequence of the behaviour for /).
Both languages require (a/b)*b+a%b to equal a.
Java does not allow references to be subtracted.
In Java the >> operator performs sign extension. The >>> operator shifts with zero extension.
In C the result of this operator has int type.
In Java the result has boolean type.
In Java the result has boolean type.
In C the result has type int.
In Java the result has type boolean.
In C the result has type int.
In Java the result has type boolean.
Java does not permit this operator to appear where a void method may appear (a statement expression in C).
C permits both the second and third operands to have void type.
Java prohibits either the second or third operand being an invocation of a void method.
C: "... the usual arithmetic conversions are performed to bring them to a common type and the result is that type."
Java: "If one of the operands is of type T where T
is byte, short or char, and the
other operand is a constant expression of type int whose value is
representable in type T, then the type of the conditional
expression is T."
"Otherwise, binary numeric promotion (5.6.2) is applied to the
operand types, and the type of the conditional expression is the
promoted type of the second and third operands."
In Java the order of evaluation is left to right.
In C complete struct assignment copies the values from one object to another.
In Java class assignment copies the reference, not the values referred to.
In Java the type of an expression is deduced from the, possibly promoted, types of the operands. In the case of comparison operators the type is boolean.
Java does not support the sizeof operator.
Java does not support the unary & operator or address arithmetic on references. So such address constants may not be used.
Only the members of a class may be declared static, ie Java does not support static local variables.
In Java the C concept of bit-field is not supported.
The layout of the fields of a class is not specified in Java and such information cannot be obtained by writing a Java program.
The keyword volatile (and the Java keyword final) may only appear on the declaration of a field of a class. They may not appear in local declaration of an identifier.
Java: "There are three kinds of reference types class types
(8), interface types (9), and array types (10)."
A reference to a primitive type can be created by declaring a class
containing a single field of the appropriate type.
In Java an identifier in an array declaration is a reference to a number of elements of storage. Information on the layout of this storage in memory is not available to the programmer.
In C a definition reserves storage for the array.
In Java a definition only reserves storage for the reference. This does not refer to any storage until a reference is assigned to it.
Methods may not be declared with variable numbers of arguments.
In Java the types of all parameters to methods must be given (C prototypes).
In C a function that takes no parameters is denoted by using the keyword void.
In Java a method that takes no parameters is denoted by an empty parameter list.
Java: "The length of the constructed array will equal the number of expressions."
In C the initial values appear outside of the aggregate or union declaration.
In Java the initial values appear inside the class definition, alongside the field being initialised.
Java: "The Expression must have type boolean ..."
Java requires that the body of a switch statement be a block, ie enclosed in braces.
Methods must appear within a class definition.
Java does not support the extern keyword.
Java does not allow methods taking variable numbers of arguments to be defined.
isalnum -> isLetterOrDigit
isalpha -> isLetter
iscntrl -> ???
isdigit -> isDigit
isgraph -> ???
islower -> isLowerCase
isprint -> ???
ispunct -> ???
isspace -> isSpace
Note that Java does not consider '\v', vertical tab, to be
a space character.
isupper -> isUpperCase
isxdigit -> ???
tolower -> toLowerCase
toupper -> toUpperCase
1 Jul 05 Added references to c0x 15 Jan 97 Got a few useful ideas from this paper. 27 Dec 96 Updated, plus comments from Eamonn McManus, emcmanus@gr.osf.org 24 Dec 96 Created
Copyright (c) 1996,1997,2005,2008. Knowledge Software Ltd. All rights reserved.
1 Jul 2005