JavaRush /Java Blog /Random EN /Java Variables and Constants

Java Variables and Constants

Published in the Random EN group
To understand the Java language, it is very important to understand its syntax. One of its key, fundamental building blocks is variables.

What is a variable in Java

A variable in Java is a container that can store some data value for later use in a program. Essentially, a variable is the smallest indivisible unit of a Java application. Variables in Java come in two types: those intended for small data (primitive variables) and for more complex, heavy ones (reference variables). Today we will look at the first case when variables store the data value itself. Such variables are called primitive. Primitive variables in Java and constants - 1

Declaring Variables in Java

Let's look at this example:
int x = 9;
Let's look at what we see: int- the type of variable that describes integers located in the range -2147483648 to 2147483647 x- the name of the variable (we need to distinguish them from each other, right?) = the assignment sign to some variable, some value 9- its immediate value is ;the end of this command. Now let’s put everything together: we specify that a variable of type intwith a name xhas a value 9. This example has an abbreviated form, the full one looks like this:
int x;
x = 9;
In the first line we see the declaration of a variable and assigning it a name, that is, with this we tell the JVM to allocate space for the variable int(4 bytes) and give it a name x. In the second we give it the value 9. Before this, it had a default value, namely 0. It is worth saying a few words about naming variables. Typically they are written in lower camel style. That is, for example, if we have a variable describing the number of people (count of people), an appropriate name for it would be:
int countOfPeople;
In this name, the first word begins with a lowercase (small) letter, and each subsequent word begins with a capital (capital) letter. This is done to make these names easier to read, since variable names usually consist of more than one word.

Redefining Variables

Let's return to our variable declaration example:
int x = 9;
If we once put a value into a variable, this does not mean that during program execution the variable xwill always have a value 9. We can rewrite it:
x = 4;
Everything is almost the same, but we no longer add the type ( int), because it is registered when declaring a variable (declaring its existence). Next, we will only reuse it, as for example here we see it being overwritten (we set our variable a new value, overwriting the old one). Let's assume that we also have a variable:
int y = 7;
And by setting: x = y; The old value of the variable xwill be deleted, overwritten by a copy of the value y, namely - 7. You can also set the value of some other variable, increased by the number we need:
x = y + 5;
Since our variable ywas equal to 7, the result xwill be equal to 12. More interesting is the ability to perform these actions:
x = x + 6;
What do we see here? The variable xis given a value equal to the past increased by 6, that is, it will be: 12 + 6 = 18. The same entry can be shortened by omitting x:
x =+ 6;

Types of Variables

An application in Java consists of classes and objects. Let's look at what Java variables are:
  • object variables;
  • local variables;
  • class variables.
Primitive variables in Java and constants - 2As an example class, we will take the dog class with the “bark” method:
public class Dog {
   public void bark() {

Object Variables

Variables are declared in a class, but not in a method, constructor, or block.
public class Dog {
public  int value = 9;
   public void bark() {
To call this variable, we need to first create an object:
Dog dog = new Dog();
Variables are created only after the object is created (that is, after the object is created using new). In an object, internal variables are always visible to all methods, constructor, or anything within that same object. As mentioned above, object variables have default values. For a number, the default value is 0, for logical (boolean) - false, for references to an object - null.

Local variables

These variables are declared in methods, constructors or blocks.
public class Dog {
   public void bark() {
   int value = 9;
Local variables exist only in the called block of code, at the end of which they are removed from memory. They are visible only within the declared method, constructor , or block. That is, you cannot use a variable, for example, in another method. Access modifiers cannot be used for local variables. What is the point of them if the variable is not visible beyond the boundaries of the method? This type of variable is created when a method (or constructor, or block) is called and destroyed when it completes.

Class Variables

This type of variable is also called static. They are declared with a modifier word static, but outside the scope of the method, constructor, or block.
public class Dog {
public static  int value = 9;
   public void bark() {
Call our variable:
Where Dogis the name of the class to which the variable is attached. The default value is the same as for object variables. For numbers the default is 0, for boolean it is false; for object references - null. There is always one static variable, no matter how many objects are created from the class, because it is attached only to the class. Class variables are created when a program starts and are destroyed when the program completes execution. Static variables in Java are often used when they are declared as constants. Let's talk about them in more detail.

What are constants in Java

A constant is a given, fixed value that should not change. What are constants in programming? This is some constant value that is known before the application starts running, and it is set in the code once. In Java, constants are variables denoted by a special word - final:
final int VALUE = 54;
Here we get a variable that cannot be changed after setting it to any value. The point is that constants are much simpler than variables. They are always uniquely defined and never change. A little higher we talked about naming variables, and we should pay attention to the peculiarities of naming constants. In constant variables, all words are written in CAPITAL letters, separated by an underscore. _ For example, we need a constant that describes the maximum value for something:
final int MAX_VALUE = 999;
So, to summarize: A variable is provided to us as a place to store our data (or location address), which allows us to control the application. Variables of a primitive type have a specific data format, size, and range of values ​​that can be stored in memory. Variables can be divided by location: object, local, class variables. There is a special type of variables - constants, which can be global variables. Their scope is the entire program.