Home >Java >javaTutorial >What are the differences between java and c language?

What are the differences between java and c language?

青灯夜游
青灯夜游Original
2019-11-16 13:30:5414179browse

What are the differences between java and c language?

#What are the differences between java and c language?

1. Java and C memory allocation are different:

There are five memory allocation areas in Java, namely the stack area (which stores local Variables, recycled immediately after use), heap area (stores new things, entities, objects, each object has an address value, and the data of each object has a default value. After use, it will be collected in the garbage Recycled when the processor is idle), method area, local method area (related to the system), register area (used by the CPU). [Recommended learning: java course]

There are four memory allocation areas in C, which are the stack area (storing function parameters and values ​​of local variables) and the heap area (dynamic memory application and release , if the programmer does not release the memory, it may be released by the operating system after the program ends), global area (static area), code area.

2. Function

1) For variables and functions, C needs to implement declarations and definitions, while Java only has definitions and no declarations;

2) Since C is not object-oriented, all global variables and functions in C are essentially static to Java. Java is an object-oriented language, which does not allow functions to be declared outside classes and interfaces, including main functions and variables. In C, it is common to define a large number of global variables in one file, and declare these variables by reference in other files to achieve sharing, but Java does not allow global variables or functions;

3) When C When the type of the function's return value does not match the declared type, the function's return value will be automatically converted to this type before the function returns; when facing this situation in Java, forced type conversion needs to be added;

4) The order of function call parameter calculation in C is undefined, while Java has an order from left to right;

5) If a variable or function in C is defined in another file, The variable or function must be declared using extern. They are not visible when the individual files are compiled, but are visible during the linking phase. Java's compiler does not have such a link phase, and all required files must be visible during compilation;

6) Both C and Java programs start execution from the main function. The main function in C generally requires two parameters, argc and argv. argc contains the number of command line parameters, and argv contains the actual command line parameters; main in Java must use stringargs[] as a method parameter, which is used to receive the running system The command line parameters used when starting a Java application;

7) The exit function can be called in C and Java to terminate the program; the abort function can be used in C to exit an abnormally terminated program, and there is no such function in Java corresponding strategy.

3. Comparison of preprocessors and header files

Java has no preprocessor and no header files; in C, the preprocessor just receives some text and It is converted into other text, and the compiler compiles the preprocessed source program. In C, header files are usually used to declare prototypes, global variables, library functions, etc.

4. In terms of usage

C language: Pure C is generally used in embedded systems to write the lowest-level programs, because its language is the easiest to connect to hardware. . Can do: embedded low-level software, operating system, various function libraries, engine library software core code, server-side low-level tools.

JAVA: J2EE, J2SM, J2ME, anything. What you can do: Embedded mobile device application software development, web page background logic development, some desktop programs

C, JAVA can actually be used in various fields.

5. In terms of operation mode:

C language: The original C is process-oriented, that is, executed line by line.

JAVA: Absolutely object-oriented and executed by module.

(Supplement: Of course, in C and C#, C also converts to object-oriented mode)

6. Different syntax:

  • C language can directly operate memory, but java cannot directly operate it;

  • C language can encapsulate dynamic libraries, but java cannot;

  • C language code is not easy to cross-platform, java code is easy to cross-platform;

  • C language has pointers, java does not have pointers;

  • C language can directly operate the serial port, and Java requires third-party jar package support;

  • C language threads are more flexible, and Java threads have been encapsulated;

  • The identifiers available in C are numbers, uppercase and lowercase letters, and underscores, and cannot start with a number; in addition to the three types of identifiers available in C, the identifiers available in Java have one more dollar sign ($), which also cannot start with a number. .

The above is the detailed content of What are the differences between java and c language?. 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
Previous article:Is java useful?Next article:Is java useful?