Data Types & Arithmetic Flashcards

1
Q

Primitive data types

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the primitive data types in Java?

A

In ascending order of size:

  1. Boolean (1 bit) - this type returns true or false
  2. Byte - a number made up of 8 bits. Essentially all manufactured computers use a byte as the smallest unit of storage in memory.
  3. Char (2 bytes) - characters that use the Unicode encoding scheme
  4. Short (2 bytes) - integers, a number that cannot have a fractional part, in the range of -32,768 to +32,767
  5. Int (4 bytes) - integers in the range of -2,147,483,658 to +2,147,483,657
  6. Long (8 bytes) - integers in the range of -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
  7. Float (4 bytes) - a single precision data type. It can store a floating-point, or fractional, number with 7 digits of accuracy.
  8. Double (8 bytes) - a double precision data type. It can store a floating-point, or fractional, number with 15 digits of accuracy.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why are data types important?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Unary operators

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Binary operators

A

Operators that work with two operands, such as:

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division
  • % Modulus (returns the remainder of a division)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Ternary operator

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Combined assignment operators / Compound operators

A

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;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Strongly typed language

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Widening conversion

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Narrowing conversion

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Incrementer

A

An operator, ++, that increments a variable by adding 1.

For example:
counter++; // adds 1 to the variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Decrementer

A

An operator, –, that subtracts 1 from the variable.

For example:
counter–; // subtracts 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Boolean

A

A data type with two possible values: true or false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Cast operator

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Integer Division

A

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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Modulus

A

The % operator that computes the remainder of integer division.

17
Q

Operator associativity

A

The rule that governs in which order operators of the same precedence are executed. For example, in Java the - operator is left-associative because a - b - c is interpreted as (a - b) - c, and = is right-associated because a = b = c is interpreted as a = (b = c).

18
Q

Operator precedence

A

The rule that governs which operator is evaluated first. For example, in Java the && operator has higher precendence than the || operator. Hence a || b && c is interpreted as a || (b && c).