Chapter 1 & 2 Flashcards
(59 cards)
Define Methods?
Groups of related statements in a class that perform a specific task. They are used to do specific tasks on their own objects and other objects. They are comparable to functions and subroutines in other languages. A well-designed method performs only 1 task.
How are attributes in a class defined? Discuss.
They are defined by variables. Each object in a class has instance variables (aka object variables) that differ between objects.
Each class attribute has a single corresponding variable.
Class variables are the variables that have the same value for every object in the class.
How do objects communicate with each other?
Methods. A class or object can call methods in another class or object for many reasons, including to report changes to other objects, to change something about the other object, or to ask the other object to do something.
Define expression.
A statement that returns a value.
Return value?
The value produced by a statement
What punctuation groups statements in Java? What is a group of statements grouped by this punctuation called?
Braces, {}. A block, or “block statement”.
Name and define the 3 types of variables in Java.
Instance, class, and local. Local variables are used inside method definitions or even smaller blocks of statements within a method. They cease to exist after the method or block is done being executed.
Variable names in Java must start with ________.
a letter, an underscore _, or a dollar sign $
What are the types of data that can be used for variables?
Primitive types (like Int, Boolean, floats, and characters), the name of a class or interface, or an array
Types, ranges, and defs of integers, NOT ints
Byte–8 bits, -128 to 127 (halves of 2^8)
Short–16 bits, -32, 768 to 32,767 (halves of 2^16)
Int–32 bits, (halves of 2^32)
Long–64 bits, halves of 2^64
What is a double re:floats?
A float with more decimal places
Difference between Boolean and boolean?
Class versus a variable type
Can a variable have a class as its type? How is that denoted?
Yes, for instance
Color hair;
String lastName= “Hopper”;
Notice the caps in the variable type
What is a constant? What are they useful for?
A variable whose value never changes. Useful for defining shared values for the use of all methods of an object.
What’s the diff between instance variables and methods versus class variables and methods?
Class variables occupy memory until a Java program is finished running; if a class variable references am object, that object will ALSO remain in memory. This often causes programs to run too slowly.
Packages?
A way to group related classes and interfaces. They allow groups of classes to be easily referenced in other classes, as well as eliminating potential naming conflicts among classes.
By default, Java classes can refer to the ones in the java.lang package using only short names. To use classes from another package, you must use the full package name, or use the “import” command to import the package in your source code file.
Because the Color class is contained in the java.awt package, you would call this java.awt.Color.
If the entire package has been imported, you can refer to the class simply as Color.
Interface?
Helps avoid problems with single inheritance. Allows a class to have behaviors that its superclass doesn’t.
Do the methods included in am interface define the additional behavior that it allows a subclass to have?
No
How do you assign values to variables?
=
How do you declare a constant?
Use the “final” keyword before the variable declaration, and include an initial value, like
final int YAR = 15
Yar is all caps as a naming convention
How do you present more than 1 variable or literal as the argument to println()?
The + combines them into one string
3 types of comments–describe
Single-line–preceded by two slash characters. //. Everything after to slashes to the end of the line is considered a comment.
Multi-line–/* text. */
Javadoc–/** text. **/. Certain utilities like javadoc create a set of web page records that document the functionality of a Java class.
How do you indicate that a literal should be a “long” integer?
Adding the letter L to the number, e.g.,
yarTotal=yarTotal + 4L
Adds the number 4 in the long format to the current yarTotal format.
How do you denote binary, octal, and hex as literals?
Octal–add a zero to the beginning of the number
Hex–add a zero and an x to the beginning (0x)
Binary–add 0b to the beginning