JavaRush /Java Blog /Random EN /Variables in Java and constants

Variables in Java 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 are of two types: designed for small data (primitive variables) and for more complex, heavy data (reference variables). Today we will consider the first case when the variables store exactly the value of the data. Such variables are called primitive. Primitive variables in Java and constants - 1

Declaring Variables in Java

Let's look at an example:

int x = 9;
Let's analyze what we see: int- the type of the variable, which describes integers that are in the interval -2147483648 to 2147483647 x- the name of the variable (we need to distinguish them from each other, right?) = The assignment sign of some variable, some value 9- its immediate value ;end of this command Now let's put it all together: we specify that a variable of type int, named xhas the value 9. This example has an abbreviated notation, the full one looks like this:

int x; 
x = 9;
In the first line, we see the declaration of a variable with a name assigned to it, that is, by doing 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 a value 9. Prior to that, it had a default value, namely 0. It is worth saying a few words about variable naming. As a rule, they are written in lower camel style. That is, for example, if we have a variable describing the number of people (count of people), a suitable 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 (large). This is done for the convenience of reading such names, since usually variable names consist of more than one word.

Redefining Variables

Let's go back to our variable declaration example:

int x = 9;
If we once put some value into a variable, this does not mean that during the execution of the program the variable xwill always have the value 9. We can overwrite it:

x = 4;
Everything is almost the same, but we no longer add the type ( int), because it is written when declaring a variable (declaring its existence). Further, we only reuse it, as for example here we see its overwriting (we set our variable to a new value, overwriting the old one). Suppose we also have a variable:

int y = 7;
And by setting: x = y; The old value of the variable xwill be deleted, overwritten with 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 is made up of classes and objects. Consider what Java variables are:
  • object variables;
  • local variables;
  • class variables.
Primitive variables in Java and constants - 2As an example class, we will take a dog class with a “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();
dog.value;
Variables are created only after the object has been created (that is, after the object has been created with new). In an object, internal variables are always visible to all methods, constructor, or anything inside that same object. As mentioned above, object variables have default values. For a number, the default value is 0, for booleans it is false, for object references it is 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 on local variables. What is the point of them if the variable is not visible beyond the boundaries of the method? This kind of variable is created when a method (or constructor, or block) is called and destroyed upon completion.

class variables

This type of variable is also called static. They are declared with the modifier word static, but outside of a method, constructor, or block.

public class Dog {
public static  int value = 9;
   public void bark() {
   }
}
We call our variable:

Dog.value
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. A static variable is always the same, no matter how many objects are created from the class, because it is attached only to the class. Class variables are created when the program starts and are destroyed when the program ends. 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 start of the application, while 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 once it has been given a value. The fact 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;
Primitive variables in Java and constants - 3So, to recap: A variable is provided to us as a place to store our data (or the address of a place), which allows us to control the application. Variables of a primitive type have a specific data format, size, range of values ​​that can be stored in memory. Variables can be separated by location: object variables, local variables, class variables. There is a special kind of variables - constants, which can be global variables. Their scope is the entire program.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION