Lesson 4.3: Advanced Programming Logic Flashcards
When can a local variable be inferred instead of specified?
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.
What does the variable type “var” do?
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.
Can you use the var type as a name of a class?
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.
When creating a variable array، what would make the brackets illegal?
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.
What type of variables does var declare?
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.
What are the limits of the var variable?
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.
What types of values can bitwise operators work on and why are they called “bitwise operators”?
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.
What are the “AND، OR، XOR، and NOT” operators for bitwise?
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:
Give me a simple code that turns lowercase letters into capital case using bitwise.
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 does the bitwise OR work? Give me a coding example that shows OR’s ability to create lower case letters.
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:
What does the XOR bitwise operator do? Give me a code example that uses XOR to create a simple cipher program.
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:
What does the unary complementary operator(NOT)(~) do?
The unary one’s complement (NOT) operator reverses the state of all the bits of the operand.
What are the shift bitwise operators and how do they work? What happens to lost values?
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 «_space;num-bits value»_space; num-bits value»_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»_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.
Why do you need to be careful when shifting byte and short values? (Other than bits being lost when shifted out)
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.
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?
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.