Viewing the characteristics of c,c++,c#,java,php by static local variables

Source: Internet
Author: User

0 origin of the problem

The consideration of this problem comes from the implementation of the single-instance design pattern in object-oriented design.

The standard code for the singleton pattern implementation in C + + is:

#include <stdio.h>int init () {    printf ("init () \ n");    return 22;} int gettheonly () {    static int x = init ();      return x;} int main () {    int only = Gettheonly ();    return 0;}

In the Get instance function gettheonly (), the static local variable user stores a unique instance and initializes it dynamically using the init () function directly.

It seems so simple, but the same code is compiled as C but cannot pass , the compiler compiles the static int x = init () This line times wrong:

Error: initializer element is not constant

Itcan be seen that static local variables in the C language must be initialized with constants, meaning that the initial value must be determined during compilation .

Consider, in order to invoke a function to initialize a static variable, C + + must guarantee init (); For this purpose, the C + + compiler must add extra code, and I can think of the C + + compiler for the static int x = init (); The pseudo-code that may be added is as follows:

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

In order to see how the other languages dealt with the problem, a similar experiment was followed using c#,java,php, and a small conclusion was drawn.

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

Both support static member variables and do not support static local variables within functions. Think about it too, static local variables can almost always be replaced with static member variables.

C # test Code:

Using System;namespace consoleapplication2{    class program    {        static int x = 0;        static int init ()        {            Console.WriteLine ("Init ()");            return;        }        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;    }    private static int x = 0;    public static int gettheonly () {        //static int x = 0;  This line cannot was 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, only supports constant initialization

PHP Test Code:

<?phpfunction init () {    echo "init () \ n";    return 22;} function Gettheonly () {    //static $x = init ();  PHP only supports initializing static local Vairalbel with constant.    static $x = 0;    if ($x ==0) {        $x = init ();    }    return $x;} Gettheonly (); gettheonly ();

31-Point thinking

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

C + + compiler is a full-time industrious all-rounder, as far as possible to provide users with more language features, and do these must secretly generate code for users, resulting in C + + language complexity and "iceberg effect." (Think of C + + multiple inheritance, stack objects, copy constructs ...) )


Java and C # are focused on ease of use, to avoid ambiguity, for the same function, only give the user a correct choice. (Think of single inheritance, objects can only be built on the heap, garbage collection)


C always keeps its simplicity and transparency, the compiler wussy, and sees that the code can basically predict the resulting assembly.


The structural part of PHP mimics C's syntax, so many features are similar, but after all it is explanatory language, especially variable name, class name and so on itself can be used as the explanatory language characteristics of variables, so that it becomes extraordinarily flexible. The object-oriented part imitates the Java syntax, and it fully embodies the characteristics of the interpretation language.

Viewing the characteristics of c,c++,c#,java,php by static local variables

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.