国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

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

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

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

注意下各類型一般所占字節(jié)數(shù):

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:

數(shù)據(jù)類型變量名[ = 變量初值];

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:有心的讀者可能會發(fā)現(xiàn),如果你定義了一個byte或者short類型的負(fù)數(shù),如-10,采用>>>方法進(jìn)行無符號右移后輸出的結(jié)果是-5,按照上面說的高位添0應(yīng)該是正數(shù)。而int或long類型的就不會是負(fù)數(shù),這是為什么呢?

我認(rèn)為這是因為Java在進(jìn)行>>>運算時采用的最低數(shù)據(jù)類型是int類型,導(dǎo)致高位數(shù)據(jù)全為1(計算機(jī)內(nèi)存儲的數(shù)據(jù)是以補(bǔ)碼存儲的,所以負(fù)數(shù)的byte或short類型轉(zhuǎn)成int類型高位全填充1),移位時高位的最后一個1移到低位的第一位,然后截取成我們定義的數(shù)據(jù)類型(byte或short),所以我們看到的數(shù)還是負(fù)數(shù)。從這里我們可以看出,在byte和short類型的數(shù)據(jù)做>>>運算的時候可能得不到我們想要的值,千萬注意。

6、數(shù)組

C中數(shù)組的定義如下:

類型說明符數(shù)組名[常量表達(dá)式];

定義可與初始化同時進(jìn)行,如:int a[10] = {0,1,2,3,4,5,6,7,8,9};中括號內(nèi)的常量可以省略。

Java中數(shù)組定義有兩種方式:

數(shù)據(jù)類型 數(shù)組名[];或
數(shù)據(jù)類型 []數(shù)組名;

定義和初始化可同時進(jìn)行,如:int []a = {0,1,2,3,4,5,6,7,8,9};

注意Java中數(shù)組如果在定義時沒有進(jìn)行初始化,在進(jìn)行初始化的時候需要先分配內(nèi)存,即:

數(shù)組名 = new 數(shù)據(jù)類型[常量表達(dá)式];

也可在定義同時進(jìn)行內(nèi)存分配:

數(shù)據(jù)類型數(shù)組名[] = new 數(shù)據(jù)類型[常量表達(dá)式];

C和Java都不支持變長數(shù)組,引用的時候都是 數(shù)組名[下標(biāo)]。區(qū)別是:Java的下標(biāo)范圍為0~數(shù)組長度-1,不在該范圍會拋出數(shù)組下標(biāo)越界異常,而C有效范圍也是0~數(shù)組長度-1,但下標(biāo)超出此界不會報錯。

多維數(shù)組中,數(shù)組元素都是按行排列的。

還有一點要注意:C中定義數(shù)組不進(jìn)行初始化則數(shù)組元素值是不可預(yù)知的,而Java中分配內(nèi)存而不進(jìn)行初始化數(shù)組中是有默認(rèn)值的。

7、語句

C和Java語句區(qū)別不大,主要是:

1)方法/函數(shù)調(diào)用時C直接調(diào)用函數(shù),Java調(diào)用方法時方法名前面要加對象名。

2)C中兩個嵌套的復(fù)合語句同時定義同名變量是可以的,而Java不可以。

8、類、域、方法和全局變量、函數(shù)

1)類是C中沒有的,Java中類定義如下:

[修飾符] class 類名[extends 父類名][implements 接口名]
{
//類體
}

其中修飾符可以為以下一個或多個訪問修飾符:

abstract:抽象類。

final:最終類。

public:公共類。

2)域(成員變量)和全局變量類比:

Java中域的定義如下:

[修飾符] 類型 成員變量名;

修飾符可選以下一個或多個關(guān)鍵字:

public:公共成員。

protected:本類或同一個包的其他類以及其它包該類的子類可訪問。

private:私有成員。

final:常量,確定后不能改變。

static:靜態(tài)變量。

transient:臨時變量。

volatile:備份變量。

各類型成員變量默認(rèn)初始化為:

整型變量:0

浮點型變量:0.0

布爾型變量:false

字符型變量:空格

類變量:null

C中全局變量定義同一般變量:

[存儲類別] 數(shù)據(jù)類型 變量表列;

其中存儲類別可選:

auto:自動變量,當(dāng)不申明存儲類別時隱式默認(rèn)該值。

static:靜態(tài)變量。

register:寄存器變量。

extern:外部變量。

3)方法和函數(shù)類比:

Java中方法的定義如下:

[修飾符] 返回類型 方法名([參數(shù)表列])
{
//方法體
}

修飾符可選以下一個或多個:

public:公共方法。

protected:本類或同一個包的其他類以及其它包該類的子類可訪問。

private:私有方法。

abstract:抽象方法,只有方法頭沒有方法體。

static:靜態(tài)方法。

C中函數(shù)的定義如下:

