Ticker

6/recent/ticker-posts

Declaring a Variable in Java



Declaring a Variable Java provides us containers or placeholders to gore data within a program. These containers are called variables since the value stored in them can be changed during the course of execution of a program. To define a variable in a lava program, you need to use the combination of a data type and an identifier.An identifier should be a logical name representing a variable, constant, or method.

Following code snippet shows how to declare a variable: data type identifier:// declaring variable In the preceding syntax,

·      data Type: It specifies a valid Java's data type, such as int,char and so on.

       identifier: It can be any name that follows some specified naming 

       conventions.  Naming conventions  are discussed later in this topic., After

       declaring the   variable, let's now see how to   initialize it.


·     identifier = val;   // to declaring a variable

The 'val' in the preceding syntax represents the value that is used to initialize the variable and it should be according to the data type used within the variable. It is not only that you first have to declare a variable and then initialize it. You can also initialize the variable at the time of declaration.As we know, the name given to a variable is known as an identifier, which should reflect the contents contained inside the variable. An identifier must take care of the following rules specified in Java:

·          An identifier must start with an underscore, a letter, or a dollar sign ( $ ).

·          An identifier may consist of a letter, a number, an underscore, and dollar sign.

·          Spaces are not allowed in between an identifier.

·          Identifiers are case sensitive that means the words salary, Salary, and SALARY are      taken as three different identifiers. Java keywords should not be used while naming        an identifier since they hold some special meaning in Java.

    Keyword:  Java Keyword that have special meaning in java

   Abstract, default, implements, protected, throw, assert, do, imports, 

   public, throws, Boolean, double, instance of, return, transient, break, 

   else, int, short, try, byte, extends, interface, static, void, case, final, long, 

   strictfp, volatile, catch, finally, native, super, while, char, float, new, 

    switch, synchronized,continue,if,private,this,enum.

Understanding DataType:

Data types are very important in Java as you cannot even declare a variable without specifying its data type.

In Java, the data types can be divided into three broad categories:

*  Integer: Represents integers using byte, short, int, and long.

* Character Represents char, which is used to More characters, such as letters and numbers.

* Floating point Represents fractional numbers using f lost and double data types.

* Boolean: Represents logical values using Boolean data type.

 

Loop Statement: Loop Statement is a statement that allow us to execute one or multiple statement in multiple times and flow a general form of loop statement in most of the programming language


 For more understanding you can also read below mentioned topic from online:

variable in javascript


Post a Comment

0 Comments