What is a Variable? How to Declare and Assign Values to Variables?

Variable:-

Variable  is  a named memory  locations, to store, some value. The  name  of  variable  can  be   chosen  by the  programmer   in a  meaningful way.
The   variable  name  may  consists  of   alphabets, digits, underscore &  dollar  sign with the  following  conditions.

Rules:-

  • The variable name must not  begin  with  a  digit.
  • It  should n’t   be  a keyboard.
  • Variable  name  shouldn’t  allow  blank spaces & white space
  • Upper can letter  &  lower can  letters one distinct
  • Variable name  can be of  any length.

Declaration  of variables:-

In java  variables  are  names  of  strong  locations.After  designing  suitable  variable  names   are must  be  declared  to  the  compiler. Declaration  tells be  declared  to  the  compiler. Declaration tells  three things:

Tell the compiler  what is the variable name.

It  specify what  type of  data  variable can hold.

The place  of  declaration  decides  the   scope of  variable  in the program.

Variable must be defined  before  it  is   used  in  the  program. The   variable  can  be  used  to  store the  value of  any data  type.  The  declaration  statement  define  the  type  of  variable.

The   genaral  from of  declaring the  variable is:

Syntax:  data type  var1,  var2,-----,varn;

 Eg:- int a,b,c ;

Variables  are   separated  by  coma’s   the  declaration statement must be  end   with  a  semicolon (;)

Assigning   values   to variables :-

The  variable  must be  given  a  value  after it has  been declared  but before  it  is  used  in  an  expression. It  will be  done in two  ways:

By  using  an assignment  statement.

By  using  the  Read  statement.

Assignment statement:-

Simple  method  of  giving  values  to  variables  is  through  assignment  statement  as follows.

Syntax:  Variablename = value ;

Ex: Count=0 ;
       Hight= 6.5;

We can  also  assign an  expression  as  follows:

a=b=c=0;

it  is  also possible assign  value to variable at the  time of  its  declaration as  follows.

Syntax :   datatype  variablename= value;

Ex: int  count =0;
       float  height=6.5;

The  process  of  giving  initial values  to the  variables  is  known  as  initialization.

Read  statement :-   

We may also give values  to  variables  intercultural  through  keyword using  the redLine ( ) method.

The   read  line ( ) method  (which  is invoked   using  an  object of  the  class   data  input stream) reads  the  input  from  the keyboard   as  a string. Which  are  then converted to  the  corresponds  datatype. Using wrapper classes.

Note  that we have used  the keywords  (try, catch) to  handle any errors  that might  occur   during  the  reading process.
      

Related

Java 6415052979424147351

Post a Comment

emo-but-icon

item