Lesson 4.3: Advanced Programming Logic Flashcards

1
Q

When can a local variable be inferred instead of specified?

A

Beginning with JDK 10، it is now possible to let the compiler infer the type of a local variable based on the type of its initializer، thus avoiding the need to explicitly specify the type. Local variable type inference offers a number of advantages. For example، it can streamline code by eliminating the need to redundantly specify a variable’s type when it can be inferred from its initializer.

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

What does the variable type “var” do?

A

To support local variable type inference، the context-sensitive identifier var was added to Java as a reserved type name. Page 167To use local variable type inference، the variable must be declared with var as the type name and it must include an initializer. Let’s begin with a simple example. Consider the following statement that declares a local double variable called avg that is initialized with the value 10.0: double avg = 10.0; Using type inference، this declaration can also be written like this: var avg = 10.0; In both cases، avg will be of type double. In the first case، its type is explicitly specified. In the second، its type is inferred as double because the initializer 10.0 is of type double.

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

Can you use the var type as a name of a class?

A

Even though it is a context-sensitive identifier، there are a few places in which the use of var is illegal. It cannot be used as the name of a class، for example.

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

When creating a variable array، what would make the brackets illegal?

A

In the first line، an attempt is made to bracket var. In the second، an attempt is made to bracket myArray. In both cases، the use of the brackets is wrong because the type is inferred from the type of the initializer.

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

What type of variables does var declare?

A

Also، remember that var can be used only to declare local variables. It cannot be used when declaring instance variables، parameters، or return types، for example.

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

What are the limits of the var variable?

A

Only one variable can be declared at a time; a variable cannot use null as an initializer; and the variable being declared cannot be used by the initializer expression. Although you can declare an array type using var، you cannot use var with an array initializer. For example، this is valid: var myArray = new int[10]; // This is valid but this is not: var myArray = {1،2،3}; // Wrong It also cannot be used as the name of other reference types، including an interface، enumeration، or annotation. Local variable type inference cannot be used to declare the exception type caught by a catch statement. Also، neither lambda expressions nor method references can be used as initializers.

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

What types of values can bitwise operators work on and why are they called “bitwise operators”?

A

The bitwise operators can be used on values of type long، int، short، char، or byte. Bitwise operations cannot be used on boolean، float، or double، or class types. They are called the bitwise operators because they are used to test، set، or shift the individual bits that make up a value.

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

What are the “AND، OR، XOR، and NOT” operators for bitwise?

A

The bitwise operators AND، OR، XOR، and NOT are &، |، ^، and ~. They perform the same operations as their Boolean logical equivalents described in Chapter 2. The difference is that the bitwise operators work on a bit-by-bit basis. The following table shows the outcome of each operation using 1’s and 0’s:

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

Give me a simple code that turns lowercase letters into capital case using bitwise.

A

The following program demonstrates the & by turning any lowercase letter into uppercase by resetting the 6th bit to 0. As the Unicode/ASCII character set is defined، the lowercase letters are the same as the uppercase ones except that the lowercase ones are greater in value by exactly 32. Therefore، to transform a lowercase letter to uppercase، just turn off the 6th bit، as this program illustrates:

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

How does the bitwise OR work? Give me a coding example that shows OR’s ability to create lower case letters.

A

The bitwise OR، as the reverse of AND، can be used to turn bits on. Any bit that is set to 1 in either operand will cause the corresponding bit in the result to be set to 1. For example:

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

What does the XOR bitwise operator do? Give me a code example that uses XOR to create a simple cipher program.

A

An exclusive OR، usually abbreviated XOR، will result in a set bit if، and only if، the bits being compared are different، as illustrated here: The XOR operator has an interesting property that makes it a simple way to encode a message. When some value X is XORed with another value Y، and then that result is XORed with Y again، X is produced. That is، given the sequence then R2 is the same value as X. Thus، the outcome of a sequence of two XORs can produce the original value. You can use this principle to create a simple cipher program in which some integer is the key that is used to both encode and decode a message by XORing the characters in that message. To encode، the XOR operation is applied the first time، yielding the cipher text. To decode، the XOR Page 175is applied a second time، yielding the plain text. Of course، such a cipher has no practical value، being trivially easy to break. It does، however، provide an interesting way to demonstrate the XOR. Here is a program that uses this approach to encode and decode a short message:

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

What does the unary complementary operator(NOT)(~) do?

A

The unary one’s complement (NOT) operator reverses the state of all the bits of the operand.

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

What are the shift bitwise operators and how do they work? What happens to lost values?

A

