Table of Contents

    Java Variable Declaration and Initialization: Best Practices

    Java Variable Declaration and Initialization: Best Practices

    Variable Declaration and initialization

    Variables are used to represent values that may be changed in the program. In Java, all variables must be declared before they can be used. The basic form of a variable declaration is shown here:

    type identifier [ = value][, identifier [= value] ...] ;
    • The type is one of Java’s primitive types or the name of a class or interface.
    • The identifier is the name of the variable. You can initialize the variable by specifying an equal sign and a value. Keep in mind
    • You can initialize the variable by specifying an equal sign and a value. Keep in mind that the initialization expression must result in a value of the same (or compatible) type as that specified for the variable.
    • To declare more than one variable of the specified type, use a comma-separated list.

    int Variable Declaration and Variable Initialization in two steps:

    Save Source File Name as : IntExample1.java
    To compile : javac IntExample1.java
    To Run : java IntExample1

    Example :

    // A Java program to demonstrate int data type
    class IntExample1 {
       public static void main(String args[])
         {
               int age;  // Declaration of variable age
               age = 20;  // initialization of the variable age
               System.out.println(age);
         }
      }
    Output :
    20
     press any key to continue ...

    Types of variable with respect to data type:

    Variable Delcaration:

    Here are examples of how to declare variables of all the primitive data types in Java:

    data_type variable_name ;
    byte    myByte;
    short   myShort;
    char    myChar;
    int     myInt;
    long    myLong;
    float   myFloat;
    double  myDouble;

    Here myByte, myShort, myChar, myInt, myLong, myFloat, myDouble all are variable name which have data type byte, short, char, int, long, float, double respectively


    Here are examples of how to declare variables of the object types in Java:

    Byte       myObjectByte;
    Short      myObjectShort;
    Character  myObjectChar;
    Integer    myObjectInt;
    Long       myObjectLong;
    Float      myObjectFloat;
    Double     myObjectDouble;
    String     myObjectString;

    Notice the uppercase first letter of the object types.

    Here myObjectByte, myObjectShort, myObjectChar, myObjectInt, myObjectLong, myObjectFloat, myObjectDouble all are variable name which have data type Byte, Short, Char, Int, Long, Float, Double respectively

    Variable initialization:

    Here are examples of how to variables are initialize to all the primitive data types in Java:

    myByte = 21 ;
    myShort = 22;
    myChar = 'A';
    myInt = 232 ;
    myLong = 2121314312321;
    myFloat = 23.54f ;
    myDouble = 12441124124.4343;

    Here are examples of how to initialize variables of the object types in Java:

    myObjectByte = 21 ;
    myObjectShort = 22;
    myObjectChar = 'A';
    myObjectInt = 232 ;
    myObjectLong = 2121314312321;
    myObjectFloat = 23.54f ;
    myObjectDouble = 12441124124.4343;
    Practice Quiz 25 MCQs Smart Learning

    Master This Topic with Smart Practice

    Reinforce what you just learned by solving high-quality MCQs. Improve accuracy, boost confidence, and prepare like a topper.

    Topic-wise MCQs
    Instant Results
    Improve Accuracy
    Exam Ready Practice
    Login & Start Quiz Create Free Account
    Save progress • Track results • Learn faster