Summary: In this tutorial, you will learn with examples about variables and literals in Java.

Variables in Java

In Java, a variable is a named location in memory that is used to store a value. Variables have a specific type, which determines the kind of value that can be stored in them and the operations that can be performed on them.

There are several types of variables in Java, including primitive types (such as int, float, and boolean) and reference types (such as objects and arrays). We will study reference type in more detail in later posts.

Here is an example of declaring and initializing primitive variables in Java:

int age = 30;         // an integer variable
double height = 1.75; // a floating-point variable
char initial = 'J';   // a character variable
boolean married = true; // a boolean variable
String name = "John Smith"; // a string variable

In this example, the variables age, height, initial, married, and name are all declared and initialized on the same line. The type of the variable is specified before the variable name, and the value is assigned using the assignment operator =.

Rules for Naming a Variable in Java

In Java, variable names (also known as identifiers) follow certain rules in order to be valid. Here are the main rules for naming variables in Java:

  1. Variable names must begin with a letter, an underscore (_), or a dollar sign ($).
  2. After the first character, variable names can contain letters, digits, underscores, and dollar signs.
  3. Variable names are case-sensitive, which means that age and Age are considered to be different variables.
  4. Variable names cannot contain spaces or other special characters, except for the underscore and dollar sign.
  5. Variable names cannot be the same as a Java keyword.

Here are some examples of valid and invalid variable names in Java:

  • age (valid)
  • _private (valid)
  • $salary (valid)
  • first_name (valid)
  • 1st_name (invalid, cannot start with a digit)
  • first name (invalid, cannot contain spaces)
  • #value (invalid, cannot contain special characters other than underscore and dollar sign)
  • public (invalid, cannot be the same as a Java keyword)

It is good practice to choose descriptive and meaningful names for your variables, as this can help to make your code easier to read and understand.

It is also common to use a naming convention, such as camelCase or snake_case, to make the names more readable.

What is the use of Variables in Java?

Some of the main uses of variables in Java are:

  • Storing values that are used repeatedly in the program, such as constants or user input
  • Holding temporary results of calculations or other operations
  • Controlling the flow of the program by changing the values of variables based on certain conditions
  • Storing and manipulating data structures, such as arrays and objects

Here is an example of using variables in a simple Java program:

public class Calculator {
    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 20;
        int sum = num1 + num2;
        System.out.println("The sum is: " + sum);
    }
}

In this example, the variables num1 and num2 are used to store the values that will be added together, and the sum variable is used to store the result of the addition.

The sum variable is then printed to the console using the println method.

Literals in Java

Java also has a concept of literals, which are fixed values that are written directly in the code. For example, the values 30, 1.75, 'J', true, and "John Smith" in the above example are all literals.

There are different types of literals for each of the different variable types in Java. Here are some examples of literals for each of the primitive types:

  • Integer literals: 10, -20, 0
  • Floating-point literals: 3.14, -2.718, 0.0
  • Boolean literals: true, false
  • Character literals: ‘a’, ‘Z’, ‘\n’ (newline)
  • String literals: “hello”, “goodbye”, “abc123”

In addition to the basic types of literals, Java also supports a number of advanced literals, such as hexadecimal integers, binary integers, and floating-point numbers in scientific notation.


Leave a Reply