Chapter 12 - Type Details Flashcards

1
Q

What are the two basic categories of values in Java?

A

Primitive values and objects

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

How are primitivie values categorized?

A
  • Byte, short, int, long
  • float, double
  • char
  • boolean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the primitive value integer types?

A

byte, short, int, long

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

How do you access a data type’s minimum and maximum values?

A

Use the MIN_VALUE and MAX_VALUE named constants.

Example: To print the maximum int value type:

System.out.println(“Largest int = “ + Integer.MAX_VALUE);

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

What is the default integer constant type?

A

int

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

What does using a 1 or L suffix do?

A

Explicility forces an integer constant to be long.

Example:

long ageOfPlanet = 4_540_000_000; (causes a compilation error)

long ageOfPlanet = 4_540_000_000L; (will compile, notice the L at the end)

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

What are the floating-point types?

A

float, double

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

Why is the double type preferred over the float type?

A

Because it provides more accuracy.

Meaning that for the doubles type the number of significant digits is 15, while only 6 for the float type.

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

What commands access a floating-point data type’s minimum and maximum values?

A

MIN_NORMAL and MAX_VALUE

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

How does the floating point MIN_NORMAL differ from the integer MIN_VALUE?

A

Instead of being the largest-magnitude negative value, it’s the smallest-magnitude positive value.

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

For floating-point numbers, how do you access the largest-magnitude negative value?

A

By using the negation operator (-) with the MAX_VALUE named constant.

Example:

To print the largest-magnitude negative float value type:

System.out.println(

“Largest-magnitude negative float = “ + -Float.MAX_VALUE);

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

What is the default floating-point constant type?

A

double

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

What are you required to do if you declare a variable a float?

A

Append an f or F suffix to all floating-point constants that go into it.

Examples:

float gpa1 = 3.22f;

float gpa2 = 2.75F;

float gpa3 = 4.0; (this will cause a compilation error)

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

What data types are normally used for numbers with large magnitude?

A

long and double

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

What data types should be used if you require greater magnitude or higher precision than long or double offer?

A

BigInteger and BigDecimal where magntiude and precision are only limited by the computer’s memory

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

How do you create BigInteger and BigDecimal objects?

A

Call their constructors with string arguments

Example:

BigInteger num = new BigInteger(“12345678”);

BigDecimal x = new BigDecimal (“12.34567890123456e400”);

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

When using the BigInteger and BigDecimal classes what must you import?

A

The java.util package

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

API Headings and example for the more common BigInteger methods:

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

What method does the BigDecimal class not implement?

A

The mod method

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

What is the encryption and decryption of a message called?

A

Cryptography

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

What is the purpose of Cryptography?

A

To ensure that only the intended recipient(s) can read the message, while others cannot.

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

What is a popular cryptography algoritm that relies on the difficulty of factoring large integers?

A

The RSA algorithm

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

What do character values have in most programming languages?

A

An underlying numeric value

For example the letter “A” has the underlying value of 65

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

What specifies the underlying numeric values of characters?

A

The ASCII table

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

What is having underlying numeric values important?

A

So characters can be ordered. Ordering is necessary so that characters and strings can be sorted.

Example: ‘A’ comes before ‘B’ because A’s 65 is less than B’s 66

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

ASCII Table

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

How can you concatenate a char value to a string?

A

Using the + operator.

Example:

char first = ‘J’;

char last = ‘D’;

System.out.println(“Hello, “ + first + last + ‘!’);

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

What happens with the JVM sees a string next ot a + sign?

A

It converts the operand on the other side of the + sign to a string.

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

What happens if you apply the + operator to two chars?

A

It doesn’t perform concatenation, it instead does mathmatical addition using the characters’ underlying ASCII values.

Example:

char first = ‘J’;

char last = ‘D’;

System.out.println(first + last + “, What’s up?’);

Outputs 142, What’s up?

You must have a string on each side of the chars

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

What is a problem with the ASCII character set?

A

It specifies only 128 characters and there are way more than 128 character in the world. (Think foreign alphabets)

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

How many underlying numeric values does the Unicode Standard define?

A

65,536 characters

32
Q

What composes the first 128 slots of the Unicode character set?

A

The ASCII character set.

33
Q

Why does it matter that the Unicode developers included the ASCII character set as the first 128 slots?

A

It means that programmers can find those characters’ numeric values by referring to a simple ASCII table rather than having to go through the entire Unicode character set.

34
Q

What is needed to print non-ASCII characters?

A

GUI output

35
Q

What is the ordering scheme for primitive type conversions?

A
36
Q

What are the only types taht conversions are allowed for?

A

Numeric types, not boolean.

37
Q

What are the two conversion types?

A

Promotion and Type casting

38
Q

What are the features of a Promotion coversion type?

A
  • It’s an implicit conversion. It’s when an operand’s type is automatically converted without having to use a cast operator.
  • It occurs when there’s an attempt to use a narrower type in a place that expects a wider type; i.e., it occurs when you’re going with the flow of the arrows in the attached diagram.
39
Q

What happens when the expression is a mixed expression?

A

The narrower operand is promoted to match the type of the wider operand.

40
Q

When do promotions typically occur?

