View the features of C, C ++, C #, Java, and PHP through static local variables

Source: Internet
Author: User
The characteristics of C, C ++, C #, Java, and PHP are identified by static local variables.

The thinking on this problem comes from the implementation of the single-instance design mode in object-oriented design.

The Standard Code for implementing the single-sample mode in C ++ is as follows:

#include 
 
  int init(){    printf("init()\n");    return 22;}int GetTheOnly(){    static int x = init();      return x;}int main(){    int only = GetTheOnly();    return 0;}
 

In GetTheOnly (), the user stores the unique instance with static local variables and uses the init () function for dynamic initialization during initialization.

It looks so simple,However, the same code cannot be compiled as C.The compiler reports an error when compiling the static int x = init () line:

Error: The initial value element is not a constant.

Visible,When initializing static local variables in C language, values must be assigned with constants, that is, the initial values must be determined by the compiler..

For details, to call a function to initialize static variables, C ++ must ensure that init (); only run once. For this purpose, the C ++ compiler must add additional code. I can think of the C ++ compiler for static int x = init (); the possible added pseudo code is as follows:

    static int x;    static char flag = 0;    if(flag == 0){        x = init();
        flag = 1;    }    return x;
It can be seen that the C ++ compiler generates redundant assembly code for us in private. The result is that C ++ is easy to use, but not easy to understand.

Then I want to see how many other languages handle this problem. then I used C #, Java, and PHP to perform similar experiments and draw a small conclusion.

1 C #, Java does not support static local variables at all

Both support static member variables, but do not support static local variables in functions. Think about it. static local variables can almost always be replaced by static member variables.

C # test code:

using System;namespace ConsoleApplication2{    class Program    {        static int x = 0;        static int init()        {            Console.WriteLine("init()");            return 22;        }        static int GetTheOnly()        {            // static int x = 0;  static local variable is NOT supported by C#.            if(x == 0)            {                x = init();            }            return x;        }        static void Main(string[] args)        {            GetTheOnly();            GetTheOnly();        }    }}

Java test code:

public class t{    public static int init(){        System.out.println("init()\n");        return 22;    }    private static int x = 0;    public static int getTheOnly(){        //static int x = 0;  This line cannot be compiled, static local variable is NOT supported by Java        if(x==0){            x = init();        }        return x;    }    public static void main(String[] args) {        getTheOnly();        getTheOnly();    }}

2 PHP treats static local variables the same as C and only supports constant initialization.

PHP test code:

   
3. Thinking

The characteristics of these languages can be found through the very small language details of static local variables.

C ++ compilers are hardworking and comprehensive, and try to provide users with more language functions. to do this, you must secretly generate code for users, this leads to the complexity of the C ++ language and the "iceberg effect ". (Think about the multi-inheritance of C ++, the objects on the stack, the replication structure ...)


Java and C # focus on ease-of-use and avoid ambiguity. for the same function, only give users a correct choice. (Think about single inheritance, objects can only be built on the stack, and garbage collection)


C has always kept it concise, efficient, and transparent, and the compiler is honest, and the generated assembly can be predicted by the code.


The structured part of PHP imitates the C syntax, so many features are similar. although it is an explanatory language, especially the variable name and class name, it can be used as an explanatory language feature of the variable, make it abnormal and flexible. The object-oriented part imitates the Java syntax and fully embodies the characteristics of the explanatory language.

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.