[存儲類別] [數(shù)據(jù)類型] 函數(shù)名([形參表列]) 
{
//函數(shù)體
}

存儲類別可選:

extern:外部函數(shù)。

static:靜態(tài)函數(shù)。

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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

Hot Topics

PHP Tutorial
1502
276
How to handle transactions in Java with JDBC? How to handle transactions in Java with JDBC? Aug 02, 2025 pm 12:29 PM

To correctly handle JDBC transactions, you must first turn off the automatic commit mode, then perform multiple operations, and finally commit or rollback according to the results; 1. Call conn.setAutoCommit(false) to start the transaction; 2. Execute multiple SQL operations, such as INSERT and UPDATE; 3. Call conn.commit() if all operations are successful, and call conn.rollback() if an exception occurs to ensure data consistency; at the same time, try-with-resources should be used to manage resources, properly handle exceptions and close connections to avoid connection leakage; in addition, it is recommended to use connection pools and set save points to achieve partial rollback, and keep transactions as short as possible to improve performance.

Understanding the Java Virtual Machine (JVM) Internals Understanding the Java Virtual Machine (JVM) Internals Aug 01, 2025 am 06:31 AM

TheJVMenablesJava’s"writeonce,runanywhere"capabilitybyexecutingbytecodethroughfourmaincomponents:1.TheClassLoaderSubsystemloads,links,andinitializes.classfilesusingbootstrap,extension,andapplicationclassloaders,ensuringsecureandlazyclassloa

How to work with Calendar in Java? How to work with Calendar in Java? Aug 02, 2025 am 02:38 AM

Use classes in the java.time package to replace the old Date and Calendar classes; 2. Get the current date and time through LocalDate, LocalDateTime and LocalTime; 3. Create a specific date and time using the of() method; 4. Use the plus/minus method to immutably increase and decrease the time; 5. Use ZonedDateTime and ZoneId to process the time zone; 6. Format and parse date strings through DateTimeFormatter; 7. Use Instant to be compatible with the old date types when necessary; date processing in modern Java should give priority to using java.timeAPI, which provides clear, immutable and linear

Comparing Java Frameworks: Spring Boot vs Quarkus vs Micronaut Comparing Java Frameworks: Spring Boot vs Quarkus vs Micronaut Aug 04, 2025 pm 12:48 PM

Pre-formanceTartuptimeMoryusage, Quarkusandmicronautleadduetocompile-Timeprocessingandgraalvsupport, Withquarkusoftenperforminglightbetterine ServerLess scenarios.2.Thyvelopecosyste,

How does garbage collection work in Java? How does garbage collection work in Java? Aug 02, 2025 pm 01:55 PM

Java's garbage collection (GC) is a mechanism that automatically manages memory, which reduces the risk of memory leakage by reclaiming unreachable objects. 1.GC judges the accessibility of the object from the root object (such as stack variables, active threads, static fields, etc.), and unreachable objects are marked as garbage. 2. Based on the mark-clearing algorithm, mark all reachable objects and clear unmarked objects. 3. Adopt a generational collection strategy: the new generation (Eden, S0, S1) frequently executes MinorGC; the elderly performs less but takes longer to perform MajorGC; Metaspace stores class metadata. 4. JVM provides a variety of GC devices: SerialGC is suitable for small applications; ParallelGC improves throughput; CMS reduces

Understanding Network Ports and Firewalls Understanding Network Ports and Firewalls Aug 01, 2025 am 06:40 AM

Networkportsandfirewallsworktogethertoenablecommunicationwhileensuringsecurity.1.Networkportsarevirtualendpointsnumbered0–65535,withwell-knownportslike80(HTTP),443(HTTPS),22(SSH),and25(SMTP)identifyingspecificservices.2.PortsoperateoverTCP(reliable,c

Comparing Java Build Tools: Maven vs. Gradle Comparing Java Build Tools: Maven vs. Gradle Aug 03, 2025 pm 01:36 PM

Gradleisthebetterchoiceformostnewprojectsduetoitssuperiorflexibility,performance,andmoderntoolingsupport.1.Gradle’sGroovy/KotlinDSLismoreconciseandexpressivethanMaven’sverboseXML.2.GradleoutperformsMaveninbuildspeedwithincrementalcompilation,buildcac

go by example defer statement explained go by example defer statement explained Aug 02, 2025 am 06:26 AM

defer is used to perform specified operations before the function returns, such as cleaning resources; parameters are evaluated immediately when defer, and the functions are executed in the order of last-in-first-out (LIFO); 1. Multiple defers are executed in reverse order of declarations; 2. Commonly used for secure cleaning such as file closing; 3. The named return value can be modified; 4. It will be executed even if panic occurs, suitable for recovery; 5. Avoid abuse of defer in loops to prevent resource leakage; correct use can improve code security and readability.

See all articles