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

The difference between c syntax and java syntax

Nov 19, 2019 am 11:27 AM
java

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles