Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables Variables:-

Similar presentations


Presentation on theme: "Variables Variables:-"— Presentation transcript:

1 Variables Variables:-
In computer programming, a variable or scalar is a storage location paired with an associated symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value. The variable name is the usual way to reference the stored value; this separation of name and content allows the name to be used independently of the exact information it represents. The identifier in computer source code can be bound to a value during  run time, and the value of the variable may thus change during the course of program execution. References:

2 Variables Variables:-
A variable is a symbolic name for (or reference to) information. The variable's name represents what information the variable contains. They are called variables because the represented information can change but the operations on the variable remain the same. In general, a program should be written with "Symbolic" notation, such that a statement is always true symbolically. For example if I want to know the average of two grades, We can write "average = (grade_1 + grade_2) / 2.0;" and the variable average will then contain the average grade regardless of the scores stored in the variables, grade_1 and grade_2. References:

3 Variables Variables:- Variables - Symbolic Nature
Variables in a computer program are analogous to "Buckets" or "Envelopes“ where information can be maintained and referenced. On the outside of the bucket is a name. When referring to the bucket, we use the name of the bucket, not the data stored in the bucket. References:

4 Variables Variables:- Variables Actions :-
There are only a few things you can do with a variable: Create one (with a nice name) A variable should be named to represent all possible values that it might contain. Some examples are: last_name, Basic_Pay etc. Put some information into it (destroying whatever was there before). We "put" information into a variable using the assignment operator. For Example: last_name = “Beniwal”; References:

5 Variables Variables:- Variables Actions :-
There are only a few things you can do with a variable: Get a copy of the information out of it (leaving a copy inside) We "get" the information out by simply writing the name of the variable, the computer does the rest for us. For Example: average = (grade_1 + grade_2) / 2. References:

6 Variables Variables:- Variable Properties :-
There are 6 properties associated with a variable. The first three are very important as you start to program. The last three are important as you advance and improve your skills. Memorize These! A Name A Type A Value A Scope A Life Time A Location (in Memory) References:

7 Variables Variables:- Variable Properties :-
Clarification of Properties :- A Name :- The name is Symbolic. It represents the "title" of the information that is being stored with the variable. The name is perhaps the most important property to the programmer, because this is how we "access" the variable. Every variable must have a unique name! References:

8 Variables Variables:- Variable Properties :-
Clarification of Properties A Type The type represents what "kind" of data is stored with the variable. (See the next slides on Data Types). In C, Java, ActionScript, etc, the type of a variable must be explicitly declared when the name is created. In Matlab, the type of the variable is inferred from the data put into the variable. References:

9 Variables Variables:- Variable Properties :-
Clarification of Properties A Value A variable, by its very name, changes over time. Thus if the variable is jims_age and is assigned the value 21. At another point, jims_age may be assigned the value 27. References:

10 Variables Variables:- Variable Properties :-
Clarification of Properties A Value Default Values Most of the time, when you "create a variable" you are primarily defining the variables name and type. Often you will want to provide an initial value to be associated with that variable name. If you forget to assign an initial value, then various rules "kick in" depending on the language. References:

11 Variables Variables:- Variable Properties :-
Clarification of Properties A Scope Good programs are "Chopped" into small self contained sections (called methods) much like a good novel is broken into chapters, and a good chapter is broken into paragraphs, etc. A variable that is seen and used in one methods is NOT available in another section. This allows us to reuse variable names, such as age. In one methods 'age' could refer to the age of a student, and in another function 'age' could refer to the vintage of a fine wine. Further this prevents us from "accidentally" changing information that is important to another part of our program. References:

12 Variables Variables:- Variable Properties :-
Clarification of Properties A Scope We can declare variables within any block. Block is begun with an opening curly brace and ended by a closing curly brace. 1 block equal to 1 new scope in Java thus each time you start a new block, you are creating a new scope. References:

13 Variables Variables:- Variable Properties :-
Clarification of Properties A Life Time The lifetime of a variable refers to how long the variable exists before it is destroyed. Destroying variables refers to de-allocating the memory that was allotted to the variables when declaring it References:

