Chapter 2 Flashcards
When is type casting required? (Two conditions with numbers)
1.Any time you go from larger numerical data types to smaller types 2. When you convert a floating point number to an integral value.
- Would this run? for( ; ;){ System.out.println(“HelloWorld”); }
This will run without issue. This example reinforces the fact that the pieces of a for loop are each optional. But the semi colons are retired.
What two things can you do a for-each method call on?
a built in java array or an object whose class implements java.lang.Iterable.
What is overflow?
When a number is so large that it no longer fits within the data type. The system in these cases wraps around to the next lowest value and counts up from there.
What’s the structure for using a continue statement?
while(booleanExpression)
{ //body //somewhere in loop continue; }
For example:
public class Main {
public static void main(String… strings) {
int x = 9;
while (x < 12) {
System.out.println(x);
x++;
if (x == 11) {
System.out.println(“continued”);
continue;
}
System.out.println(“didn’t continue”);
}
}
}
Are floating-point literals automatically set to float or double unless postfixed?
Double. Must have an f on the end if you want it to be a float.
Which two primitives can arithmetic operators not be applied to?
boolean and char
What three scenarios could equality operators be used in?
- Comparing two numeric primitive types. 2. Comparing two boolean values. 3. Comparing two objects, including null and String values.
Why won’t this compile? String names = “Lisa”; for (String name : names){ System.out.println(name); }
Because names is not an array. pg. 84
What can logical operators be applied to?
numeric and boolean data types
Are labels required for break and continue statements?
No
Why doesn’t this compile? int x =!5;
Because ! is reserved for boolean values or expressions. If you want 5 to be negative simply put the negation operator (-) in front of 5
- For loop examples to know - Adding Multiple Terms to the for Statement: int x = 0; for (long y=0, z = 4; x <5 && y < 10; x++, y++) { System.out.print(y + “ “); }
This demonstrates a few things: you can declare a variable before the loop begins and use it after it completes. Second, your initialization block, boolean expression, and update statements can have include references that do not talk to each other. Third, the update statement, can modify multiple variables.
What is the output? short x = 10; short y = 3; short z = x/y; System.out.println(z);
This won’t compile. The result of x/y would be an int as data types smaller than int are promoted to int when used with arithmetic operators.
What’s a java statement?
Anything that ends with ;
What are logical operators called when they refer to boolean data types?
Logical operators
What is the logical complement operator?
! inverts the value of a boolean expression
- For loop examples to know - Using incompatible data types in the initialization block: for (long y = 0, int x = 4; x < 5 && y < 10; x++, y++){ // why won’t this compile? System.out.print(x + “ “ ); }
The variables in the initialization block must all be of the same type.
What are the three flavors of operators available to java?
Unary, binary, and ternary. One, two, or three operands
Why won’t this compile? String[] names = new String[3]; for (int name : names0{ System.out.println(name); }
Because name is a string array and the for each loop indicates name is an int. Also, keep in mind that this array is initialized with three null pointer values.
What is the simplest assignment operator?
= as in int x = 1;
What is the ternary operator a condensed form of?
The if, else, then statement
What is a label with respect to loops?
An optional pointer to the head of a statement that allows the application flow to jump to it or break from it. such as OUTER_LOOP: and INNER_LOOP:
What is the structure of the for-each statement?
for(datatype instance : collection){ // body } curly braces optional for single statement blocks but required for multi-statement blocks.
Why doesn’t this compile and how would you fix it? short x = 10; short y = 3; short z = 10*3;
It doesn’t compile because anything smaller than int is promoted to int during arithmetic operations. So you have a type problem. You could fix it with casting: short z = (short)10*3;
What is the structure of a switch statement?
switch(variableToTest){ caseOne constantExpressionOne: code to run for caseOne; optionalBreak; caseTwo constantExpressionTwo: code to run for case2; default: (optional)
What is the operator precedence of the third three operators?
- Relational operators: ,<=,>=, instanceof 8. Equal to/not equal to: ==,!= 9. Logical operators &,^,|
What is the structure of a ternary operator?
booleanExpression ? expressionOne : expressionTwo
for example: int y = x==2 ? 3 : 4;
Which of this code will run if false? if(hourOfDay<11) System.out.println(“Good Morning”); morningGreetingCount++;
Only morningGreetingCount++. When there are no brackets, it only runs the next statement/block of code. Whitespace means nothing to java.
What is a compile time constant value?
These are variables that have been marked with the final keyword and initialized with a literal value in the same expression in which it is declared. Variables passed into methods, even if marked final, are not considered compile time constant. Switch statements require it’s case statement variable to be this. So, like this: final int b =4;
Why does the break matter in a switch statement?
If you don’t give a case a break statement the program will run all the proceeding statements until the switch statement ends or it finds a break;
What is a nested loop?
A loop containing another loop
What are the arithmetic operators?(7)
+,-,*,/,% and include the unary operators: ++,–
What is the data type of x * y? double x = 39.21 float y = 2.1
Trick! This will not compile. 2.1 must have an f, and if you had that then both of these would be promoted to double.
Why doesn’t this compile? int animal = (y<91) ? 9 : “Horse”;
If y is greater than or equal to 91, the return type of the expression would be a string. And, as animal is an int, we have a type problem.
What is this doing? int x = (int)1.0;
Converting the double 1.0 to an int
What is an operand?
The thing an operator is evaluating.
What’s the structure for using a break statement?
optionalLabel: while(booleanExpression){ //body //somwhere in loop break optionalLabel; }
When is && true? When is it false?
only true when both operands (expressions being evaluated) are true. Returns false if either operand is false
What do break statements do?
Terminate switch statements or other statements to return control back to the program.
!=
Compares two operands and returns a boolean value whether or not the two objects are not equal.
- For loop examples to know - Using Variables Outside the Loop: for ( long y = 0, x = 4; x < 5 &&; y < 10; x++, y++){ System.out.print( y + “ “ ); } System.out.println(x); // why doesn’t this compile
(Are the variables defined in an initiation block of a for loop available outside the for loop?)
Scope variable problem. x (and all other variables defined in the initialization block of a for loop) are only in scope for the duration of the loop. After the loop is over the variable is donezo.
What is the structure of a do-while statement?
do { //body }while (booleanExpression); curly braces required for block of multiple statements. Optional for one line bodies. So this works:
String hello = “hello”;
do System.out.println(hello);
while(1>0);
What is the operator precedence of the second three operators?
- Multiplication/Division/Modulus: *,/,% 5. Addition/subtraction 6. shift operators: <>,>>>