Variables

A variable is a named space in the computer memory (RAM) that can store data.

In Java we can create a variable like below :

int number = 5 ;

In Java its different from some other languages like Python where you don’t need to give the data type.  Here “int” is the data type where it refers to “Integer”, “number” is the name of the variable and “5” in the value stored in that memory location.

There are some rules in naming a variable. Let’s take a look at them now.

  • It cannot be started by a number.
    • number1  –  (correct form)
    • 1number  –  (incorrect form)
  • spaces are not allowed – you can use underscore for that purpose
    • number_1 – (correct form)
    • number 1 – (incorrect form)
  • To start a variable name, a letter, $ sign or an underscore (_) can be used.
  • Variable names are case sensitive.
    • “number” and “Number” are two different variable names.

These rules are applied in most of the programming languages but not all of them.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s