Home  >  Article  >  Java  >  What is the difference in syntax between java and c?

What is the difference in syntax between java and c?

青灯夜游
青灯夜游Original
2019-11-15 15:26:512688browse

What is the difference in syntax between java and c?

#What is the syntax difference between java and c?

1. Identifier:

C Available identifiers are numbers, uppercase and lowercase letters, underscores, and cannot start with numbers; [Recommended Tutorial: C Language Tutorial]

In addition to the three types of identifiers available in C, Java has one more dollar sign ($), which also cannot start with a number. [Recommended learning: java course]

2. Keywords:

The keywords in C are:

auto break case case char const

continue default do double else

enum extern float for goto

if int long register return

short signed sizeof static struct

switch typedef union unsigned void

volatile while

The keywords in Java are:

abstract boolean break byte case

catch char class continue default

do double else extends false

##final finally float for if

implements import instanceof int interface

long native new null package

private protected public return short

this throws transient true

try static super switch synchronized

void volatile while

3. Data type:

The data types in C are:

  1. Basic type: integer (basic integer Type int, short integer type short[int] and long integer type long [int], as well as signed type [signed], unsigned type unsigned), character type [signed/unsigned]char, floating point type (single precision float, Double precision double and long double precision long double), enumeration type

  2. Construction type: array type, structure type, union type

  3. Pointer type

  4. Empty type

Note the number of bytes generally occupied by each type:

int: 2 bytes

short: 2 bytes

long: 4 bytes

char: 1 byte

float: 4 bytes

double: 8 bytes

long double: 16 bytes

Except for the char type, the above storage is slightly different depending on the system, but the number of low-precision digits does not Can exceed high precision.

Data types in Java:


  1. Basic types: character type (char), numerical type (integer type (byte type byte, Short integer type short, integer type int, long integer type long), floating point type (single precision float, double precision double)), Boolean type (boolean (true or false))

  2. Composite type: class, interface, array

Note the number of bytes occupied by each type of storage:

byte: 1 byte

short: 2 bytes

int: 4 bytes

long: 8 bytes

char: 2 bytes (Unicode encoding)

float: 4 bytes

double: 8 bytes

The storage space corresponding to the above data types has nothing to do with the platform and is fixed to this value.

4. Constants and variables

1) Constants

The definition of integer constants in Java and C is the same, except for long integer data Except for adding l or L at the end, other types display numerical values ​​directly. Unsigned constants in C are preceded by u or U. For different bases, decimal directly displays that the highest bit cannot contain 0, octal starts with 0, and hexadecimal starts with 0x or 0X.

For floating point types, both C and Java can only use decimal representation. Decimal form and exponential form can be used. When the exponential form is expressed, the decimal and exponent are separated by e or E. Note that Java requires f or F after single precision, and d or D after double precision to distinguish.

Character constants are represented by a single character or an escaped string enclosed in single quotes. Special attention should be paid to the fact that the character type in C can only represent characters with ASCII codes from 0 to 255. In Java, the Unicode encoding 2-byte storage unit can be used to represent special characters. When expressing Unicode encoding, \u plus a 4-digit hexadecimal string is used.

The Boolean type is only available in Java, so special attention is required.

Constants in Java are modified with the keyword final, which cannot be changed once assigned; in C, the keyword that cannot be changed is const, and the variable it modifies (note that it is a variable, not a constant) must be assigned an initial value when it is defined. , In addition, macro constants defined with #define have no type.

2) Variables

The definitions of variables in Java and C are basically the same, that is:

数据类型变量名[ = 变量初值];

Variables can be assigned initial values ​​or not, but in Java long integers and The corresponding identification mark (such as l, f) must be added after the floating point number.

Special note: Due to different compilers, C declaration variables must be placed before executable statements, otherwise compilation errors may occur.

5. Logical operators and bitwise operators

Logical operators &&, || , ! Three types, and have the same meaning. The difference is that the operation result in C is 0 and non-0, while in Java it can only be true or false. There are also &, |, ^ (XOR) in Java. The difference between & and &&, | and || is that the former is a non-shortcut operator and the latter is a shortcut operator, that is, judgments are made before and after &, and if false before &&, no judgment is made. For the subsequent judgment, |judges both before and after. If || is true before, the subsequent judgment will not be made. ^ means both are the same and false.

The bitwise operators available in C and Java are: &, |, ^, ~ (reverse) , 4686c4a8411139d4ee145c6bc3d87570> (right shift) , the meanings are basically the same. The right shift operation of negative numbers in C varies depending on the system (it may be an arithmetic right shift or a logical right shift), while in Java, >> represents an arithmetic right shift, that is, the highest bit is filled with the sign bit. The logical right shift (unsigned right shift) operator in Java is >>>, which uses two's complement right shift and adds 0 to the high bit.

PS: Careful readers may find that if you define a negative number of type byte or short, such as -10, the output result after using the >>> method for unsigned right shift is - 5. According to the above mentioned, adding 0 to the high position should be a positive number. The int or long type will not be a negative number. Why is this? I think this is because the lowest data type used by Java when performing >>> operations is the int type, causing the high-order data to be all 1 (the data stored in the computer is stored in two's complement, so negative byte or short The type is converted to int type and the high bits are fully filled with 1). When shifting, the last 1 in the high bits is moved to the first bit in the low bits, and then intercepted into the data type we defined (byte or short), so the number we see is still a negative number. From here we can see that when performing > operations on byte and short type data, we may not get the value we want, so be careful.

6. Array

The definition of array in C is as follows:

类型说明符数组名[常量表达式];

The definition can be carried out at the same time as the initialization, such as: int a[ 10] = {0,1,2,3,4,5,6,7,8,9};The constants in the brackets can be omitted.

There are two ways to define arrays in Java:

数据类型数组名[];
//或
数据类型 []数组名;

Definition and initialization can be done at the same time, such as: int []a = {0,1,2,3,4, 5,6,7,8,9};

Note: If the array in Java is not initialized when it is defined, memory needs to be allocated first when initializing, that is:

数组名 = new 数据类型[常量表达式];

You can also allocate memory at the same time as the definition:

数据类型数组名[] = new 数据类型[常量表达式];

Neither C nor Java supports variable-length arrays, and they are array name [subscript] when referenced. The difference is:

The subscript range of Java is 0~array length-1. If it is not in this range, an array subscript out-of-bounds exception will be thrown, and the valid range of C is also 0~array length-1, but No error will be reported if the subscript exceeds this limit.

In a multi-dimensional array, the array elements are arranged in rows.

One more thing to note: if an array is defined in C without initialization, the array element values ​​are unpredictable, while in Java, when memory is allocated without initialization, the array has a default value.

7. Statements

There is not much difference between C and Java statements, mainly:

1) C directly when calling methods/functions When calling a function, when calling a method in Java, the object name must be added before the method name.

2) It is possible for two nested compound statements to define variables with the same name at the same time in C, but not in Java.

The above is the detailed content of What is the difference in syntax between java and c?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn