top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Java Variables

0 votes
256 views

Variables  

A variable is a location in the computer’s memory where a value is stored and from which the value can be retrieved later.

Variables are used in a java program to store data that changes during the execution of the program. They are the basic units of storage in a java program. Variables can be declared to store values such as names, addresses and salary details.

Variables must be declared before they can be used:

datatype variablename;

where,

datatype is a valid data type in java.

Variablename is a valid variable name.

example

int rollNumber;

char gender;

The above statements declare an integer variable called rollNumber, and a character variable called gender. These statements instruct the operating system to allocate the required amount of memory to each variable in order to hold the type of data specified for each. Additionally, each statement also provides a name that can be used within the program to access the values stored in each of the variables.

Rules and Conventions

Java programming language has its own set of rules and conventions that need to be followed for naming variables. For a start, variable names should be short and meaningful.

Not adhering to the rules will result in syntax errors. Naming conventions are to be followed as they ensure good programming practices.

The rules and conventions for naming variables are:

  • Variable names may consist of Unicode letters and digits, underscore(_), and dollar sign ($).

  • A variable’s name must begin with a letter, the dollar sign ($), or the underscore character (_). The convention, however, is to always begin your variable names with a letter, not “$” or “_”.

  • Variable names must not be a keyword or reserved word in java.

  • Variable names in java are case-sensitive (for example, the variable names number and number refer to two different variables).

  • If a variable name comprises a single word, the name should be in lowercase(for example, velocity or ratio).

  • If  the variable name consists of more than one word, the first letter of each subsequent word should be capitalized (for example, employeeNumber and accountBalance).

Note: Unicode is a 16-bit character set, which contains all the characters commonly used in information processing. It is an attempt to consolidate the alphabets of the world’s various languages into a single and international character set.

The table shown below contains some example of valid and invalid java variable names:

Variable name

Valid / Invalid

rollNumber

Valid

a2x5_w7t3

Valid

$yearly_salary

Valid

_2010_tax

Valid

$$_

Valid

amount#Balance

Invalid and contains the illegal character #

double

Invalid and is a keyword

4short

Invalid and first character is a digit

posted Jun 20, 2017 by Sapna Kumari

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

Classification

Reference data types store the memory reference of other variables. These other variable hold the actual values. Reference type can be classified as:

Object: Object is a built-in reference data type. It is a base class for all predefined and user-defined data types. A class is a logical structure that represents a real world entity. This means that the predefined and user-defined data types are created on the object class.

String: String is a built-in reference type. String type signifies Unicode character string values. Once string are created, they cannot be modified.

Class: A class is user-defined structure that contains variables and methods. For example, the Employee class can be a user-defined structure that can contain variables such as empsalary, empname, and empAddress. In addition, it can contain methods such as CalculateSalary() , which returns the net salary of an employee.

Delegate: A delegate is a user-defined reference type that stores the reference of one or more methods.

Interface: An interface is a type of user-defined class that is used for multiple inheritance.

Array: An array is user-defined data structure that contains values of the same data type, such as marks of students.

Variable Naming Rules

A variable needs to be declared before it can be referenced. You need to follow certain rules while declaring a variable:

  • A variable name can begin with an upper case or a lower case letter. The name can contain letters, digits and the underscore character(_).
  • The first character of the variable name must be a letter and not a digit. The underscore is also a legal first character, but it is not recommended at the beginning of a name.
  • C# is a case-sensitive language; hence variable names count and count refer to two different variables.
  • C# keywords cannot be used as variable names. If you still need to use a c# keyword prefix it with the ‘@’ symbol.

Note: Microsoft recommends camelcase notation for c# variable names. You should not use underscores and must ensure that the first letter of the identifier is in lowercase. In addition, you must capitalize the first letter of each subsequent word of the identifier. For example, consider the following variable declarations:

Int totMonths = 12;

String empName = “John Ferb”;

Bool statusInfo = true;

 

Create and use variables

A variable’s type and identifier (name ) need to be mentioned when you declare a variable. This tells the compiler the name of the variable and the variable and the type of data the variable will store. If you attempt to use an undeclared variable, the compiler will generate and error message. The table given below displays a list of valid and invalid variable names in c#.

Variable Name

Valid/Invalid

Employee

Valid

student

Valid

_Name

Valid

Emp_Name

Valid

@goto

Valid

static

Invalid as it is a keyword

4myclass

Invalid as a variable cannot start with a digit

Student&Faculty

Invalid as a variable cannot have the special character &

Note: When you declare a variable, the computer allocates memory for it. Hence, to avoid wasting computer memory, it is recommended to declare variables only when required.

READ MORE

Variable and Data Types in C#

A variable is an entity whose value can keep changing. For example, the age of a student, the age of a student, the address of a faculty member and the salary of an employee are all examples of variables.

  

In C#, a variable is a location in the computer’s memory that is identified by a unique name and is used to store a value. The name of the variable is used to access and read the value stored in it. Different types of data such as a character, an integer or a string can be stored in variables. Based on the type of data that needs to be stored in variable, variables can be assigned different data types.

Using variables

In C# , memory is allocated to a variable at the time of its creation. During creation, a variable is given a name that uniquely identifies the variable within its scope. For example, you can create a variable called empName to store the name of an employee.

You can initialize the variables at the time of creating the variable or at a later time. Once initialized, the value of a variable can be changed as required.

In c#, variable enable you to keep track of data. When you are referring to a variable, you are actually referring to the value stored in that variable.

The following syntax is used to declare variable in c#,

<datatype> <variableName>;

Where,

Datatype: Is a valid data type in C#.

VariableName: Is a valid variable name.

Data Types

You can store different types of values such as numbers, characters or strings in different variables. But the compiler must know what kind of data a particular variable is expected to store. To identify the type of data that can be stored in a variable, c# provides different data types.

When a variable is declared, a data type is assigned to the variable. This allows the variable to store values of the assigned data types. In C# programming language, data types are divided into two categories. These are:

 

Value Types: variable of value types store actual values. These values are stored in a stack. The values can be either of a built-in data type or a user-defined data type. Most of the built-in data type are value types. The value type built-in data types are int, float, double, char and bool. Stack storage results in faster memory allocation to variables of value types.

Reference Type: Variables of reference type store the memory address of other variables in a heap. These values can either belong to a built-in data type or a user-defined data type. For example, string is a built-in data type which is a reference type. Most of the user-defined data types such as class are reference types.

Predefined Data Types

The predefined data types are referred to as basic data types in c#. These data types have a predefined range and size. The size of the data type helps the compiler to allocate memory space and the range helps the compiler to ensure that the value assigned is within the range of the variable’s data type.

The predefined data types in c# is given below:-

Data Type

Size

Range

byte

Unsigned 8-bit integer

0 to 255

short

Signed 16-bit integer

-32, 768 to 32,767

int

Signed 32-bit integer

-2,147,483,648 to 2,147,483,647

long

Signed 64-bit integer

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

float

32-bit floating point with 7 digits precisions

±1.5e-45 to ±3.4e38

double

64-bit floating point with 15-16 digits precision

±5.oe-324 to ± 1.7e308

decimal

128-bit floating point with 28-29 digits precision

±1.0  x 10e-28 to ±7.9 x 10e28

char

Unicode 16-bit character

U+0000 to U+ffff

bool

Stores either true or false

True or flase

 

READ MORE
...