Java - tutorial - 02/13 - variables

revision:


variables

variables are containers for storing data values.

In Java, there are different types of variables, for example:

string - stores text, such as "Hello". String values are surrounded by double quotes;
int - stores integers (whole numbers), without decimals, such as 123 or -123;
float - stores floating point numbers, with decimals, such as 19.99 or -19.99;
char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes;
boolean - stores values with two states: true or false.

To declare (create) a variable you must specify the type and assign it a value.

syntax: type variableName = value;, where type is one of Java's types (such as int or String), and variableName is the name of the variable (such as x or name). The equal sign is used to assign values to the variable.

example: variable that stores text

        public class Main {
            public static void main(String[] args) {
              String name = "John";
              System.out.println(name);
            }
          }
    

example: variable that stores a number

        public class Main {
            public static void main(String[] args) {
              int myNum = 15;
              System.out.println(myNum);
            }
          }
    

Note that if you assign a new value to an existing variable, it will overwrite the previous value.

example: overwriting previous value

        public class Main {
            public static void main(String[] args) {
              int myNum = 15;
              myNum = 20;  // myNum is now 20
              System.out.println(myNum);
            }
          }
    

However, you can add the final keyword if you don't want others (or yourself) to overwrite existing values (this will declare the variable as "final" or "constant", which means unchangeable and read-only).

example: adding "final" keyword

        public class Main {
            public static void main(String[] args) {
              final int myNum = 15;
              myNum = 20; // will generate an error
              System.out.println(myNum);
            }
          }
    

declaring other types

example: how variables of other types are declared:

        public class Main {
            public static void main(String[] args) {
                int myNum = 5;
                float myFloatNum = 5.99f;
                char myLetter = 'D';
                boolean myBool = true;
                String myText = "Hello";
            }
          }
    

The println() method is often used to display variables. To combine both text and a variable, use the + character. You can also use the + character to add a variable to another variable:

example: combining text and variable:

        public class Main {
            public static void main(String[] args) {
              String name = "John";
              System.out.println("Hello " + name);
            }
          }
    

example: add variable to another variable:

        public class Main {
            public static void main(String[] args) {
              String firstName = "John ";
              String lastName = "Doe";
              String fullName = firstName + lastName;
              System.out.println(fullName);  
            }
          }
    

For numeric values, the + character works as a mathematical operator.

example: + as mathematical operator (notice that we use int (integer) variables here):

        public class Main {
            public static void main(String[] args) {
              int x = 5; // x stores the value 5
              int y = 6; // y stores the value 6
              System.out.println(x + y); // Print the value of x + y, which is 11.
            }
        }
    

To declare more than one variable of the same type, use a comma-separated list.

example: :

        public class Main {
            public static void main(String[] args) {
              int x = 5, y = 6, z = 50;
              System.out.println(x + y + z);
            }
          }
    

All Java variables must be identified with unique names, which are called identifiers. Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume). It is recommended to use descriptive names in order to create understandable and maintainable code.

The general rules for naming variables are:

names can contain letters, digits, underscores, and dollar signs;
names must begin with a letter;
names should start with a lowercase letter and it cannot contain whitespace;
names can also begin with $ and _;
names are case sensitive ("myVar" and "myvar" are different variables);
reserved words (like Java keywords, such as int or boolean) cannot be used as names.