Module: (Java) Variables. Output formats


Problem

1/7

Variables

Theory Click to read/hide

A computer would not be needed if it were not possible to store various information in its memory and be able to process the same type of information using the same algorithms.
In order to create more interesting programs, you need to learn how to save information in computer memory. In this case, we need to learn how to somehow access the computer's memory cells.
In programming, as in life, so that access to any part of the computer’s memory occurs by name. Using this name, you can both read information and write it there.

A variable is a cell in the computer’s memory that has a name and stores a certain value that matches the type.

The word "variable" tells us that its meaning can change during the course of a program. When saving a new variable value - the old one is erased



For a computer, all the information is the data in its memory — sets of zeros and ones (if simpler, then any information on the computer is just numbers, and it processes them the same way). However, we know that integers and fractional numbers work differently. Therefore, in each programming language there are different types of data for the processing of which different methods are used.

For example,
- integer variables – type int, occupy 4 bytes in memory;
real variables that may have a fractional part (type float – floating point, or double - used by default), occupy 4 bytes in memory
characters (type char), usually occupy 1 byte in memory, but in Java 2 byte, because Unicode is used to encode characters.

Let's try to add some kind of variable to our program.
Before using a variable, you must tell the computer to allocate a place in memory for it. To do this, you must declare a variable, that is, indicate what type of value it will store, and give it a name.
Also, if necessary, you can assign it the initial values.

Let's analyze the program as an example
public class Main {  
    public static void main(String[] args) {  
        int a=6, b;  //two variables of integer type were declared in variable a and immediately saved the value 6. Variable b was not set to the initial value; what will be in the memory in this case we do not know.
    }
}
Now try it yourself!

Problem

In the third line, declare a variable a of integer type with an initial value of 7.