So Sánh Cú Pháp Giữa Pascal, Java và C
Đây là bảng so sánh không đầy đủ...1 số cú pháp quan trọng thôi !Pascal | Java | C |
---|---|---|
{ comment } | /* comment */ // another kind of comment | /* comment */ |
Statements | ||
Note that in Pascal, semicolons are used as statement separators while in Java and C they are used as statement terminators. In Java and C, one can define variables local to any block; thus, variables can be defined within any pair of braces. | ||
i := i+j | i = i+j; | i = i+j; |
begin statement 1; statement 2 end | { statement 1; statement 2; } | { statement 1; statement 2; } |
if expression then statement else statement | if (expression) statement else statement | if (expression) statement else statement |
for i := 1 to 10 do statement | for (int i=1; i<=10; i++) statement | for (int i=1; i<=10; i++) statement |
for i := 10 downto 1 do statement | for (int i=10; i>0; i--) statement | for (int i=10; i>0; i--) statement |
while i < 10 do statement | while (i < 10) statement | while (i < 10) statement |
repeat statements until i=10 | do statement while (i != 10); | do statement while (i != 10); |
functionName := x {Execution can continue.} | return x; // Returns immediately. | return x; /* Returns immediately. */ |
{No Pascal equivalent} | break; // leave a loop. | break; /* leave a loop */ |
{No Pascal equivalent} | continue; //skip rest, but stay in loop | continue; /*skip rest, but stay in loop*/ |
procedureCall(x,y,z) | x.methodCall(y,z); | functionCall(x,y,z); |
These are not quite equivalent since in Java there can be several methods that have the same name; the one chosen depends on the types of x, y, and z. Note that x, y, and z are not treated symmetrically in Java: the method called is based on the true class of the data stored in x, while for y and z we use types based on the declarations of y and z. | ||
Operators | ||
All three languages agree on the standard operators for arithmetic (+,-,*,/) and on several of the relational operators (<, >, <=, and >=). | ||
= <> | == != | == != |
and or not {No Pascal equivalent.} | & (logical-and) | (logical-or) ! (logical-complement) ^ (logical-xor) | /* No C equivalent. */ /* No C equivalent. */ ! /* No C equivalent. */ |
{No Pascal equivalents.} | & (bitwise-and) | (bitwise-or) ~ (bitwise-complement) ^ (bitwise-xor) | & (bitwise-and) | (bitwise-or) ~ (bitwise-complement) ^ (bitwise-xor) |
{No Pascal equivalents.} | && (conditional-and) || (conditional-or) | && (conditional-and) || (conditional-or) |
mod div | % / | % / |
{No Pascal equivalent} | + (string concatenation) | /* No C equivalent. */ |
Primitive Types | ||
integer | byte (8 bits) short (16 bits) int (32 bits) long (64 bits) | short int long |
The integer types above are not actually equivalent. The implementations of the Pascal and C types can vary depending on the machine, while the Java types are consistent across all platforms. This is also true of the floating point types described below. The Java types, for both integers and floating point numbers, are based on IEEE standards. | ||
real | float (32 bit) double (64 bit) | float double |
char | char | char |
boolean | boolean | int /* C has no boolean type */ |
Declarations | ||
i,j,k:integer | int i,j,k; | int i,j,k; |
constant MAX = 100 | final int MAX = 100; | #define MAX = 100; |
A:array[0..9] of integer | int[] A = new int[10]; or int A[] = new int[10]; | int A[10]; |
In Java and C, all arrays start at index 0, so the declarations for A all mean that A has 10 locations, indexed 0 through 9. For the 2-dimensional array below, B[i,j] in Pascal corresponds to B[i][j] in both Java and C. | ||
B:array[0..9,0..99] of real | float[][] B = new float[10][100]; | float B[10][100]; |
s: packed array[0..9] of char | String s; | char *s; s = malloc(10); |
In Java, a String is not the same as an array of characters and it is not terminated by the NUL character. | ||
The above string declarations are not actually equivalent. In Java, an uninitialized variable (one that is not one of the primitive types) always has the value null. Any string (of any length) can legally be assigned to s. A Java String cannot be altered; strings that need to be altered use the type StringBuffer. | ||
r: record a:char; b:integer end | class r { char a; int b: } | struct r { char a; int b; } |
The Java class declaration above is only roughly equivalent to the record and struct declarations. The Java class is the basic building block of programs. A class contains not only fields, as shown above, but it also contains the methods that act on members (instances) of that class. | ||
For all three languages, one can reference a field within a record, class, or struct with the notation r.a for the character field above and r.b for the integer field. | ||
b: ^integer | /* No Java equivalent. */ | int *b; |
Functions | ||
function a(b:integer):char begin ... a:='X' end; | char a (int b) { ... return 'X'; } | char a (int b) { ... return 'X'; } |
There is no direct Java or C equivalent to Pascal's var parameters. Note though that in Java, each Object is actually passed by copying a (nonexplicit) pointer to that Object, thus giving a result quite similar to Pascal's var parameters. In C, explicit pointers are used to get a similar result. |