C# has more keywords than Java. When both languages define the same keyword, they typically have the same function, although some equivalent features are available through different keywords. Each keyword will be discussed in detail in the appropriate section of this chapter, but we’ll begin by presenting a high-level comparison of the keywords available in both languages.
Although Java packages and C# namespaces are significantly different in their implementation and capabilities, both languages provide keywords for their use and declaration. Table 4-1 contrasts these keywords.
For details of packages and namespaces, refer to the Namespaces section later in this chapter.
Table 4-2 contrasts the Java and C# keywords associated with
Built-in constant values required by the languages: null, void, true, and false.
Built-in simple data types: char, int, byte, and so forth.
In this category, C# provides a superset of the keywords provided in Java. Because the Java Virtual Machine supports a restricted range of data types that doesn’t include unsigned integer types, Java does not have keywords to represent them. Java also doesn’t have the decimal type, although the BigDecimal class provides similar functionality.
For a comprehensive coverage of each keyword in Table 4-2, refer to the Types section in Chapter 5.
Table 4-2. Simple Type Comparison
Java | C# | Comments |
---|---|---|
boolean | bool | True or false values. |
char | char | 16-bit Unicode character. |
byte | sbyte | 8-bit signed integer (-128 to 127). |
byte | 8-bit unsigned integer (0 to 255). | |
short | short | 16-bit signed integer (-32768 to 32767). |
N/A | ushort | 16-bit unsigned integer (0 to 65535). |
int | int | 32-bit signed integer (-2147483648 to 2147483647). |
N/A | uint | 32-bit unsigned integer (0 to 4294967295). |
long | long | 64-bit signed integer (-9223372036854775808 to 9223372036854775807). |
N/A | ulong | 64-bit unsigned integer (0 to 18446744073709551615). |
float | float | 32-bit double-precision floating point. |
double | double | 64-bit double-precision floating point. |
N/A | decimal | 128-bit high-precision decimal number with 28 significant digits. Intended for precise financial calculations. |
true | true | Technically, true is not a Java keyword but a Boolean literal. |
false | false | Technically, false is not a Java keyword but a Boolean literal. |
null | null | Technically, null is not a Java keyword but a literal. |
void | void | Identify a functional member as not returning a value. |
Table 4-3 contrasts the Java and C# keywords associated with declaring, accessing, and manipulating complex types and their members.
These keywords are covered in detail in the Modifiers section later in this chapter and in the Types and Members sections in Chapter 5.
Table 4-3. Complex Type Keyword Comparison
Table 4-4, Table 4-5, and Table 4-6 contrast the keywords used as modifiers for program elements. We have broken this category down into three topic areas: access modifiers, inheritance-related modifiers, and others.
For comprehensive coverage of each keyword, refer to the Modifiers section later in this chapter.
Table 4-4. Access Modifier Keyword Comparison
Java | C# | Comments |
---|---|---|
public | public | Identifies a public program element. |
private | private | Identifies a private program element. |
protected | protected | Identifies a protected program element. The protected keyword provides different accessibility in C# than Java. |
N/A | internal | Identifies an internal program element providing access to all members of the containing assembly. |
Table 4-5. Inheritance Modifier Keyword Comparison
Java | C# | Comments |
---|---|---|
abstract | abstract | Identifies an incomplete implementation of a program element. |
final | sealed | Identifies an element that cannot be derived from. |
N/A | new | Identifies an element that hides an inherited member. |
N/A | override | Identifies an element that overrides an inherited virtual member. |
N/A | virtual | Identifies a member that can be overridden by a derived class. |
Table 4-6. Other Modifier Keyword Comparison
Java | C# | Comments |
---|---|---|
final | readonly | Identifies a field that can be assigned to only once. |
native | extern | Identifies an externally implemented member. |
static | static | Identifies a static program element. |
N/A | C# has no equivalent for the strictfp keyword. | |
synchronized | lock | Synchronizes access to a statement block. |
transient | N/A | C# does not provide a language equivalent of transient; however, the NonSerialized attribute provides comparable functionality. See Chapter 10 for full details. |
volatile | volatile | Ensures synchronized and ordered access to a field. |
Table 4-7 contrasts the Java and C# keywords used to provide conditional and flow control capabilities.
See the Statements section later in this chapter for a full description of the keywords in Table 4-7.
Table 4-7. Conditional and Flow Control Keyword Comparison
Java | C# | Comments |
---|---|---|
assert | N/A | C# alternatives are implemented through static members of the Trace and Debug classes in the .NET class libraries. |
if | if | Conditional statement. |
else | else | Optional component of the if statement. |
switch | switch | Conditional statement. |
case | case | Component of the switch statement. |
default | default | Optional component of the switch statement. |
for | for | Determinant loop. |
do | do | Postloop conditional. |
while | while | Preloop conditional. |
N/A | foreach | Collection-specific enumerator. |
N/A | in | Part of the foreach statement. |
goto | goto | Java reserved keyword (unused). Provides unconditional branching support in C#. |
continue | Start a new iteration of a loop. | |
break | break | Terminate a loop. |
return | return | Return from a functional member. |
Exception handling is predominantly the same in C# and Java, although declaring exceptions is not required in C#. C# adds keywords used to control how integral arithmetic overflow is handled at compile time and run time. Table 4-8 describes the C# keywords related to exception handling.
For comprehensive coverage of the keywords in Table 4-8, see the Statements section later in this chapter and the Exceptions and Exception Handling section in Chapter 6.
Table 4-8. Exception Handling Keyword Comparison
Java | C# | Comments |
---|---|---|
try | try | Encloses a block of code statements. If an exception is thrown by one of these statements, program execution will be transferred to the statements enclosed by a suitable catch block or to the statement immediately following the enclosed statement block. |
catch | catch | C# also supports general and anonymous catch clauses. |
finally | finally | Code to execute regardless of whether a try...catch statement block exits normally or via an exception. |
throw | throw | Throw or rethrow an exception. |
throws | N/A | Not supported in C#. |
N/A | checked | Turns on integral arithmetic overflow checking. |
N/A | unchecked | Turns off integral arithmetic overflow checking. |
C# introduces four keywords specifically related to the use of unmanaged code, a feature not available in Java. Table 4-9 summarizes these keywords.
Refer to the Unsafe Code section in Chapter 6 for comprehensive coverage of the keywords in Table 4-9.
Table 4-9. Unmanaged Code Keywords
Java | C# | Comments |
---|---|---|
N/A | fixed | Allows a pointer to reference a managed variable, ensuring that the garbage collector does not relocate or deallocate the variable while it’s still referenced. |
N/A | sizeof | Gets the size in bytes of the memory occupied by a value type instance. See the section Unsafe Code in Chapter 6 for details. |
N/A | stackalloc | Allocates memory blocks on the stack for the storage of local variables. |
N/A | unsafe | Identifies unsafe types, members, statements, or code blocks. |
It’s possible to use keywords as identifiers in C#. The compiler will not interpret an identifier as a keyword if it’s prefixed with the @ symbol. For example:
public class @class { public string @public(string @string) { string @return = @string + @string; return @return; } public static void Main() { System.Console.WriteLine( new @class().@public("A silly test")); } }
Because the common language runtime (CLR) is designed to run components developed in different languages, the @ prefix is required to allow methods to be called in components that have used identifiers that are also C# keywords.