When your program carries out computations, you will want to store values so that
you can use them later. In a Java program, you use variables to store values.
Variable Declarations
A variable is a storage location in a computer program.
Each variable has a name and holds a value.
A variable is similar to a parking space in a parking garage. The parking space has an identifier (such as “J 053”), and it can hold a vehicle. A variable has a name (such as cansPerPack), and it can hold a value (such as 6).
Ex: int cansPerPack = 6;
Syntax:
typeName variableName = value; (or)
typeName variableName;
When you declare a variable, you specify the data type which tells the types of values that the variable holds.
Also, you may want to initialize the value with a value.
Variable Names
When you declare a variable, pick a name that explains its purpose. So, better to use a descriptive name.
you can use them later. In a Java program, you use variables to store values.
Variable Declarations
A variable is a storage location in a computer program.
Each variable has a name and holds a value.
A variable is similar to a parking space in a parking garage. The parking space has an identifier (such as “J 053”), and it can hold a vehicle. A variable has a name (such as cansPerPack), and it can hold a value (such as 6).
Ex: int cansPerPack = 6;
Syntax:
typeName variableName = value; (or)
typeName variableName;
When you declare a variable, you specify the data type which tells the types of values that the variable holds.
Also, you may want to initialize the value with a value.
Variable Names
When you declare a variable, pick a name that explains its purpose. So, better to use a descriptive name.
- Variable names must start with a letter or underscore character(_). (Technically, the $ symbol is allowed as well, but you should not use it—it is intended for names that are automatically generated by tools.)
- Spaces are not permitted inside names.
- You can use upper case letters to denote word boundaries, as in cansPerPack. This notation is called 'camelCaseNotation'
- Variable names are case sensitive.
- You should not use reserved words for variable names.
Convention:
It is a convention among java programmers that variable names should start with a lower case letter and class names should start with an upper case letter.
Number types
int type - denote a whole number without a fractional part.
Ex: int cansPerPack = 6;
When a fractional part is required (such as in the number 0.335), we use floating-point numbers. The most commonly used in java is double.
Ex: double pricePerCan = 1.5;
Assignment Statement
You use the assignment statement to place a new value into a variable.
Ex: cansPerPack = 8;
The left-hand side of an assignment statement consists of a variable. The right-hand side is an expression that has a value.
- An assignment statement stores a new value in a variable, replacing the previously stored value.
- The assignment operator(=) does not denote mathematical equality.
Constants
You cannot change the value of a variable that is defined as final.
Constants are commonly written using capital letters to distinguish them visually from regular variable.
Ex: final double PI = 3.14;
Syntax: final typeName variableName = expression;
Comments
As your program gets more complex, you should add comments. Comments add explanations for humans who read your code.
Compiler ignores comments.
Single line comments - //
Multi-line comments - /* ........ */
Java doc comments : /** ........... */
- This commenting style is used by tools that analyze source files, like javadoc tool which generates documentation of java source files based on this notation.
Some tips:
- Do not use magic numbers in code. A magic number is a numeric constant/literal that appears in code without explanation.
- Floating point numbers are usually rounded. Ex: 1/3 calculated as 0.33 and you multiply 0.33 * 3, it is 0.99 not 1.
- Please check for overflow or underflow errors with numeric types.
- Don't use undeclared and uninitialized variables in your program. Compiler will throw errors for these.
Numeric types in Java
Java has several numeric types:
- byte - single byte consisting of 8 bits : -128 to 127, including 0
- short - two bytes : -32,768 ... 32,767
- int - four bytes
- long - 8 bytes
- float - 4 bytes
- double - 8 bytes.
Computers use base 2.
double data type is the default for floating point numbers.
Ex: double val = 0.235d;
float data type literal value must be suffixed with 'f'
Ex: float val = 0.235f;
long literal value must be suffixed with 'l'
Ex: long val = 125l;
Note: There are no unsigned integers in Java. All are signed and Big-Endian.
No comments:
Post a Comment