Similarities and differences between C language and Java

Source: Internet
Author: User
Tags strtok

Similarities and differences between C language and Java

 

 

Author: Finan <li.zhongnan@hotmail.com>

 

 

 

 

 

 

Since I started my work, I first used programming such as VB and ASP. Later I switched to Java and had some knowledge of object-oriented languages. Now I mainly use C language to write some code. Since I learned some C language knowledge at school, and later I wrote several hundred lines of simple programs, it was quite quick to get started with C during this time.

All things are profound and profound, not to mention the Java and C languages that have a long history. However, when the press button is reached, you will have some insights on C and Java.

 

Same place:

 

1. The syntax is similar:

Java can be developed from C ++, so the syntax of Java and C is similar.

 

2. Programming proficiency is the mastery of the Language Library:

To some extent, programming languages are made up of syntaxes and corresponding libraries. Java has its own class libraries, while C has a standard library. The so-called programming is to use and syntax to call and combine functions in the library.

 

Different Places:

 

1. Memory Management

In Java, there is basically no need to consider the memory issue. If you want to use an object, you can use a new one. Behind this process is a certain amount of memory that JRE classifies as objects, when JRE finds that you no longer use this object, it will automatically recycle the memory. That is to say, you just need to borrow something and don't have to pay it back, because it is your shift with someone, when you don't use it, you will return it. However, this process still exists, but it is just done by JRE.

But C is different. If you want to use it, you can use methods like malloc to apply for memory. When you are used up, because there is no shift, you need to return this memory by yourself, that is, call the Free Method to complete this task. Because the memory needs to be explicitly returned, when a function needs to return a piece of memory to the caller, the problem is more complicated, it is not as intuitive as object-oriented and Java with memory reclaim functions. There are several solutions for this problem in the C language:

(1) The caller allocates memory first and passes it as a parameter to the called function.

(2) Allocate in the called function and release it in the caller after use

(3) Use the static variable in the called function to return the variable

 

2. Object-oriented

Java has obvious object-oriented features, while C is an authentic structured language. Java has a string class string, which is called by the string. length () indicates the length of a string. However, in C, you need to call the strlen (STR) function to obtain the length of a string (character array. Since C is not an object-oriented language, it does not have the concept of this. Therefore, when using a function related to a "something, you need to pass the variable representing this "thing" as a parameter.

 

3. namespace

Java uses packages to implement namespaces. In C, all functions are in the same namespace, that is, there is no namespace, therefore, many Program-provided API functions have a prefix, such as MySQL before mysql_init (), mysql_real_connect (), and mysql_real_query _.

 

Appendix:The split function written in C language shows the memory management of C.

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>

Char ** split (char * STR, char * delim)
{
Static char ** str_arr = NULL;
Static char * CSTR = NULL;
If (str_arr)
{
Free (str_arr );
Str_arr = NULL;
}

If (CSTR)
Free (CSTR );

CSTR = (char *) calloc (strlen (STR) + 1, sizeof (char ));
Strcpy (CSTR, STR );

Char * P;
Int n = 0;
P = strtok (CSTR, delim );
While (P! = NULL)
{
N ++;
If (str_arr = NULL)
Str_arr = (char **) malloc (sizeof (char *));
Else
Str_arr = (char **) realloc (str_arr, N * sizeof (char *));
Str_arr [n-1] = P;

P = strtok (null, delim );
}
Str_arr = (char **) realloc (str_arr, (n + 1) * sizeof (char *));
Str_arr [N] = NULL;

Return str_arr;
}

Int main ()
{
Char * STR = "Please split this phrase into tokens ";
Char * delim = "";
Char ** str_arr;
Str_arr = Split (STR, delim );
While (* str_arr)
{
Printf ("/T % s/n", * str_arr );
Str_arr ++;
}

Return 0;
}

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.