Q&A

Q1:

The if statement seems like the one that's most useful. Is it possible to use only if statements in programs and never use the others?

A1:

It's possible to do without else or switch, and many programmers never use the ternary operator ?. However, else and switch often are beneficial to use in your programs because they make them easier to understand. A set of if statements chained together can become unwieldy.

Q2:

An if statement is described as either a single statement or as a conditional statement followed by another statement to handle if the condition is true. Which is it?

A2:

The point that might be confusing is that if statements and other conditionals are used in conjunction with other statements. The if statement makes a decision, and the other statements do work based on the decision that is made. The if statement combines a conditional statement with one or more other types of Java statements, such as statements that use the println() method or create a variable.

Q3:

In the ClockTalk program, why is 1 added to Calendar.MONTH to get the current month value?

A3:

This is necessary because of a quirk in the way that the Calendar class represents months. Instead of numbering them from 1 to 12 as you might expect, Calendar numbers months beginning with 0 in January and ending with 11 in December. Adding 1 causes months to be represented numerically in a more understandable manner.

Q4:

During this hour, opening and closing brackets { and } are not used with an if statement if it is used in conjunction with only one statement. Isn't it mandatory to use brackets?

A4:

No. Brackets can be used as part of any if statement to surround the part of the program that's dependent on the conditional test. Using brackets is a good practice to get into because it prevents a common error that might take place when you revise the program. If you add a second statement after an if conditional and don't add brackets, unexpected errors will occur when the program is run.

Q5:

Will the JDK's Java compiler catch the error when an = operator is used with a conditional instead of a ==?

A5:

Often it will not, and it results in a real doozy of a logic error. These errors only show up when a program is being run and can be discovered only through observation and testing. Because the = operator is used to assign a value to a variable, if you use name = "Fernando" in a spot in a program where you mean to use name == "Fernando", you could wipe out the value of the name variable and replace it with Fernando. When the value stored in a variable changes unexpectedly, it can cause subtle and unexpected errors.

Q6:

Does break have to be used in each section of statements that follow a case?

A6:

You don't have to use break. If you do not use it at the end of a group of statements, all of the remaining statements inside the switch block statement will be handled, regardless of the case value they are being tested with.

Q7:

Why do you sometimes use single-quotation marks after a case statement and sometimes leave them off?

A7:

The value associated with a case statement must be either a character or an integer. The single-quotation marks surround a char value and are not used with an int value, so they differentiate between these two data types—for example, case '1': looks for the '1' character and case 1: looks for the integer value of 1.

Q8:

Why do they call them "buffalo wings" when they are made out of chicken?

A8:

The spicy chicken dish was invented by a bar owner in Buffalo, N.Y. Teressa Bellissimo served the wings in 1964, as a late-night snack for her son and his friends, including blue cheese dressing and celery because they were on hand. The family-owned Anchor Bar continues to serve 1,000 pounds of wings a day.

      
Comments