A contrastive analysis of C and JAVA __java

Source: Internet
Author: User
Tags arithmetic arithmetic operators array definition logical operators reserved java keywords

Sun introduced Java is an object-oriented programming language, which is applicable to the development of Internet applications, known as one of the most important languages in the network age. Java can use a derivative language that is considered C, with C in a large number of elements remain the same, such as the structure of this method, expression statements, operators, and so is basically consistent with C: But Java is more concise, there is no C redundancy and easy to cause the exception of the functional components, and increased multithreading, exception handling, Network programming and other aspects of support functions. This paper compares Java with C from many angles, and improves the learning of C and Java language.

1. The structure of the adjusting method

C is similar to Java's lexical structure, for detailed analysis of whitespace characters, identifiers, annotations, constants, operators, delimiters, and keywords in the program.

1.1, whitespace and notes

White space characters include spaces, tabs, and line breaks.

There are two kinds of annotations in C:

1) The comment statement starts with/* and ends with/*.

2 note can be extended to the next line terminator with//start.

Java contains two kinds of annotations and whitespace characters for C.


1.2, identifiers

The set of identifiers for C is a subset of the Java identifier collection. The identifiers in C consist of uppercase and lowercase Latin characters, numbers, and underscores, and cannot begin with a number and cannot be the same as the keyword;

Java identifiers consist of uppercase and lowercase Latin characters, numbers, underscores, and $ , and cannot begin with numbers.


1.3. Keywords and reserved identifiers

Keywords are special symbols for languages, and C and Java keywords are similar.

Some of the keywords are unique in Java, as shown in the following illustration:

Abstract

Assert

Boolean

Byte

Catch

Class

Extends

Final

Finally

Implements

Import

Instranceof

Interface

Native

New

Package

Private

Protected

Public

Strictfp

Super

Synchronized

This

Throw

Throws

Transient

Try

Some keywords are used in C and Java is not used, and the following table shows the keywords that are unique to C.

Auto _bool _complex extern _imaginary Inline Register
Restrict Signed sizeof struct typedef Union usigned


1.4, Constant

Constants include 4 types: cosmetic, floating-point, character, and string. For constants, there is a slight difference between C and Java.

1 The integer constant in c only unsigned integer constant is larger than the Java integer constant, there is no suffix long long and unsigned in Java;

2 C and Java Word constants and string constants are very close, C there is a continuation of the mechanism, that is, if the string is too long, the line can not be placed, then the end of the line with a backslash, backslash and row non-terminal are ignored, so that the string constants can be written in multiple lines, and all the source line can go on. In Java, however, strings are not allowed to span multiple lines.

3 each string in C is terminated by a null character (""), and there is no such terminator in the Java string.

4 in C, "0" can also represent Boolean false (FALSE) or pointer null (NULL), with a reserved identifier false or NULL in Java.


1.5, operators and separators

Most of the operators and delimiters in Java are compatible with C, and the operators provided in C are almost entirely appropriate for the Java language. The subtle differences between the two are:

1) The unique indirect member operator (->) in C, the value operator (*), the address operator (&), the comma operator (,);

2 Java does not provide pointer operators, increased the object operator instanceof, string operators + and 0 padding right shift, etc.

3) Unlike C, the arithmetic operators in Java cannot be used on Boolean types, and the operands of logical operators can only be boolean but not integral, and special handling is required in the conversion;

4) C uses sizeof to allocate and release memory and the number of data obtained, but does not have this operator in Java because all data types are the same size in all machines;


2. Data type

The data types in C are divided into: book type, pointer type, array type, struct type, union type, enumeration type, function type and void type, and the following is a comparative analysis of the similarities and differences between Java and C and type conversion for these types.

2.1. Arithmetic type

The arithmetic types in C include integer and floating-point type. The integral type in C has character type, Boolean type, and enum type. The type size in C depends on the concrete implementation of C compiler;

Java has a clear convention about the size of the base data type, where Char is a 2-byte unsigned integer used to store Unicode characters. In addition to the char type, Java does not support unsigned integers, and other integers are unsigned;

and the use of char types in C is more flexible than in Java. Arrays and pointers of char types in C can be used to represent strings, and Java must use a string class representation.


2.2. Structure and Union type

There is no structure type in Java; The compiler allocates the memory space for the struct variable when it is declared in C, and in Java it needs to allocate space with new; C to allow incomplete initialization and to declare anonymous constructs (nested) that are not allowed in Java ; There is no language structure in Java that corresponds to the union type in C.


2.3. Enumeration type

In C, the enumeration type declaration represents the symbolic name of an integer constant, which is in fact the int type;

In Java, there is an enumeration type, and the enum variable declared by the keyword enum is in fact a subclass of Java.lang.Enum, and you can add properties and methods to it, although the enumerated constants are also int types, but they cannot be initialized with integers or used as integral types.


2.4. void type

Both C and Java support the void type.

C Void can replace the function parameter table and the representation function without parameters, when the void * type is established, the variable of void * type can store a pointer to any type or function;

void in Java only indicates that the method does not have a return value.


2.5, typedef

A typedef in C is not a preprocessing instruction, but rather a different name for a type, and C allows the name of another typedef definition to be referenced in a typedef;

There is no corresponding mechanism in Java;


2.6. Data type conversion

The Java language is a strongly typed language and requires more stringent data type compatibility than C, which guarantees security and robustness. All numeric passes in Java, whether directly or through a parameter, are first checked for type compatibility, and any type of mismatch will result in a compilation error. In Java, integer types, floating point types, and character types are compatible with each other, but are incompatible with Boolean types, except for Boolean and enum types, which allow any basic type to be converted to other basic types in Java;

A variable in C that represents true/false is always represented by an int type. C allows conversion from an arithmetic type to a Boolean type. Java does not allow implicit and explicit conversions between the two types, nor is it possible to compare Boolean and arithmetical patterns.


3, expressions and statements

Java expressions and statements are almost identical to C, and there are nuances: the conditional expressions in the IF, write, and for statements in Java should be Boolean, and in c they can be plastic; Java does not provide goto statements, and the presence of goto statements can change how the program works, is the security and stability of the program is reduced, Java uses the symbol of the break statement and continue statement to implement the transfer function; expression Statements in Java have more restrictions than C, Java only allow replication, function calls, the increment, new expression, etc. appear in the expression statement.


4. Function

1 for variables and functions, C needs to implement declarations and definitions, while in Java only definitions, no declarations;

2 because C is not object oriented, all global variables and functions in C are inherently static to Java. Java is an object-oriented language that does not allow you to declare functions outside of classes and interfaces, including main functions and variables. And in C, often appear in a file to define a large number of global variables, in other files by reference declaration of these variables to achieve sharing, but Java does not allow global variables or functions;

3 when the type of the return value of the function in C is not consistent with that of the declaring type, the return value of the function is automatically converted to that type and then the function returns;

4 the order in which function call parameters are computed in C is undefined, and Java is ordered from left to right;

5 in C if a variable or function is defined in another file, the variable or function must be declared with extern. They are not visible when a single file is compiled, but are visible at the link stage. Java compilers do not have this link phase, all required files must be visible at compile time;

6 C and Java programs are executed from the main function. In C, main function usually need to have argc,argv two parameters, argc exist the number of command-line parameters, argv exist the actual command-line parameters; In Java, main must use stringargs[] as the parameter of the method, which is used to receive running system boot Java The parameters of the command line used by the application;

7) C and Java can call the Exit function to terminate the program, C can use the Abort function to exit the abnormal termination of the program, there is no corresponding policy in Java.


5, Array

1 The array is an ordered sequence of data elements of the same type. The Java program strictly checks the bounds of all arrays while compiling and running, improves the security of the program, and does not provide the boundary check function in C, so it can provide the running speed of the program;

2 in C, the array is generally defined as the size of the specified, a contiguous amount of memory space required by the compiler to allocate an array, and the elements in the array are stored sequentially; in Java, the array definition is not allowed to specify the size of the array, nor does it allocate memory space, and the new operator is required to display the creation , or implicitly created by initialization methods, the space occupied by the array is automatically reclaimed by Java and collectors;

3 in C if the array is partially initialized, the element that does not cause the flower is set to 0. Java does not directly support multidimensional arrays, but can create arrays of arrays, in which multidimensional arrays can be implemented;

4 It is worth noting that in C the array actually gets its pointer, while in Java it gets the reference, not the pointer. In Java, a program can access an element only by using the subscript operator and by accessing an array element, not by adding and subtraction an integer as a pointer in C.

5 C allows the array of traction type can be long, and Java requirements are int type.


6. Comparison of preprocessor and header files

Java has no preprocessor, no header file; c the preprocessor only receives some text and converts it into other text, the compiler compiles the preprocessed source program, and in C typically uses a header file to declare the prototype as well as global variables, library functions, and so on.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.