Hey Guys welcome to today's blog. Today we talk more on variables and hopefully constants in the next blog.
Variables
Variables are basically storage spots in your program that are designed to hold values that change over time. Before a variable is used, it must be declared first. Anytime we declare a variable, we tell Java two things: the type of value the variable holds and the name assigned to that variable.
A declaration is not the same as assigning values to the variable. You could declare a variable and assign a value to it later. to declare a variable the syntax is type variableName;
The type represent to thte specific data tye being stored in the variable and the variable name is the identifier that will be used to recognize this particular variable in your code.
It is important to note that we use ;
to mark the end of a declaration as it is a complete statement in Java.
Important things to note about variable declarations
In Java almost everything is case-sensitive, including variable names. Variable name Salary and salary are different identifiers in Java.
You could use letters, digits (just not as the first character), and even underscores to name variables. However, anything that is not considered under the Unicode system as letters, digits, currency symbols, or connector punctuation cannot be used in variable names such as ; +, # ...
In the new version of Java, underscores could not be used as variable names alone. They may be later used as a wildcard just like in other languages like Python.
When we talk about initializing a variable we are basically assigning a value to that variable. The value may be of any data type. Initialization is said to be done only the first time. Any other time is just altering the value or reassigning a new value to it.
If you are going to reassign a value to a variable it must be of the same type as the variable.
int number; //this is a declaration
number = 26 // this is an initialisation
int number = 26; // this is a declaration + initialization, same line
number = 300; // this is a reassignment
You could also declare multiples variables on the same line. For example; int i, j;
as long as they are of the same type.