A

As part of assignment statements, mixed expressions, and method calls.

41
Q

What type of conversion is Type casting?

A

An explicit type conversion.

That means when an expression’s type is converted by using a cast operation.

42
Q

When is it legal to use a cast operator?

A

To convert any numeric type to any other numeric type.

For example, the conversion can go in either direction in the previous “ordering scheme” diagram.

43
Q

What is the syntax for Type Casting?

A
44
Q

What are the two different modes for the increment operator?

A

Prefix and postfix

45
Q

What is prefix mode?

A

++x means increment x before x’s value is used

Example:

y = ++x; is the same as x = x+1;

y = x;

46
Q

What is postfix mode?

A

x++ means to increment x after x’s value is used

Example:

y = x++; is the same as y = x;

x = x + 1;

47
Q

How are decrement operators (–x or x–) similar to increment operators?

A

They work the same except that subtraction is performed instead of addition.

48
Q

What determines the group of operands to operators when an expression is surrounded by two operators?

A

Operator precedence

Example:

z = y + ++y * y is equal to z = (y + ((++y) * y)))

49
Q

What is evaluation order?

A

The JVM uses operator precedence to identify an expression’s binary operations.

After that it then evaluates the expression’s bianary operations left to right - left first, then right. Then the binary operatin’s operator is executed.

50
Q

What are sometimes embedded inside larger expressions?

A

Assignment expressions

51
Q

What must be remembered when assignment expressions are embedded inside larger expressions?

A
  • An assignment expression evaluates to the assigned value.
  • The assignment operator exhibits right-to-left associativity and right-to-left evaluation order. (This is an exception to the left-to-right evaluation order rule)
52
Q

What is a common practice for assignment expressions for compactness?

A

To embed them inside a loop condition.

53
Q

What expressions implement if else logic using a more compact form?

A

Conditional Operator

Example syntax:

54
Q

Conditional operator sytax and breakdown.

A
55
Q

Why can a conditional operator expression not appear on a line by itself?

A

Because it is not considered a statement. Instead it must be embedded inside of a statement.

Example:

56
Q

How long is a expression containing && or || evaluated?

A

Until a truth value is known.

For the && operator, short-circuiting takes place when the left operand is false.

For the || operator, short-circuiting takes place when the left operand is true.

57
Q

What is short-circuit evaluation?

A

Using the && or || in an expression.

58
Q

What is a statement that does nothing?

A

An empty statement

59
Q

How do you use an empty statement?

A

It consists of a semicolon by itself, you use it in places where the compiler requires a statement, but there is no need to do anything.

60
Q

What can empty statements result in?

A

Cryptic code, you should use them only if there’s a good reason to.

61
Q

What is the purpose of the break statement when used inside a loop?

A

It terminates the immediately enclosing loop and gives control to the next statement after the bottom of the loop.

62
Q

How can using the break statement make your program harder to understand?

A

People reading programs typically only look at the loop heading to determine how a loop terminates. Using a break forces them to look inside the loop for termination conditions.

63
Q

What is a programmer-defined type that is used to restrict a variable to holding one of a fixed set of values defined by the programmer.

A

An enumerated type

64
Q

Enumerated type example

A
65
Q

How can you test enumerated types for equality?

A

Using == and != operators

66
Q

Because computers pay attention to the order in which we list an enumerated type’s value. Values listed earlier are “less than” one listed later. What can we not use directly, and what do we have to use instead?

A

We cannot use, <, >, <=, or >=. Instead we must ahve the enumerated constant call its ordinal method, this returns its position within its enumerated type declaration where 0 is the first position.

Example:

67
Q

What is an enumerated type a class of?

A

Pre-instantiated constant objects

68
Q

What can be in a seperate file with a .java extension, like any other class, and after compiling, it’s compiled form will be in a file with a .class extension?

A

An enumerated type

69
Q

What are the steps in defining an enumerated type as a fixed list of objects?

A

Step 1: Provide a list of names for the objects, with each name followed by a list of values in parentheses.

Step 2: Provide a list of instance constants that correspond to and will store each object’s parenthetical list of values.

Step 3: Provide a private constructor whose parameters correspond to the parenthetical lists of values in step 1, and have that constructor use those values to initialize the intance constants in step 2.

70
Q

What, because it is private, cannot be called from outside the enumerated type definition?

A

A constructor.

71
Q

How can you retrieve all the objects in an enumerated type?

A

By calling the enumerated type’s values method.

72
Q

Example of a method that call returns an array of the enumerated type’s objects.

A
73
Q

What is a binary number, octal number, and hexidecimal number?

A

Numbers that can be understood by the computer.

Base 2 numbers are called binary numbers (2)

Base 8 numbers are called octal numbers (2^3)

Base 16 numbers are called hexadecimal numbers (2^4)

74
Q

What symbols do binary numbers use?

A

0 and 1

75
Q

What symbols does hexidecimal use?

A

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f

76
Q

In Java, what must you do if you want the computer to interpret your presentation of an isolated number as something other than decimal?

A

Prefix the presentation of that number with a special qualifer.

Binary, use a 0b or 0B prefix

Hexadecimal use a 0x or 0X prefix