Previous | Table of Contents | Next |
14.2.14.2. Forward References
For compiler efficiency, C requires that variables and functions must be defined, or at least declared, before they can be used or called. That is, forward references are not allowed in C. Java does not make this restriction, and by lifting it, it also does away with the whole concept of a variable or function declaration that is separate from the definition.
Java allows very flexible forward references. A method may refer to a variable or another method of its class, regardless of where in the current class the variable or method is defined. Similarly, it may refer to any class, regardless of where in the current file (or outside of the file) that class is defined. The only place that forward references are not allowed is in variable initialization. A variable initializer (for local variables, class variables, or instance variables) may not refer to other variables that have not yet been declared and initialized.
14.2.14.3. Method Overloading
A technique that Java borrows from C++ is called method overloading. Overloaded methods are methods that have the same name, but have different signatures. In other words, they take different types of arguments, a different number of arguments, or the same type of arguments in different positions in the argument list. You cannot overload a method by changing only its return type. Two methods with the same name may have different return types, but only if the method arguments also differ. Similarly, two overloaded methods may throw different exceptions, but only if their arguments differ as well.
Method overloading is commonly used in Java to define a number of related functions with the same name, but different arguments. Overloaded methods usually perform the same basic operation, but allow the programmer to specify arguments in different ways depending on what is convenient in a given situation. Method overloading is discussed in more detail later.
14.2.14.4. The void Keyword
The void keyword is used in Java, as in C, to indicate that a function returns no value. (As we will see in the next section, constructor methods are an exception to this rule.)
Java differs from C (and is similar to C++) in that methods that take no arguments are declared with empty parentheses, not with the void keyword. Java does not have any void * type, nor does it use a (void) cast in order to ignore the result returned by a call to a non-void method.
14.2.14.5. Modifiers
Java defines a number of modifier keywords that may be applied to variable and/or method declarations to provide additional information or place restrictions on the variable or method:
14.2.14.6. No Structures or Unions
Java does not support C struct or union types. Note, however that a class is essentially the same thing as a struct, but with more features. And you can simulate the important features of a union by subclassing.
14.2.14.7. No Enumerated Types
Java does not support the C enum keyword for defining types that consist of one of a specified number of named values. This is somewhat surprising for a strongly typed language like Java. Enumerated types can be partially simulated with the use of static final constant values.
14.2.14.8. No Method Types
C allows you to store the address of a function in a variable and to pass function addresses to other functions. You cannot do this in Java: Methods are not data, and cannot be manipulated by Java programs. Note, however, that objects are data, and that objects can define methods.12 So, when you need to pass a method to another method, you declare a class that defines the desired method and pass an instance of that class. See, for example, the FilenameFilter interface in the java.io package.
12An interesting way to think about objects in Java is a kind of method that defines multiple entry points.
14.2.14.9. No Bitfields
Java does not support the C ability to define variables that occupy particular bits within struct and union types. This feature of C is usually only used to interface directly to hardware devices, which is never necessary with Javas platform-independent programming model.
Previous | Table of Contents | Next |