Chapter 2 Flashcards
(112 cards)
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.