Introduction to Java Flashcards
In the java programming language:
A program is made up of ________ classes
one or more
In the java programming language:
A program is made up of ________ , methods
one or more
A method contains
program statements
Anatomy of a Java Program:
Comments Reserved words Modifiers Statements Blocks Classes Methods The main method
How do you structure a class?
//comments about the class public class classHeader {
//class body
}
How do you structure a method?
//comments about the class public class classHeader {
public static returnType name(|inputType| tempName) { //method body }
}
Comments in a program are called…
They should be included to…
inline documentation, explain the purpose of the program and describe processing steps
How are comments structured?
// this comment runs the end of line
/* this comment runs to the terminating symbol */
/** comment */
Identifiers are…
the words that a programmer use in a program
Identifiers can be made up of…
letters, digits, underscore and $
Identifiers cannot begin with…
a digit
Java is case…
sensitive
White space includes…
spaces, blank lines, tabs
White spaces are used to…
separate words and symbols in a program and make the program more readable along with indentation
Extra white spaces are
ignored
Reserved words called modifiers are used to
specify properties of the data, methods and classes and how they can be used
private, final, public and static are examples of
modifiers
A statement…
represents an action or a sequence of actions
A statement ends in
;
Blocks can be identified with…
{
}
The syntax rules of a language define…
how we can put together symbols, reserved words, and identifiers to make a valid program
The semantics of a program is…
what the programmer intends the program to achieve/do
A program that is syntactically correct does not mean it is
semantically/logically correct
The three types of errors in java are
- Compile time errors
- Run-time errors
- Logical errors
Compile time errors occur when…
The program is fundamentally flawed (e.g. syntactically incorrect), hence an executable version of the program can not be compuled
Run time errors occur when…
A problem arises halfway through the execution of the program, e.g. divide by 0
Logical errors occur when…
There’s no compile time or runtime errors (i.e. the program runs perfectly) but does not produce the intended results
A class is…
a template or blueprint for constructing objects, it is a program entity that represents either 1. a program/module or 2. a type of object
A method is…
a collection of statements that performs a sequence of actions to return a message (or void)
The main method…
provides the control of program flow
The java interpreter executes the application by invoking the..
main method
The main method looks like…
public static void main(String[] args) { //Statements; }
An algorithm is…
a set of precise instructions that lead to a solution
An algorithm does not..
have to be written in programming language, it can be written in pseudocode (a mix of human and programming languages)
Pseudocode must be…
precise and clear enough so that a programmer can convert it into syntactically correct code easily
Pseudocode is much less…
rigid than programming language
The steps to solving a problem are…
- Understand the problem
- Design a solution
- Consider alternatives and refine the solution
- Implement
- Test
The steps to solving a problem are…
not linear and overlap
The key to designing a solution is…
breaking it down into manageable pieces with classes and objects (i.e. modular programming)
What type of programming language is Java?
OOP Object-Oriented Programming Language
The Scanner class (type) is a template for…
creating many Scanner objects
objects are
an entity that combines data (variables OF the object) and behaviour (methods OF the object)
Object-oriented programming is
program that performs their behaviour as interactions between objects
How is an object constructed?
Type objectName = new Type (parameters);
Calling an object’s method:
objectName.methodName (parameters);
A variable holds either:
- Primitive types
2. Reference to objects
To define an OBJECT REFERENCE variable,
a class name can be used a the type
e.g. String name;
The declaration String title;
does not create a new object, the object is constructed separately or constructed when it is assigned
An object reference variable holds the…
address of an object
title = new String (“Java”);
is…
an example of
- a constructor method
- instantiation (an instance of a particular class is created which is an object)
Once an object is created, the dot operator is used to…
e.g. count = title.length();
invoke a method
The act of assignment…
e.g. num1 = num2
takes a COPY of a value/address and stores it in a variable, nothing is destroyed
Aliases are…
two or more object reference variables that refer to the same object, they are the alias of each other
An object is a garbage when…
It is no longer being referenced, therefore becomes useless
Java performs garbage collection…
automatically and periodically
To create a new String object, the constructor method is_______ because ________.
not required because it is so common
String literals each represent…
a String object
Literal strings are defined by…
enclosing in double quotes “String”