Java Variables

Variables are data storage containers.

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

  • string - saves text, such as "Hello". Character unit values ​​are surrounded by two quotes
  • int - keeps numbers (whole numbers), excluding decimals, such as 123 or -123
  • float - keeps floating point numbers, with decimals, such as 19.99 or 19.99
  • char - saves single characters, such as 'a' or 'B'. Char values ​​are surrounded by single quotes
  • boolean - keeps values ​​in two forms: true or false

Declaring (Creating) Variables

To create a variable, you must specify a type and assign a value:


type variableName = value;
        

Where type is one of the Java variants (such as int or string), and variableName is a variable name (such as x or name). The equals sign is used for variable values.

To create an alternative that should keep the text, see the following example:


Example

Create a variable called name of type String and assign it the value "John":

String name = "John";
            System.out.println(name);
            

To create an alternative that should keep the number, see the following example:


Example

Create a variable called myNum of type int and assign it the value 15:

int myNum = 15;
            System.out.println(myNum);
            

You can also declare the variable without giving value, and provide value later:


Example
int myNum;
            myNum = 15;
            System.out.println(myNum);
            

Note that if you assign a new value to an existing variant, it will delete the previous value:


Example

Change the value of myNum from 15 to 20:

int myNum = 15;
            myNum = 20;  // myNum is now 20
            System.out.println(myNum);
            


Final Variables

However, you can add a final keyword if you do not want others (or yourself) to write over existing values ​​(this will declare variables as "last" or "continuous", meaning they are invariant and read only):


Example

Change the value of myNum from 15 to 20:

final int myNum = 15;
            myNum = 20;  // will generate an error: cannot assign a value to a final variable
            


Other Types

Demonstration of a flexible advertising method for other types:


Example
int myNum = 5;
            float myFloatNum = 5.99f;
            char myLetter = 'D';
            boolean myBool = true;
            String myText = "Hello";
            


Display Variables

The println() method is often used for dynamic displays.

To combine both text and flexibility, use the + character:


Example
String name = "John";
            System.out.println("Hello " + name);
            

You can also use the character + to add variables to other variables:


Example
String firstName = "John ";
            String lastName = "Doe";
            String fullName = firstName + lastName;
            System.out.println(fullName);
            

In numerical values, the character + acts as a mathematical operator (note that we use the int (number) variable here):


Example
int x = 5;
            int y = 6;
            System.out.println(x + y); // Print the value of x + y
            

In the example above, you can expect:

  • x saves the number 5
  • y saves the number 6
  • Then use the println() method to display the number x + y, which is 11

Declare Many Variables

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


Example
int x = 5, y = 6, z = 50;
        
        System.out.println(x + y + z);
            


Java Identifiers

All Java variables must be identified by a different name.

These unique names are called identifiers.

Identifiers can be short words (like x and y) or multiple descriptive words (age, total, volume).

Note: It is recommended to use descriptive words to create understandable and sustainable code:


Example
// Good
            int minutesPerHour = 60;
            
        // OK, but not so easy to understand what m actually is
        int m = 60;
        

The general rules for flexible design are:

  • Names can contain letters, digits, underscore, and dollar symbols
  • Words should start with a letter
  • Words should start with a lower case letter and may not contain white space
  • Names can also be started with $ and _ (but we will not use it in this course)
  • Highly sensitive words ("myVar" and "myvar" are different variations)
  • Saved words (like Java keywords, such as int or boolean) cannot be used as words