Variables are the nouns of a programming language-that is, they are the entities (values and data) that act or are acted upon.
A variable declaration always contains two components:For example, "int count;" has type integer and name "count".
- the type of the variable and
- its name.
All variables in the Java language must have a data type. A variable's data type determines the values that the variable can contain and the operations that can be performed on it. For example,
values it can contain operations which can be performed on it Integer integral values
(both positive and negative)standard arithmetic (+, -, *, / and %) Integers can contain only integral values (both positive and negative), and you can use the standard arithmetic operators (+, -, *, / and %) on integers to perform the standard arithmetic operations (addition, subtraction, multiplication, and division, respectively).
Operator / and %: The operator / is the division operator, and % is the remainder operator. For example,
int i =17 / 3; // The result is 5
int i = 17 % 3; // The result is 2, which is the remainder after the division.
There are two major categories of data types in the Java language: primitive and reference.Which primitive types should you know?
The following table lists, by keyword, some of the primitive data types in Java which you should know, their sizes and formats, and a brief description of each.
Type Size/Format Description (integers) int 32-bit two's complement Integer (real numbers) double 64-bit IEEE 754 Double-precision floating point (other types) char 16-bit Unicode character A single character boolean true or false` A boolean value (true or false) Numbers
Numbers come in various favours, but the most important classification is: does it have a fraction?CharactersThere are times when this advice will be wrong, but at this point in your career, you shouldn't be worrying about those times.
- For fractional numbers, use the type double --
double radius = 50.4;
double length = 4.0;
Rahter than writing386000000
people normally write this kind of number using scientific notation, such as,3.86 X 10^8
(i.e., 3.86 times 10 to the power of 8). Since superscripts are traditionally not easy to print, the power of 10 is represented by the letter E followed by the value of the exponent:3.86E8
To distinguish double from int literals, double literals with no decimal point or exponent (i.e., those that look like int literals) must have a trailing d, as, for example,double total = 98d;
- for non-fractional, use int --
int i = 50;
Booleans
- Characters are represented by the type char.
- Each char represent a single character. For example:
char c = 'h';
- Strings are made up of characters: the value of
"hi".charAt(0)
is 'h'.- characters can be treated as a sort of number. You can write
'h'+2
but you don't get 'j'; instead, 'h'+2 is some number you are really not interested in. To get 'j', write(char) ('h'+2) // a cast
- Remember two rules:
- 'a' ... 'z', 'A' ... 'Z', '0' ... '9' are each in order and contiguous.
- ' ' (the blank) is a character and comes "before" all other printable characters.
- Booleans represent "truth values" and can only have the values true and false.
- Most Often, you use booleans without explicitly declaring a variable of the boolean type. For example, the expression
number < 0
is actually boolean, and has the value true if the variable "number" contains a value less than 0, and false otherwise.As we will see, we would seldom declare a boolean variable to contain the result; instead, we would use it inside a statement somehow.
- One thing you will find yourself needing is combinations of booleans in expressions like these:
operator example expression expression evaluates to ... Logic AND && (number > 0) && (number < 100) true, if the first condition and the second condition are both satisfied;
false, otherwise.logic OR || (number < 0) || (sum < 100) true, if either the first condition or the second condition (or both) are satisfied;
false, otherwise.equality == object1 == object2 true, if object1 is the same as object2;
false, otherwise.inequality != object1 != object2 true, if object1 is not the same as object2;
false, otherwise.
Purity Tip: In other languages, the format and size of primitive data types may depend on the platform on which a program is running. In contrast, the Java language specifies the size and format of its primitive data types. Hence, you don't have to worry about system-dependencies.
Sometimes, we would like to work with different data types. Different data type has different formats/sizes. For instance, char, int and double are 16-bits, 32-bits and 64 bits, respectively. The convertion from one data type to the other has to follow ceratin rules.Promotion Rule
The promotion rules specify how types can be converted to other types without losing data. For example, an int is automatically converted to a double without changing its value. However, a double converted to an int trucates the fractional part of the double value.CastingThe promotion rules apply to expressions containing values of two or more data types; such expressions are also referred to as mixed-type expressions. The type of each value in a mixed-type expression is promoted to the "highest" type in the expression (actually a temporary version of each value is created and used for the expression -- the original values remain unchanged). The table below lists the primitive data types (boolean, char, int, double) and the types to withch each is allowed to be promoted automatically.
Type Allowed promotions double None (there are no primitive types larger than double) int double char int, double boolean None (boolean values are not considered to be numbers in Java) If you havedouble d = 3.14;The compiler will complain that "this is an illegal assignment". Becuase a double number has 64-bits whereas an integer (int) has only 32 bits, i.e., a double stores more information than an int.
int i = d; // this is WRONG!!Converting values to lower types can result in incorrect values. Therefore, in cases where information may be lost due to conversion, the Java compiler requires the programmer to use a cast operator to force the conversion to occur.
For example, assigning the double
double d = 3.1415926;to an int type variableint i;we would lose accuracy. So we have to do the conversion explicitly by telling Java that we want to convert the double to an int --i = (int) d; // now i is 3Java will make a copy of d, truncate the fractional part of number, and assign the result to i.
Here are some more examples of casting lower types (less bits in its format/size) to
The table below shows the precedence and of the operators. The operators are shown top-to-bottom in decreasing order of precedence.
Operators Type () parenthese ++ -- + - (type) unary * / % multiplicative + - additive < <= > >= relational == != equality = += -= *= /= %= assignment
There are two major categories of data types in the Java language: primitive and reference.Primitive Data Types
Reference Data TypesA variable of primitive type contains a single value of the appropriate size and format for its type: a number, character, or boolean value.
For example, the value of an int is an integer, the value of a char is a 16-bit Unicode character, and so on.Arrays, classes, and interfaces are reference types. The value of a reference type variable, in contrast to that of a primitive type, is a reference to the actual value or set of values represented by the variable. A reference is like your friend's address: The address is not your friend, but it's a way to reach your friend. A reference type variable is not the array or object itself but rather a way to reach it. ![]()
This is like -- knowing the address of Joe's house, we can locate Joe's house.
The addressis a reference to Joe's house, not the actual object.
Example type The variable stores... Primitive Data Type int, double, char, boolean the actual value Reference Data Type class, array, interfaces a reference (like an address) The countChars method uses one variable of reference type, in, which is a Reader object. When used in a statement or expression, the name in evaluates to a reference to the object. So you can use the object's name to access its member variables or call its methods (just as countChars does to call read).