(Java) Variables. Output formats


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!

Let's try to write a calculator for primes

Our task is to display some arithmetic expression on the screen and make the computer count it.
For example, this:
5 + 7 = 12
Moreover, instead of 5 and 7, there may be different numbers, depending on the values of the variables a and b in the program.

In the output operator, you can output not just text, but also the values of variables, as well as the result of an arithmetic expression. Moreover, the output sequence may be different.

For example, you can output the above expression by writing:
System.out.print(a+"+"+b+"="+(a+b));
If you want to output the value of a variable, then you just need to specify its name without quotes. If we want to output the result of an arithmetic expression, then it is enough to simply write the arithmetic expression correctly.


*** Advanced material***
The number of output specifiers in the format string is not limited, the main thing for each qualifier is to list all the values that will be substituted for the template after the format string, by comma.
For example: System.out.printf("%d+%d=%d",a,b,a+b);
The format string indicates three specifiers for displaying integer values instead. The substitution order is direct:  instead of the first template %d, the value of the variable a is displayed; instead of the second, the value of the variable b; and instead of the third, the value of the expression a + b

Now let's write a program that outputs the result of basic arithmetic operations with these variables

In order for the user to be able to set the value of the variable himself, it is necessary to be able to input values from the keyboard. 
To do this, use the in object, which is written like this:
Scanner in = new Scanner(System.in); 
But for it to work, you must first import using
import java.util.Scanner

To get an integer value, use in.nextInt (); , the result of which must be assigned to a variable.
For example:
int a = in.nextInt();
For other data types, there are other methods:
  • next(): reads the input line to the first space
  • nextLine(): reads the entire entered line
  • nextInt(): reads the entered number int
  • nextDouble(): reads the entered number double
  • nextBoolean(): reads value boolean
  • nextByte(): reads the entered number byte
  • nextFloat(): reads the entered number float
  • nextShort(): сreads the entered number short

*** Advanced Material ( in Java ) ***
Additional output specifiers allow you to control the output of numbers in certain formats.

Minimum field width
Example:
%04d  - the number will be outputed in 4 positions, if the numbers are less than four, then the first will be zeros
int a=34; System.out.printf("%04d",a); //on the screen: 0 0 3 4   
The underscore is specially designed to visually display the output of the number.

%4d – same, only spaces instead of zeros
int a=34; System.out.printf(“%4d”,a); //on the screen: _ _ 3 4

Conclusion with a certain accuracy - used to output real numbers. By default, real numbers are displayed with an accuracy of 6 decimal places. But there are cases that need to be deduced with a different accuracy. In this case, it is necessary to indicate how much familiarity to allocate for the number itself and how much after the decimal point.

Example,
%9.3f   - the real number will be displayed in 9 positions, with three decimal places.

double a=34.24356; System.out.printf("%9.3f",a); // on the screen: _ _ _ 3 2 . 2 4 4 

Now, let's try it. 

*** Advanced material (printf() in Java)***

How to output the value of a variable on the screen?
For this, inside the format string in the output operator, you must specify a certain template, in the place of which the variable value will be outputed.
Which format specifier to use depends on the type of variable.
These patterns are called output format specifiers and are presented in the table. A large enough quantity is given by the specifier. Remembering all of them is optional.
In most cases, we will use qualifiers to output integers, real numbers, as well as characters and strings. But you can return to this lesson at any time and see the format specifier you need.

Here’s a quick summary of the available printf format specifiers:
%c character
%d decimal (integer) number (base 10)
%e exponential floating-point number
%f floating-point number
%i integer (base 10)
%o octal number (base 8)
%s a string of characters
%u unsigned decimal (integer) number
%x number in hexadecimal (base 16)
%% print a percent sign
\% print a percent sign

Additions:
To output variables of type short int, the h specifier is used (for example, \(\%hd\))
To output the values of variables of type long int, the l specifier is used (for example, \(\%ld\))
The specifier L can be used as a prefix before the specifiers e, f, g. It means that the screen outputs a value of type long double. (eg, \(\%Lf\))

Try it in practice!

 

To ensure that the output of a real number is guaranteed with a dot as a separator, you must use the American layout, which is connected using:
  Locale.setDefault(Locale.US);