Basic Java Sytax and Code
Terms
undefined, object
copy deck
- One of the variables of a class template which may have a different value for each object of that class; ________ _________ hold the state of an object.
- instance variables
- Name the 5 arithmetic operators:
-
+ additive operator (also used for String concatenation)
- subtraction operator
* multiplication operator
/ division operator
% remainder operator - Define "short-circuting" behavior in relationship to conditional operators(&& and ||)
- The second operand(expression) is evaluated only if needed.
-
A _________ forms a complete unit of execution. Here are some examples:
aValue = 8933.234; // assignment
aValue++; // increment
System.out.println("Hello World!"); // method invocation
Bicycle myBike - statement
-
What is this code called and what does it do?
switch (expression) {
case cond1: code_block_1;
case cond2: code_block_2;
...
case condn: code_block_n;
default: code_block_default;
} - The switch statement allows for any number of possible execution paths.
- A _____ ________ is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated.
- class variable
- What does a break statement do?
- An unlabeled break statement terminates the innermost switch, for, while, or do-while statement, passing control to the next line of programming following the current loop. A labeled break terminates an outer statement.
- Name the 6 Equality and Relational Operators:
-
== equal to
!= not equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to -
How does this code work?
result = someCondition ? value1 : value2; - If someCondition is true, assign the value of value1 to result. Otherwise, assign the value of value2 to result.
- A _____ is the blueprint from which individual objects are created.
- class
- A _____ is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed.
- block
- The ____ data type is an 8-bit integer number. It has a minimum value of -128 and a maximum value of 127.
- byte
- The while statement continually executes a block of statements while a particular condition is true. How is its syntax expressed?
-
while (expression)
{statement(s)} - Similar to how an object stores its state in fields, a method will often store its temporary state in ______ ________. These are only visible to the methods in which they are declared; they are not accessible from the rest of the class.
- local variables
- The _____ data type is a single-precision 32-bit decimal number.This data type should never be used for precise values, such as currency.
- float
- The ________ statement skips the current iteration of a for, while , or do-while loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop.
- continue
-
What does this code do?
if(value1 == 1)&&(value2 == 2)
System.out.println(value1 + value2); - If value1 is equal to 1 AND value2 is equal to 2 then the program will print out the value of both added together: "3"
- What are the 5 Unary Operators?
-
+ Unary Plus Operator
- Unary Minus Operator
++ Increment Operator
-- Decrement Operator
! Logical Compliment Operator - The first element in an array, accessed by its numerical index, begins with what number?
- 0
- Object-oriented programming allows classes to ______ commonly used state and behavior from other classes.
- inherit
- What code syntax is used for an if-then statement?
-
if(condition)
{
statement;
} - The _____ data type is a 16-bit integer number. It has a minimum value of -32,768 and a maximum value of 32,767.
- short
- What is the difference between a while statement and a do-while statement?
- The do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once. The while statement evaluates its expression at the beginning of each loop.
- The data type of a returned value must match the type of the method's declared return value. When a method is declared ____, use the form of return that doesn't return a value.
- void
- The ______ data type is a double-precision 64-bit decimal number. This data type should never be used for precise values, such as currency.
- double
- An ______ is a container object that holds a number of values of a single type.
- array
- The ______ statement exits from the current method, and control flow returns to where the method was invoked.
- return
-
What does this code do?
if(value1 == 1)||(value2 == 2)
System.out.println("Hello"); - If value1 is equal to 1 OR value2 is equal to 2 then the program will print out "Hello"
- An __________ is a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value.
- expression
-
A Java _________ allows unrelated objects to interact with one another. It defines a set of methods but does not implement them.
Classes that implement this agree to implement all of the methods defined in it, thereby agreeing to certain behav - interface
-
What is the range of this arrays index?
arrayName = new int[10]; - 0 - 9
- What code syntax is used for an if-then-else statement?
-
if(condition)
{statement1;}
else
{statement2;} - The ___ data type is a 32-bit integer number. It is the most common type.
- int
- What is the syntax of a for statement?
-
for (initialization; termination; increment) {
statement(s)
} -
Object-oriented programming allows classes to inherit commonly used state and behavior from other classes.
The syntax for creating a subclass is simple. At the beginning of your class declaration, use the _______ keyword, followed by the name of the - extends
- The _______ data type has only two possible values: true and false.
- boolean
- An ______ is a software bundle of related state and behavior.
- object
- A _______ is a namespace that organizes a set of related classes and interfaces.
- package
-
How many values does this array hold?
double[] anArrayOfDoubles; - an unspecified number
- The ____ data type is a 64-bit integer. It has a larger range than the int type.
- long
- As an example: Bicycle would be a __________ with MountainBike, RoadBike, and TandemBike being three of it's ________.
- superclass, subclasses
- The ____ data type is a single 16-bit Unicode character.
- char
-
What is the name of this kind of array?
String[][] names = {{"Mr. ", "Mrs. ", "Ms. "},{"Smith", "Jones"}}; - multidimensional array
- To return a value, simply put the value (or an expression that calculates the value) after the ______ keyword.
- return