Java Variables

https://w‮.ww‬theitroad.com

In Java, a variable is a container that holds a value of a particular type, such as an integer, a floating-point number, a string, or an object. Variables are used to store and manipulate data in a program.

Java Variables Naming Rules

In Java, variables are named using identifiers. Identifiers are names given to variables, classes, methods, and other program elements.

Here are some rules to follow when naming Java variables:

  1. Identifiers must start with a letter, underscore (_), or dollar sign ($). They cannot start with a number.

  2. Variable names can contain letters, numbers, underscores, and dollar signs. They cannot contain spaces or special characters such as @, #, or %.

  3. Variable names are case sensitive. For example, "myVariable" and "MyVariable" are considered two different variable names.

  4. Variable names should be descriptive and meaningful. Avoid using single-letter variable names or names that are too generic, such as "x", "y", or "value".

  5. Reserved words cannot be used as variable names. Reserved words are words that have a special meaning in Java, such as "int", "double", "class", and "if".

  6. Constant variables should be named using all capital letters. This makes it easier to distinguish them from regular variables. For example, "final int MAX_VALUE = 100;".

  7. Camel case is a commonly used naming convention for variables in Java. In camel case, the first word of the variable name starts with a lowercase letter, and the first letter of each subsequent word is capitalized. For example, "myVariableName" or "numberOfStudentsInClass".

Create Variables in Java

create a variable in Java

int numberOfStudents = 20;

declare variables and assign variables separately

int numberOfStudents ;
numberOfStudents = 20;

Change values of variables

int numberOfStudents = 20;
numberOfStudents = 30;
  1. Instance variables - An instance variable is a variable that is declared within a class, but outside of any method or block. It is accessible to all methods and blocks within the class, and it retains its value between method calls. Instance variables are initialized to a default value if no value is specified. Example:
public class MyClass {
   int x = 10;  // declaring and initializing an instance variable
   public void myMethod() {
      System.out.println(x); // prints 10
   }
}
  1. Static variables - A static variable is a variable that is declared with the "static" keyword. It is associated with the class, rather than with any instance of the class. Static variables are shared by all instances of the class, and they are initialized to a default value if no value is specified. Example:
public class MyClass {
   static int x = 10;  // declaring and initializing a static variable
   public void myMethod() {
      System.out.println(x); // prints 10
   }
}

In Java, variables must be declared with a data type, such as int, float, double, String, or any other valid data type. The data type specifies the kind of values that the variable can hold. For example, an int variable can hold integers, while a String variable can hold strings.