Skip to main content

Best Ways to Initialize variables in Java programming - 2022 reviewed



When the variables in the example above are declared, they have an undetermined value until they are assigned a value for the first time. But it is possible for a variable to have a specific value from the moment it is declared. This is called the initialization of the variable.

In C++, there are three ways to initialize variables. They are all equivalent and are reminiscent of the evolution of the language over the years:

The first one, known as c-like initialization (because it is inherited from the C language), consists of appending an equal sign followed by the value to which the variable is initialized:

type identifier = initial_value;
For example, to declare a variable of type int called x and initialize it to a value of zero from the same moment it is declared, we can write:

 
int x = 0;



A second method, known as constructor initialization (introduced by the C++ language), encloses the initial value between parentheses (()):

type identifier (initial_value);
For example:

 
int x (0);



Finally, a third method, known as uniform initialization, similar to the above, but using curly braces ({}) instead of parentheses (this was introduced by the revision of the C++ standard, in 2011):

type identifier {initial_value};
For example:

 
int x {0}; 



All three ways of initializing variables are valid and equivalent in C++.


// initialization of variables

#include <iostream>
using namespace std;

int main ()
{
  int a=5;               // initial value: 5
  int b(3);              // initial value: 3
  int c{2};              // initial value: 2
  int result;            // initial value undetermined

  a = a + b;
  result = a - c;
  cout << result;

  return 0;
}
 
print out
 
6 


Popular posts from this blog

Best chess matchup: Peaceful_Moon_4D vs IM Toro123

Fundamental data types

The values of variables are stored somewhere in an unspecified location in the computer memory as zeros and ones. Our program does not need to know the exact location where a variable is stored; it can simply refer to it by its name. What the program needs to be aware of is the kind of data stored in the variable. It's not the same to store a simple integer as it is to store a letter or a large floating-point number; even though they are all represented using zeros and ones, they are not interpreted in the same way, and in many cases, they don't occupy the same amount of memory. Fundamental data types are basic types implemented directly by the language that represent the basic storage units supported natively by most systems. They can mainly be classified into: Character types: They can represent a single character, such as 'A' or '$' . The most basic type is char , which is a one-byte character. Other types are also provided for wider charac...

Coding Tips you should know

Get started . Do not feel bad that you are not an expert programmer yet. In 10,000 hours, you will be. All you need to do is start. Dedicate some time each day or week to checking things off this list. You can take as long as you want or move as fast as you want. If you’ve decided to become a great programmer, you've already accomplished the hardest part: planting the seed. Now you just have to add time and your skills will blossom. If you need any help with any of these steps, feel free to email me and I'll do my best to help. Don't worry . Do not be imitated by how much you don't understand. Computers are still largely magic even to me. We all know that computers are fundamentally about 1s and 0s, but what the hell does that really mean? It took me a long time to figure it out–it has something to do with voltages and transistors. There are endless topics in computer science and endless terms that you won’t understand. But if you stick wit...