14 Variables Variables:- Variable Properties :-
Clarification of Properties A Life Time The life time of a variable is strongly related to the scope of the variable. When a program begins, variables "come to life" when the program reaches the line of code where they are "declared". Variables "die" when the program leaves the "Scope" of the variable. In our initial programs (which do not have methods), the life time and scope of the variables will be the "entire program“. References:

15 Variables Variables:- Variable Properties :-
Clarification of Properties A Location (in Memory) Luckily, we don't have to worry too much about where in the computer hardware the variable is stored. The computer ( via the compiler) does this for us. But you should be aware that a "Bucket" or "Envelope" exists in the hardware for every variable you declare. In the case of an array, a "bunch of buckets" exist. Every bucket can contain a single value. References:

16 Variables Variables:-
The rules and conventions for naming your variables are followings:- Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning 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 "_". Additionally, the dollar sign character, by convention, is never used at all. References:

17 Variables Variables:-
The rules and conventions for naming your variables are followings:- You may find some situations where auto-generated names will contain the dollar sign, but your variable names should always avoid using it. A similar convention exists for the underscore character; while it's technically legal to begin your variable's name with "_", this practice is discouraged. White space is not permitted. References:

18 Variables Subsequent characters may be letters, digits, dollar signs, or underscore characters. Conventions (and common sense) apply to this rule as well. When choosing a name for your variables, use full words instead of cryptic abbreviations. Doing so will make your code easier to read and understand. In many cases it will also make your code self-documenting; fields named cadence, speed, and gear, for example, are much more intuitive than abbreviated versions, such as s, c, and g. Also keep in mind that the name you choose must not be a keyword or reserved word. References:

19 Variables If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word. The names gearRatio and currentGear are prime examples of this convention. If your variable stores a constant value, such as static final int NUM_GEARS = 6, the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character. By convention, the underscore character is never used elsewhere. References:

20 Variables Java Variable Declaration
Exactly how a variable is declared depends on what type of variable it is (non-static, static, local, parameter). However, there are certain similarities that In Java you declare a variable like this: type name ; Instead of the word type, you write the data type of the variable. Similarly, instead of the word name you write the name you want the variable to have. Here is an example declaring a variable named myVariable of type int. int myVariable; References:

21 Variables Here are examples of how to declare variables of all the primitive data types in Java: byte myByte; short myShort; char myChar; int myInt; long myLong; float myFloat; double myDouble; References:

22 Variables Java Variable Assignment
Assigning a value to a variable in Java follows this pattern: variableName = value ; Here are three concrete examples which assign values to three different variables with different data types myByte = 127; myFloat = ; myString = "This is a text"; You can also assign a value to a variable already when it is declared. Here is how that is done: byte myByte = 127; float myFloat = ; String myString = "string value"; References:

23 Variables “In programming, a variable is a value that can change, depending on conditions or on information passed to the program.” Typically, a program consists of instructions that tell the computer what to do and data that the program uses when it is running. The data consists of constants or fixed values that never change and variable values. Usually, both constants and variables are defined as certain data type s. Each data type prescribes and limits the form of the data. Examples of data types include: an integer expressed as a decimal number, or a string of text characters, usually limited in length. In object-oriented programming , each object contains the data variables of the class it is an instance of. The object's method s are designed to handle the actual values that are supplied to the object when the object is being used. References:

24 Variables The Java programming language defines the following kinds of variables: Instance Variables (Non-Static Fields):-  Technically speaking, objects store their individual states in "non-static fields", that is, fields declared without the static keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); the current Speed of one bicycle is independent from the current Speed of another. References:

25 Variables Class Variables (Static Fields):-
A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change. References:

26 Variables Local Variables:-
 Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example, int count = 0;). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class. References:

27 Variables Parameters:-
 You've already seen examples of parameters, both in the Bicycle class and in the main method of the "Hello World!" application. Recall that the signature for the mainmethod is public static void main(String[] args). Here, the args variable is the parameter to this method. The important thing to remember is that parameters are always classified as "variables" not "fields". This applies to other parameter-accepting constructs as well (such as constructors and exception handlers) that you'll learn about later in the tutorial. References:


Download ppt "Variables Variables:-"

Similar presentations


Ads by Google