Week 2 Flashcards
(117 cards)
Array
a contiguous block of memory storing a group of sequentially stored elements of the same type.
What is int []
int array
int array declaration
int []
int [] count;
What is this set to if not initialized?
null.
When an array is declared, you can resize as often as you’d like. T/F?
False. Once an array is declared it cannot be resized.
How do you declare a double array?
double []
what is the difference between length and length()
length is for an array.
length() is for string objects.
What is a Package?
A method to encapsulate a group of classes or sub packages, and interfaces.
Why do we use packages?
prevent naming conflicts.
location and usage of classes, interfaces, enums, and annotations easier.
Packages can be considered as data encapsulation (or data-hiding)
explain package college.staff.cse
There are 3 directories total in this package.
cse is inside staff which is inside college.
how are packages written?
In the reverse way you would specify a domain name.
How can I pull in other classes?
Import statement after our package declaration.
What is the Scanner class used for?
to read user input from the command line.
What is an import statement?
An import statement improves the readability of the program by taking a class or all classes visible for a program under a package, using just a single statement.
Explain the following:
import package1[.package2].(*);
The top level package is package 1.
The subordiante-level package is package 2, under package 1.
the (*) is to import all classes from package 1 into package 2.
Explain the following:
import java.util.ArrayList;
This is an import statement that takes an Array list from the package util which is the subpackage of the package java.
Where is the Scanner class found?
At the java.util package.
What class is used to get user input? And where can you find the package to import it?
Scanner class.
java.util
What does the following code do?
java.util.Scanner
Imports the scanner class.
What is the following code? Explain each part.
public static void main(String[] args) {
}
The main method.
public = access modifier.
static means the method can be accessed straight from the class, we don’t have to instantiate an object to have a reference and use it.
void = The data type. this method doesn’t return a value.
main = the name of the method. This is the identifier java looks for when executing a Java program.
Explain the following code, define each part:
Scanner myObj = new Scanner(System.in); System.out.println("Enter username");
This creates a scanner object so the user can input their username.
Scanner is the class.
myObj is the object name.
new is telling java to create a new object.
Scanner is instantiating the object as a Scanner.
System.in is allowing the user to input from device.
Explain the following code, define each part:
String userName = myObj.nextLine();
System.out.println(“Username is: “ + userName);
Read user input.
String class is creating a string object called userName which is being set to equal the myObj user input. Then it is printing out the userName.
The three steps when creating an object from a class.
Declaration (creating the name, the variable part.).
Instantiation (‘new’ keyword which is used to create an object.)
Initialization (following ‘new’ keyword is a call to a constructor. This call initializes the new object)
What is a java constructor?
a special method that is used to initialize objects.
