Home > Java > Javagetting Started > The difference between c syntax and java syntax

The difference between c syntax and java syntax

(*-*)浩
Release: 2019-11-19 11:27:22
Original
4230 people have browsed it

The difference between c syntax and java syntax

The difference between c syntax and java syntax:

1. Identifier: (recommended) Study: java course)

The identifiers available in C are numbers, uppercase and lowercase letters, and underscores, and cannot start with numbers;

The identifiers available in Java are except C In addition to the three types, there is one more dollar sign ($), which also cannot start with a number.

2. Keywords:

The keywords in C are:

auto   break    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
Copy after login

The key in Java The characters 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   throw    throws    transient   true
try   static    super    switch   synchronized
void   volatile    while
Copy after login

3. Data type:

The data types in C are:

1 ) Basic types: integer type (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), enumeration type

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

3) Pointer type

4) Null type

注意下各类型一般所占字节数:
Copy after login

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 types are slightly different depending on the system, but the number of low-precision digits cannot exceed the high-precision number.

Data types in Java:

1) Basic types: Character type (char), numerical type (integer type (byte type byte, short integer type, 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 Except for integer data with l or L added 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 to be added after single precision, and d or D to be added 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 defined when it is defined. Assign an initial value, and macro constants defined with #define have no type.

2) Variables

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

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

Variables can be assigned initial values ​​or not, but In Java, long integers and floating point numbers must be followed by corresponding identification marks (such as l, f).

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

The logical operators &&, ||, ! both in C and Java. There are three types, and they 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 in both C and Java are: &, |, ^, ~ (inversion), << (left shift), >> (right shift), their meanings are basic 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:有心的读者可能会发现,如果你定义了一个byte或者short类型的负数,如-10,采用>>>方法进行无符号右移后输出的结果是-5,按照上面说的高位添0应该是正数。而int或long类型的就不会是负数,这是为什么呢?

我认为这是因为Java在进行>>>运算时采用的最低数据类型是int类型,导致高位数据全为1(计算机内存储的数据是以补码存储的,所以负数的byte或short类型转成int类型高位全填充1),移位时高位的最后一个1移到低位的第一位,然后截取成我们定义的数据类型(byte或short),所以我们看到的数还是负数。从这里我们可以看出,在byte和short类型的数据做>>>运算的时候可能得不到我们想要的值,千万注意。

6、数组

C中数组的定义如下:

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

定义可与初始化同时进行,如:int a[10] = {0,1,2,3,4,5,6,7,8,9};中括号内的常量可以省略。

Java中数组定义有两种方式:

数据类型 数组名[];或
数据类型 []数组名;
Copy after login

定义和初始化可同时进行,如:int []a = {0,1,2,3,4,5,6,7,8,9};

注意Java中数组如果在定义时没有进行初始化,在进行初始化的时候需要先分配内存,即:

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

也可在定义同时进行内存分配:

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

C和Java都不支持变长数组,引用的时候都是 数组名[下标]。区别是:Java的下标范围为0~数组长度-1,不在该范围会抛出数组下标越界异常,而C有效范围也是0~数组长度-1,但下标超出此界不会报错。

多维数组中,数组元素都是按行排列的。

还有一点要注意:C中定义数组不进行初始化则数组元素值是不可预知的,而Java中分配内存而不进行初始化数组中是有默认值的。

7、语句

C和Java语句区别不大,主要是:

1)方法/函数调用时C直接调用函数,Java调用方法时方法名前面要加对象名。

2)C中两个嵌套的复合语句同时定义同名变量是可以的,而Java不可以。

8、类、域、方法和全局变量、函数

1)类是C中没有的,Java中类定义如下:

[修饰符] class 类名[extends 父类名][implements 接口名]
{
//类体
}
Copy after login

其中修饰符可以为以下一个或多个访问修饰符:

abstract:抽象类。

final:最终类。

public:公共类。

2)域(成员变量)和全局变量类比:

Java中域的定义如下:

[修饰符] 类型 成员变量名;
Copy after login

修饰符可选以下一个或多个关键字:

public:公共成员。

protected:本类或同一个包的其他类以及其它包该类的子类可访问。

private:私有成员。

final:常量,确定后不能改变。

static:静态变量。

transient:临时变量。

volatile:备份变量。

各类型成员变量默认初始化为:

整型变量:0

浮点型变量:0.0

布尔型变量:false

字符型变量:空格

类变量:null

C中全局变量定义同一般变量:

[存储类别] 数据类型 变量表列;
Copy after login

其中存储类别可选:

auto:自动变量,当不申明存储类别时隐式默认该值。

static:静态变量。

register:寄存器变量。

extern:外部变量。

3)方法和函数类比:

Java中方法的定义如下:

[修饰符] 返回类型 方法名([参数表列])
{
//方法体
}
Copy after login

修饰符可选以下一个或多个:

public:公共方法。

protected:本类或同一个包的其他类以及其它包该类的子类可访问。

private:私有方法。

abstract:抽象方法,只有方法头没有方法体。

static:静态方法。

C中函数的定义如下:

[存储类别] [数据类型] 函数名([形参表列]) 
{
//函数体
}
Copy after login

存储类别可选:

extern:外部函数。

static:静态函数。

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template