Some characteristics of variables in C, C++, Java, Python and JavaScript

Some characteristics of variables in C, C++, Java, Python and JavaScript

★Characteristics of variables in C language:

C language variables have types and need to be explicitly declared.

When declaring a variable in C language, the variable type must be defined before memory space can be allocated for it.

The scope of variables in C language can be global or local. Global variables are defined outside the function and can be accessed anywhere in the program; local variables are defined within the function and can only be used inside the function.

The life cycle of a variable in C language is determined by its scope. Local variables are created during function execution and destroyed after the function returns. Global variables exist throughout the running of the program.

In C language, variables can be assigned to various types of constants and variables. Operators can act on variables of different types, but they must be cast according to the variable type.

★ Characteristics of variables in C++ language:

C++ is an object-oriented programming language. It supports classes and objects. Variables need to declare their types explicitly. Variables can be defined using classes and objects.

In C++, variable declaration and initialization can occur at the same time. For example: int i = 0; means that an integer variable i is defined and initialized to 0.

C++ introduced reference variables. Reference variables associate one variable with another so that they can interact. Reference variables are commonly used for function parameter passing and return values. (Note: C language does not support reference variables.)

Variables in C++ support overloading, and the same variable name can correspond to different types of variables or functions.

The const and volatile keywords are used in C++ to define constants and volatile variables. Const constants cannot be modified while the program is running, while volatile variables represent variables that cannot be processed by the compiler, such as variables running in multiple threads.

Special note : In C and C++, the memory allocation of most variables is performed automatically, and there is no need to manually allocate and release memory. This includes local variables on the stack, function parameters, etc. When these variables go out of scope, their memory is automatically freed.

In C language, you can use the standard library functions malloc, calloc and realloc for dynamic memory allocation, and use the free function to release allocated memory. In C++, you can use the keywords new and delete to perform dynamic memory allocation and release.

★Characteristics of variables in Java language:

Java is an object-oriented programming language. It supports classes and objects. Variables need to explicitly declare their types. Variables can be defined using classes and objects.

The scope of variables in Java can be global or local. Global variables need to be defined using the static keyword, while local variables only exist in the code block in which they are located.

The size of variable types in Java is fixed, and their type and size are determined at compile time. The only things that can be dynamically resized are array variables.

The default value of a variable in Java is null or zero (0), depending on the type of the variable.

Java programmers cannot directly control the memory allocation and release when a Java program is running. The memory used by variables in Java is automatically allocated and released by the JVM.

★ Characteristics of variables in Python language:

Python is an interpreted language and does not require explicit declaration of variable types.

The type of variables in Python is dynamic, that is, the type of a variable is determined by the value assigned to it. A variable can change its type at any time without type conversion.

Variables in Python do not need to be declared in advance and can be defined directly when used.

Naming variables in Python does not need to follow specific rules. Python only requires that variable names cannot start with a number and cannot be Python reserved words.

The scope of variables in Python can be global or local. The scope of a variable defined inside a function is local, and if it is defined outside the function, it is global.

★Characteristics of variables in JavaScript language:

JavaScript is a dynamically typed language. There is no need to explicitly declare the type of variables. The types of variables are automatically deduced at runtime.

The scope of variables in JavaScript can be global or local. The scope of a variable defined inside a function is local, and if it is defined outside the function, the scope is global.

Naming variables in JavaScript does not need to follow specific rules, but unlike Python, variable names in JavaScript are case-sensitive.

Variables in JavaScript can be of any data type, including strings, numbers, Boolean values, objects, and functions.

Variables in JavaScript can change their type at any time without type conversion.

Special note : Variables in Python and JavaScript do not need to explicitly declare their type. This is because both languages ​​​​are dynamically typed - Python and JavaScript are dynamically typed languages, which means that variables do not need to be declared. Specify its type. However, the two languages ​​have very different strategies for handling mixed-type operations, such as adding a string and a number.

Python's behavior is strongly typed, which means that you cannot perform certain operations on different data types unless you explicitly convert the types. In the Python example you gave, one is the value '54' of type number, and the other is the value 'Hi' of type string. Python does not allow strings and numbers to be manipulated directly, so a + b will raise a TypeError error. If you want to add numbers and strings, you need to manually perform type conversion. For example, converting numbers to strings str(a) + b is completely legal.

JavaScript is relatively loose. When you try to operate on data of two different basic types, JavaScript will try to automatically perform type conversion (in this case, convert a number into a string). Therefore, in JavaScript: a + b will be converted to '54' + 'Hi' first, and finally the string '54Hi' will be obtained. This is a behavior called "weak typing". Therefore JavaScript is said to be a weakly typed language

Although both Python and JavaScript are dynamically typed, Python is still "strongly typed" (does not allow certain cross-type operations), while JavaScript is "weakly typed" (automatically converted and allows cross-type operations).

The following example illustrates that Python and JavaScript are dynamically typed languages, but they are still different.

Not allowed in Python:

a=54

b="Hi"

a+b #error report

str(a) + b # '54Hi'

Allowed in JavaScript:

a=54

b="Hi"

a+b // '54Hi'

There is no strict, generally accepted definition of "strongly typed" and "weakly typed". Like C++, it allows implicit conversion of certain types, such as int to double conversion, or implicit conversion of user-defined types through constructors and conversion operators. However, C++ is still fairly strict about type management. For example, you cannot arbitrarily convert a pointer to an integer, or convert a struct to an unrelated struct. This means that, in many cases, you need to perform type conversions explicitly. Therefore, we would say that C++ is a strongly typed language, although it allows some forms of implicit conversions.

For information about weak typing and strong typing of programming languages, please refer to https://blog.csdn.net/cnds123/article/details/115275931

Guess you like

Origin blog.csdn.net/cnds123/article/details/132479212