Declaring and initializing Variables

declaring and instantiating variables

Declaring and initializing variables in Java

As we explained Java is strictly typed language. Declaring and initializing  variables  in Java is nothing but creating a  name with that type and assigning a value to that variable ,and this type refers to primitive types or data types. Java has eight primitive types of data. But before going into the deep , first we will discuss how a variable looks inside a Java program.

Declaring variables in Java

Syntax :

type  Variable-name;  

Note : Every statement in Java should end with ” ; “

class VariableExamples
{
 int x;  // declaring an integer type variable
long L; // declaring a long integer type variable
String name; // Declaring a string type variable
float y;  // declaring a float type variable
double z;  // declaring a double type variable
boolean b; // declaring a boolean type variable

/* all the above variables are called  instance variables which is also called
    global variables in C-Lang
*/

    void method variableMethod()
     {
         int x; // declaring an integer type variable
         String name; // declaring a string type variable
         float f; // declaring a float type variable
       /* all the above variables inside the method are called local
          variables */

     }

}

In the above sample program , we declared two flavors of variables one is “instance variables” and the other is “local variables“. Instance variables are the variables which are declared inside the class and outside the method and these variables can be accessed by any method in that class ,in C-Lang we call it as “global variables“. Local variables are the variables which are declared right inside the method , these variables can only be accessed by that method only,no other method can access those variables. And local variables must be initialized before using them.

declaring and initializing variables

Initializing  variables in Java

After declaring a variable , we have to assign some value to that variable. Initializing variables in Java should be done according to that type mentioned. And remember we can declare and assign a value to a variable in one single step , but the beginner should know how actually a variable will be in  java program before assigning a value.  Initializing a variable is nothing but assigning some value to that variable.

class VariableExamples
{
   int x;
   float f;
   String name;
   double d;
   boolean b;
   long l;
   // now assign some values to that variables
     x=10;
     f= 11.4f; // always recommended to mention f after the value to a float type
     d= 11.444d; // always recommended to mention d after the value to a double
     name="javafrombasics"; // string value should always be inside double quotes
     b=true; // Boolean type has only two values true or false
     l=186000;
     void variableMethod()
     {

         int x;
         String name;
         double d;
         float f;
         boolean b;
         long l;
       // now assigning values to the above variables 
         x= 20;
         d=20.1245d;
         b= false;
         f= 12.2f;
         l = 10001;
         name= "java";
     }

}

Initializing the  variables are done by the above sample program , but there are some consequences between instance variables and local variables while running the program. This can be well explained by a real sample program which will be discussed in the next later. Right now we will look into how to declare and assign in one single step in a Java program.

Instance variables are the also called as object state variables. Whenever you create an object for the class VariableExamples , all the instance variables and their values will be automatically stored in the object you created. This can also be done manually by creating constructors , you will learn about constructors and object creation in the next chapters.

Declaring and initializing a variable in one single step.

class VariableExamples
{
  int x=10;
  String name= java;
  double d= 12.3600d;
  float f=12.5f;
  boolean b= true;
 
   void variableMethod()
   {
      int x=25;
      double d=12.365d;
      String name="javafrombasics";
      boolean = true;
   }
 }

As you observed we have declared and assigned some values to the variables in one single step. This is the recommended step in the real time java programming as it saves some memory as well as time ,which is the main factor in the real time project. The reason we discussed only declaring a variable is , in the coming chapter you will go through the object declaration which is quite interesting topic in Java. In an  object declaration you cannot assign a value to the object directly as we assigned to an int x, we can only declare an object and assigning is done using methods and constructors which we will discuss later in the coming chapters.

In the next chapter you will go through the detailed explanation about the primitive types of Java and their range.

 

 

4 thoughts on “Declaring and initializing Variables

  1. You actually make it appear so easy with your presentation but I in finding this matter to be really one thing that I think I might by no means understand. It kind of feels too complex and extremely vast for me. I’m having a look ahead on your subsequent put up, I will attempt to get the hold of it!

Leave a Reply

Your email address will not be published. Required fields are marked *