To write programs in Java, you need to know its syntax. With this lesson, we begin our acquaintance with the basics of the language. In this tutorial, we’ll cover: what are variables in java and how to declare them, how to name variables correctly.
What are the data types in java –
- Primitive data types,
- Reference data types,
- Consider the String type separately.
In the previous lessons, we talked about the fact that a class in Java consists of attributes and methods. Variables can be class attributes, method parameters, or can be used in a program for short-term data storage. In the Java language, all variables must be declared before they can be used.
Variable Declaration In Java
When declaring a variable, the following sequence indicates:
- DataType (in this example – int – the variable contains an integer),
- Variable name (in this example, names are x and y ),
- The initial value of the variable or, in other words, the initialization of the variable. In this example, variables x and y are assigned the values 1 and 2. However, this is not required when declaring a variable.
Example: declaring variables without initialization :
After each line when declaring variables, you must put a semicolon “;”.
If you need to declare several variables of the same type, then this can also be done in one line, specifying the variable names separated by commas.
Types of variables
A variable gives us named capacity that our code can control. Every variable in Java has a particular sort, which decides the size and format of the variable’s memory; the scope of values that can be put away inside that memory; and the set of operations that can be connected to the variable. You must make an explicit declaration of all variables before they can be utilized. Variables can be declared in the following manner:
Data type <variable name>;
Here data type is one of Java’s datatypes. On the other hand, a variable is a name or the identifier associated with the variable. To pronounce more than one variable of the pointed out type, you can utilize a comma-divided rundown. Here are a few examples of declarations:
The following declaration declares three integer variables.
int x, y, z;
In a similar manner, variables of other data types may also be declared. Java supports three types of variables. These types are as follows:
- Class/static variables
- Instance variables
- Local variables
- Local variables are announced in systems, constructors, or scopes.
- Local variables are made when the constructor or method is entered and the variable will be decimated once it retreats the system, constructor or scope.
- Access modifiers can’t be utilized for neighbourhood variables.
- Local variables are noticeable just inside the announced method, constructor or scope.
- Local variables are executed at the stack level.
- There is no default value for these variables. So, local variables ought to be declared and a beginning value ought to be relegated before the first utilization. Sample Implementation:
- Here, age is a neighbourhood variable. This is characterized inside pupage() strategy and its
degree is constrained to this system just.
- The declaration of an instance variable is made inside the class. However, it is made outside the system, constructor or any scope.
- Instance variables are made when an object is made with the utilization of the keyword “new” and obliterated when the item is destroyed.
- When space is dispensed for an item in the memory, an opening for each one variable value is made.
- Instance variables can be pronounced in the class level before or after utilization.
- Instance variables hold values that must be referenced by more than one method, constructor or piece, or key parts of an object’s express that must be available all through the class.
- Access modifiers can be given for sample variables.
- Instance variables have default values. For numbers, the default quality is 0.
- However, for Booleans, it is false and for object references, it is invalid. Qualities can be relegated amid the statement or inside the constructor.
- The case variables are unmistakable for all methods, constructors and scope in the class. Regularly, it is prescribed to make these variables private (access level).
- However, permeability for subclasses can be given with the utilization of access modifiers for these variables.
- Instance variables can be gotten to by calling the variable name inside the class.
- Class variables otherwise called static variables are declared with the static keyword in a class, yet outside a constructor, method or scope.
- There would just be one duplicate of each class variable for every class, paying little mind to what number of objects are made from it.
- Static variables are seldom utilized other than being pronounced as constants.
- Constants are variables that are announced as private/public, static and final.
- Consistent variables never show signs of change from their introductory quality.
- Static variables are put away in static memory. It is uncommon to utilize static variables other than announced final and utilized as either private or public constants.
Static variables are made when the system begins and annihilated when the
- execution stops. Visibility is like instance variables. In any case, most static variables are announced public since they must be accessible for clients of the class.
- Default values for these variables are also same as instance variables. For numbers, the default value id typically 0. However, the same value for Booleans is false and for object reference is invalid.
- Values can be doled out amid the assertion or inside the constructor. Furthermore, values can be appointed in a unique static initializer
- Static variables can be gotten to by calling with the class name.
- When announcing class variables as public static final, variables names (constants) must all be in upper case. Moreover, the static variables are not public and the
the naming convention is the same as local and instance variables.
Variable Naming Rules in Java
- The variable name must begin with a letter (small) and consist of letters (Unicode) numbers and the underscore “_”. It is technically possible to start a variable name with “$” or “_” as well, but this is prohibited by the Java Code Conventions. Also, the dollar symbol “$” is never used by convention at all. By convention, a variable name must begin with a lowercase letter (class names begin with a capital letter). Spaces are not allowed when naming variables.
The variable name must not be a Java keyword or a reserved word.
- The variable name is case sensitive. newVariable and newvariable are different names.
- When choosing variable names, you should use full words instead of cryptic abbreviations. This will make your code easier to read and understand. In many cases, this will also make your code self-documenting.
- If your chosen variable name consists of only one word, write it down in small letters. If it consists of more than one word, separate each subsequent word in the variable name with a capital letter. For example: superCounter, myDomesticAnimal.
- If the variable is constant, then each word should be capitalized and separated with an underscore. Example: static final int NUMBER_OF_HOURS_IN_A_DAY = 24.