Data Types & Arithmetic Flashcards
Primitive data types
Data types that cannot be used to create objects, but only variables that hold a single value. There are 8 primitive data types in Java and these variables do not have attributes or methods.
What are the primitive data types in Java?
In ascending order of size:
- Boolean (1 bit) - this type returns true or false
- Byte - a number made up of 8 bits. Essentially all manufactured computers use a byte as the smallest unit of storage in memory.
- Char (2 bytes) - characters that use the Unicode encoding scheme
- Short (2 bytes) - integers, a number that cannot have a fractional part, in the range of -32,768 to +32,767
- Int (4 bytes) - integers in the range of -2,147,483,658 to +2,147,483,657
- Long (8 bytes) - integers in the range of -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
- Float (4 bytes) - a single precision data type. It can store a floating-point, or fractional, number with 7 digits of accuracy.
- Double (8 bytes) - a double precision data type. It can store a floating-point, or fractional, number with 15 digits of accuracy.
Why are data types important?
Variables must be classified according to the their data type, which determines the kind of information that may be stored in them. It’s important to select the correct data type for variables since this determines their memory, format, and storage.
Unary operators
Operators that only require one operand. For example, ( - ) gives the negative value of a number in Java. It can be expressed as: - 5 - number (with a variable)
Binary operators
Operators that work with two operands, such as:
- + Addition
- - Subtraction
- * Multiplication
- / Division
- % Modulus (returns the remainder of a division)
Ternary operator
There is only one operator that works with three operands in Java, the conditional operator. It is a shorthand method of expressing an if-else statement in the following format: BooleanExpression ? Value1: Value2;
Combined assignment operators / Compound operators
Shortcuts for common operations:
- *+=** where x+=5; is x = x + 5;
- *-=** where y -= 2; is y = y - 2;
- **=** where z *= 10; is z = z * 10;
- */=** where a /= b; is a = a / b;
- *%=** where c %= 3; is c = c % 3;
Strongly typed language
Java is a strongly typed language which means that before a value is assigned to a variable, Java checks the data type of the variable and the value being assigned to it to determine whether they are compatible.
Java will perform some conversions between data types automatically, but only if they will not result in the loss of any data.
Widening conversion
In assignment statements where lower-ranked data types are stored in variables of higher-ranked data types, Java automatically converts the lower-ranked value to the higher-ranked type, which is known as a widening conversion.
For example:
double x;
int y = 100;
x = y; // performs a widening conversion
Narrowing conversion
The conversion of a value to a lower-ranked data type. For example, converting a double to an integer would be a narrowing conversion. Given that this would result in data loss, Java does not perform them automatically.
Incrementer
An operator, ++, that increments a variable by adding 1.
For example:
counter++; // adds 1 to the variable
Decrementer
An operator, –, that subtracts 1 from the variable.
For example:
counter–; // subtracts 1
Boolean
A data type with two possible values: true or false.
Cast operator
Lets you manually convert a value from one type to a different type, even if a narrowing conversion will take place. Cast operators are unary operators that appear as a data type name enclosed in a set of parentheses.
For example:
number = (int)72.567;
value = (double)bankAccount;
* Note that when a cast operator is applied to a variable, it does not change the contents of the variable. It only returns the converted value.
Integer Division
Taking the quotient of two integers and discarding the remainder. For example, if the following two numbers are integers in Java, then 11/4 is 2, not 2.75. This is a very common mistake to watch out for!