Variables and data types
A variable is an identifier that does not store location. It use to store data values. Unlike constant that remain unchange during the execution of program, but a variable may take different value or can change value different times during the execution of program.
A variable name can be chosen by programmer in meaningful way so it can reflect what it represent in the program. Some example are as follows:
- average
- height
- total_height
- classStrength
Variable name may consist of alphabets, digits, the underscore ( _ ) and dollar ( $ ) characters. It has some conditions those are as follows:
- They must not begin/start with digit.
- Uppercase and lower case are distinct. That means TOTAL is not same as total.
- It should not be a keyword.
- White space is not allowed.
- Variable name can be any length.
Data Types
Every variable in java has data type. Data type specifies the size and type of value that can be stored. There are many data types are present in java so programer can chosen the data type according to his need or requirement.
Integer Types:
Integer type can hold whole number such as 123, -96, and 5689. The size of the values that can be stored depends on integer data type we choose. Java support 4 types of interger. They are byte, short, int and long.
It should be remember that wider data types required more time for manipulation and there for it is advisable to use smaller data type. For example, instead of storing a number 50 in an int typs variable, we must use byte variable to handle this number. This will improve the speed of execution of program.
Floating point:
Integer type can hold only whole number and therefore we use another type known as floating point type to hold number containing fractional parts such as 12.50 and -1.324.
The float type values are single-precision numbers while the double type represent double-precision numbers. Floating point numbes are treated as double-precision quantities. To force them into single-precision mode, we must append f or F to the number. For example,
1.235f
7.524568F
Double-precision type are use when we need grater precision in storage like all mathematical function such as sin, cos and sqrt returns double type value.
Chatacter type:
In order to store character constants in memory, Java provides a charactor data type call char. The size of char is 2 bytes which can hold one single character.
Boolean type:
Boolean type is use when we want to test a aprticular condition during execution of program. There are only two values true and false. Boolean is denoted by the keyword boolean.
Comments
Post a Comment