Skip to main content

Popular posts from this blog

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

Java Literals

Literals are the most obvious kind of constants. They are used to express particular values within the source code of a program. We have already used some in previous chapters to give specific values to variables or to express messages we wanted our programs to print out, for example, when we wrote:   a = 5; The 5 in this piece of code was a literal constant . Literal constants can be classified into: integer, floating-point, characters, strings, Boolean, pointers, and user-defined literals. Integer Numerals 1 2 3 1776 707 -273 These are numerical constants that identify integer values. Notice that they are not enclosed in quotes or any other special character; they are a simple succession of digits representing a whole number in decimal base; for example, 1776 always represents the value one thousand seven hundred seventy-six . In addition to decimal numbers (those that most of us use every day), C++ allows the use of octal numbers (base 8)

Introduction to strings

Fundamental types represent the most basic types handled by the machines where the code may run. But one of the major strengths of the C++ language is its rich set of compound types, of which the fundamental types are mere building blocks. An example of compound type is the string class. Variables of this type are able to store sequences of characters, such as words or sentences. A very useful feature! A first difference with fundamental data types is that in order to declare and use objects (variables) of this type, the program needs to include the header where the type is defined within the standard library (header <string> ): // my first string #include <iostream> #include <string> using namespace std; int main () { string mystring; mystring = "This is a string" ; cout << mystring; return 0; }   printout:   This is a string As you can see in the previous example, strings can be initialized with any valid string l