In Java it is possible to shift the bits that make up a value to the left or to the right by a specified amount. Java defines the three bit-shift operators shown here: The general forms for these operators are shown here: value &laquo_space;num-bits value&raquo_space; num-bits value&raquo_space;> num-bits Here، value is the value being shifted by the number of bit positions specified by num-bits. Each left shift causes all bits within the specified value to be shifted left one position and a 0 bit to be brought in on the right. Each right shift shifts all bits to the right one position and preserves the sign bit. As you may know، negative numbers are usually represented by setting the high-order bit of an integer value to 1، and this is the approach used by Java. Thus، if the value being shifted is negative، each right shift brings in a 1 on the left. If the value is positive، each right shift brings in a 0 on the left. In addition to the sign bit، there is something else to be aware of when right shifting. Java uses two’s complement to represent negative values. In this approach negative values are stored Page 177by first reversing the bits in the value and then adding 1. Thus، the byte value for –1 in binary is 1111 1111. Right shifting this value will always produce –1! If you don’t want to preserve the sign bit when shifting right، you can use an unsigned right shift (»>)، which always brings in a 0 on the left. For this reason، the&raquo_space;> is also called the zero-fill right shift. You will use the unsigned right shift when shifting bit patterns، such as status codes، that do not represent integers.

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

Why do you need to be careful when shifting byte and short values? (Other than bits being lost when shifted out)

A

You need to be careful when shifting byte and short values because Java will automatically promote these types to int when evaluating an expression. For example، if you right shift a byte value، it will first be promoted to int and then shifted. The result of the shift will also be of type int. Often this conversion is of no consequence. However، if you shift a negative byte or short value، it will be sign-extended when it is promoted to int. Thus، the high-order bits of the resulting integer value will be filled with ones. This is fine when performing a normal right shift. But when you perform a zero-fill right shift، there are 24 ones to be shifted before the byte value begins to see zeros.

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

Since binary is based on powers of two، can the shift operators be used as a shortcut for multiplying or dividing an integer by two?

A

Yes. The bitwise shift operators can be used to perform very fast multiplication or division by two. A shift left doubles a value. A shift right halves it.

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

What is the “?” and what does it do? What is its official name? What is the general format of it?

A

One of Java’s most fascinating operators is the ?. The ? operator is often used to replace if-else statements of this general form: if (condition) var = expression1; else var = expression2; Here، the value assigned to var depends upon the outcome of the condition controlling the if. Page 182The ? is called a ternary operator because it requires three operands. It takes the general form Exp1 ? Exp2 : Exp3; where Exp1 is a boolean expression، and Exp2 and Exp3 are expressions of any type other than void. The type of Exp2 and Exp3 must be the same (or compatible)، though. Notice the use and placement of the colon.

17
Q

Why does the “?” have a third expression?

A

The value of a ? expression is determined like this: Exp1 is evaluated. If it is true، then Exp2 is evaluated and becomes the value of the entire ? expression. If Exp1 is false، then Exp3 is evaluated and its value becomes the value of the expression. Consider this example، which assigns absval the absolute value of val:

18
Q

Give me a code fragment that uses “?” and then give its equivalent in a standard if statement.

A

Here، absval will be assigned the value of val if val is zero or greater. If val is negative، then absval will be assigned the negative of that value (which yields a positive value). The same code written using the if-else structure would look like this:

19
Q

What is a static import?

A

Java supports an expanded use of the import keyword. By following import with the keyword static، an import statement can be used to import the static members of a class or interface. This is called static import. When using static import، it is possible to refer to static members directly by their names، without having to qualify them with the name of their class. This simplifies and shortens the syntax required to use a static member.

20
Q

What are the two forms of static import and what is the benefit to both?

A

As you can see، this form is considerably shorter and easier to read. There are two general forms of the import static statement. The first، which is used by the preceding example، brings into view a single name. Its general form is shown here: import static pkg.type-name.static-member-name; Here، type-name is the name of a class or interface that contains the desired static member. Its full package name is specified by pkg. The name of the member is specified by static-member-name. Page 445The second form of static import imports all static members. Its general form is shown here: import static pkg.type-name.*; If you will be using many static methods or fields defined by a class، then this form lets you bring them into view without having to specify each individually.

21
Q

When should you avoid using static imports and why?

A

Whether importing System.out as just shown is a good idea is subject to debate. Although it does shorten the statement، it is no longer instantly clear to anyone reading the program that the out being referred to is System.out. As convenient as static import can be، it is important not to abuse it. Remember، one reason that Java organizes its libraries into packages is to avoid namespace collisions. When you import static members، you are bringing those members into the current namespace. Thus، you are increasing the potential for namespace conflicts and inadvertent name hiding. If you are using a static member once or twice in the program، it’s best not to import it. Also، some static names، such as System.out، are so recognizable that you might not want to import them. Static import is designed for those situations in which you are using a static member repeatedly، such as when performing a series of mathematical computations. In essence، you should use، but not abuse، this feature.

22
Q

Using static import، can I import the static members of classes that I create?

A

Yes، you can use static import to import the static members of classes and interfaces you create. Doing so is especially convenient when you define several static members that are used frequently throughout a large program. For example، if a class defines a number of static final constants that define various limits، then using static import to bring them into view will save you a lot of tedious typing