Java Variable

18-Sep-2024

Learn about declaring, initializing, and using variables in Java programming.



A variable is a box or container for storing different types of data. In Java, variables are containers that hold values ​​and have a specific data type that determines the type of data they can store. Variables in Java are used to store and manipulate data within programs. There are some rules to follow when creating variables. Here are some important points about Java variables:


1. Declaration:

  • Declare a variable


To declare a variable first specify the data type then enter the name of the variable you want, in my case myNumber is the variable name.


For example:



// data_type variable_name ;
int myNumber;


Here int is the data type where only int type values ​​can be stored. and myNumber is the name of the variable.

2. Initialization:

  • After declaring


After declaring a variable, you must initialize it before using it. Variables are assigned values.

While assigning value to the variable, first write the name of the variable, then ( = ) equal sign, then the value you want to store, and finally a ( ; ) semicolon have to give.


For example



// variable_name = value;
myNumber = 10;


  • Declare and initialize together


to declare and initialize a variable together first, specify the data type write a name for the variable then use the = (equal sign) then assign a value according to the data type.



//data_type variable_name = value;
int myNumber = 10;


3. Data Types:


Java has various data types such as Examples: int (integer), double (floating point), char (character), boolean (true/false), etc.


int integerValue = 42;
double doubleValue = 3.14;
char charValue = 'A';
boolean booleanValue = true;


  • int variables are used to store integers like 3340, 263, etc.
  • float variables are used to store decimal numbers like 30.2634, 236.330, etc.
  • char variables are used to store single characters like a, b, a, b, etc, and the single quotation is used to place the text of the variable.
  • boolean variable is used only when true or false needs to be stored. the boolean variable has two values ​​true and false.


4. Naming Rules:
 
  There are some naming rules for variables. such as

The first letter should be a letter, not a number, not a special character. example name, id, gender, etc.


If the name contains multiple words, then you should use camelCase (every word's first character should be uppercase except the first word). example firstName, lastName, etc.


Don't use special characters like #, %, &, etc. If needed you can use underscore ( _ ). 


Don't use space


Java is case-sensitive, so myVariable and myvariable are different.


Don't use keywords as a variable name


    5. Constants:


    Constant variable means which variable value cannot be changed.

    You can use the final keyword to declare constants.
    constant variable value should be assigned when declaring. you can not assign value later.




    // final data_type variable_name = value;
    final float VALUE_OF_PI = 3.1416f;

    The constant Variable naming convention is all the letters should be uppercase and words be separated by an underscore ( _ ).

    6. Scope:

    The scope of a variable defines where the variable can be accessed.


    Local variables are declared within a method or block and can only be accessed within that scope.



    Instance variables belong to instances of a class, and class variables are shared by all instances of a class.



    public class Example {
        int instanceVariable; // Instance variable

        public void exampleMethod() {
            int localVar = 5; // Local variable
        }
    }

    These are some basic concepts related to Java variables.

    Understanding these principles is essential to effective Java programming.

    Comments