Creating Variables

We need to create variable before we can use them. The syntax to create a variable is as follows :

data-type variable-name;
class VariablesDemo {
    public static void main (String[] args) {
        //When creating variable you specify the data type first
        //And then the variable name afterwards, separated by a space
        //This defines a variable called total
        int total;

        //defines a price variable that stores a number with a decimal place
        double price;

        //For variable to be useful we have to give them a value
        //To assign a value you use a single equal sign
        //Store the value 100 in total
        total = 100;

        //store the 105.80 in price
        price = 105.80;

        System.out.println("Total is " + total);
        System.out.println("Price is " + price);

        //Try creating a variable called age, give it your age and print it
    }
}
  

results matching ""

    No results matching ""