Fallowing icon

Friday, September 21, 2012

Java Variables


I had explained the last lesson, what are the data types in java. Now I explain,how to declare the variables in java. 

The variables are act very important character in all the computer languages. Why I told that, we use variables in java programs much more time. 



The basic Method to declare Variables-



   The default variable module is -  DataType VariableName;


     According to the above module you can create a variable using below process.  
     Erase the DataType word in this model and put in to the data type to  that place. 
     Erase the VariableName word in this model and put in to the variable name for that place.


ex 1:- String firstName;    
    String is a data type and firstName is a variable name.

ex 2:- int age;
int is a data type and age is a variable name. 

ex 3:- char luckyLetter;
    char is a data type and luckyLetter is a variable name.




This is the basic way to declare the variables. But in this module, we haven't initialized the variables. 



How to declare and initialize variables?  



      Modal - DataType VariableName=Value;

   
We can declare the variables using this modal. We already initialize the variables values in this module.     
      Erase the DataType word in this model and put in to the data type to  that place. 
      Erase the VariableName word in this model and put in to the variable name for that place.
      Erase the Value word in this model and put in to the value for that place. 
   
ex 1:- byte yrLuckyNum=3;
    byte is a data type, yrLuskyNum is a variable name and 3 is a value in this example. 

ex 2:- boolean yrIndex=true;

    boolean is a data type, yrIndex is a variable name and true is a value in this example.

ex 3:- String firstName="Thomas";

    String is a data type, firstName is a variable name and Thomas is a value in this example. 



How to declare several variables of the same types on the same line. 



Normally we use several variables in same types on a java codes.
We declare those variables in one by one.It wast our time. 
If you are going to declare several variables on one line, you must careful for use variables in same data type. 




     Modal 1 -   DataType VariableName1,VariableName2,VariableName3......;

Modal 2 -  DataType VariableName1=Value1,VariableName2=Value2.....;



You can see we use comma between variable names in those modules.
But you can't use different data types in this method.

    
ex 1: int index,age,marks;
    We declare the index,age,marks variables on one line. Those are Integer type variables.

ex 2: String firstName,lastName,midName;
    We declare the String type tree variables on one line in this example. 

ex 3: float priceOne=14.02f,priceTow=43.12f,priceTree=23.44f;
    We declare and initialize tree variables in float data type.

  

How to create the variable names using standard- 

               
       Some peoples are using meaningless words. Some times they use Capital letters for first letter in variable name.  it's not a good practice. We want to fallow the standards for declare variable names.
However we cannot declare two or more variable names to useing one name in a method. When we will try to compile this java code,  definitely It's give error massage.

  
eg- {int firstName; char firstName;} you can't declare variables like that.


But normally we want to create many variables in one program and we want to identify variables in variable name one by one. In that case, we required method for create the variable names.        
  
       First you think, Why I want to this variable names, for what? 
       After you think, this variable name is want to identify uniquely and can it use easily in java programming(don't use very large words for create variable names) 
       Finally, when we create variable name is using single word, use all the letters are in simple. When we create variable name is using two or more words,  use first word letters in simple and up to this word first letters are in capital and others are in simple. 




Example for variable names- 


ex 1:- When you want to declare variable name for store name of the peoples, you can use name for variable name.

ex 2:- When you want to store first name,last name and mid name, you can use firstName,lastName,midName for variable name. 


ex 3:- When you want to stare birthday, you can use bDay for it.




The example of add all steps -




//Create class call Test.

public class Test { 

    public static void main(String args[]){           //Call the main Method 

        String firstName="Smith ",lastName="Johnson";  //declare the variabls 
        int age=35,hight; 
        float weight;
        double salary=1000.00;
        
        System.out.println("My name is    :"+firstName+lastName);    //print two variables in one print command
        System.out.println("My age is     :"+age);       
        hight=150;                                     //Initialize the value
        weight=53.00f;
        System.out.println("My hight and weight are      :"+hight+" / "+weight);
        System.out.println("My monthly sallary is        :"+salary);      
    }    
}





* Download this java code, then compile it and run.         


No comments:

:a   :b   :c   :d   :e   :f   :g   :h   :i   :j   :k   :l   :m   :n   :o   :p   :q   :r   :s   :t

Post a Comment