What can be stored in a variable Why is using a meaningful name for the variable important?
Variables are the nouns of a programming language: they are the entities (values, data) that act or are acted upon. The character-counting program uses two variables--count and args. The program increments count each time it reads a character from the input source and ignores args. The declarations for both variables appear in bold in the following listing: class Count { public static void main(String[] args) throws java.io.IOException { int count = 0; while (System.in.read() != -1) count++; System.out.println("Input has " + count + " chars."); } }A variable declaration always contains two components: the type of the variable and its name. Also, the location of the variable declaration, that is, where the declaration appears in relation to other code elements, determines the scope of the variable. Variable TypesAll variables in the Java language must have a data type. A variable's type determines the values that the variable can have and the operations that can be performed on it. For example, the declaration int count declares that count is an integer (int). Integers can have only whole number values (both positive and negative) and you can use the standard arithmetic operators (+, -, and so on) on integers to perform the standard arithmetic operations (addition, subtraction, and so on). There are two major categories of data types in the Java language: primitive types and reference types. Variable NamesA program refers to a variable's value by its name. For example, when the character-counting program wants to refer to the value of the count variable, it simply uses the name count. By convention, variable names begin with a lower case letter (class names begin with a capital letter). ScopeA variable's scope is the block of code within which the variable is accessible. Also, a variable's scope determines when the variable is created and destroyed. You establish the scope of a variable when you declare it. Scope places a variable into one of these four categories: Variable InitializationLocal variables and member variables can be initialized when they are declared. The character-counting program provides an initial value for count when declaring it:int count = 0;The value assigned to the variable must match the variable's type. Why is using a meaningful name for the variable important?They allow logic to occur, enabling developers to measure time, analyze data, and customize the program to the user. Variables are so important to the code that they deserve a good name that accurately describes their purpose.
What is a meaningful variable name?Use meaningful names: A meaningful or expressive variable name will be eaiser for someone to understand. For example: precip is a more useful name that tells us something about the object compared to x or a . Do not start names with numbers Objects that start with a number are NOT VALID in Python.
What is a variable What should be kept in mind while naming a variable?Rules for Naming Variables
The first character must be a letter or an underscore (_). You can't use a number as the first character. The rest of the variable name can include any letter, any number, or the underscore. You can't use any other characters, including spaces, symbols, and punctuation marks.
|