Java Flashcards
What is a Method?
Methods define the operations that code of other classes can perform to an object instance (or just object).
In other words, a Java method is a collection of statements that are grouped together to perform an operation.
What is a simple way of describing the definition of ‘class’?
A class is a blueprint of an object. It describes the behavior/state of that object.
How would you define ‘object’?
In the real world, an object has a state and behavior. Software objects work in a similarly. The state of an object is stored in fields (aka variables) and the methods (functions) display the object’s behavior.
As stated before, objects are created through blueprints/templates called classes.
What is an object instance?
An instance is an object that has a place in memory.
What does ‘instantiating a class’ mean?
Why is it called this?
Means ‘creating an object’.
The phrase comes from the fact that when you create an object, you are creating an ‘instance of a class’, therefore ‘instantiating’ a class.
Private vs. Public
Private and Public are access modifiers applied to classes, fields(variables), methods and constructors(special method with the same name as the class).
The access modifier defines what other classes can access in a given class. Private refers to the fact that only the code inside the same class can access the variable or method. Vice-versa, public allows for universal access.
What does ‘static’ mean?
When ‘static’ is used it refers to a variable that can be accessed without instantiation of the class.
In other words, you can call a variable/method, even if an object has never been created for this class.
The benefit of this is that a virtual machine can call a main method without creating an object, avoiding memory being taken up and possible clashes/errors occurring in the program.
How do you create a new object instance?
You call the class, then press space and type the name you want for the instance, then equate that to ‘new’ followed by the class name with empty brackets at the end and a semicolon.
e.g. MeanAndVariance mv = new MeanAndVariance();f
How do you apply a method to an object?
You write out the object name, followed by a dot and then the method you want to apply with empty brackets at the end.
How do you get user input in Java?
First you need to import scanner through:
import java.util.Scanner;
Then you need to make a variable that is going to hold the input from the keyboard/user:
Scanner ‘Variable_Name’ = new Scanner(System.in);
So you state that you are going to use Scanner, write the variable name and assign that to the use input with new Scanner(System.in)
After that, you can print the use input using:
System.out.println(bucky.nextLine());
The nextLine is necessary as it tells the program to wait for user input from System.in before printing. That’s just how Java works for now.
How can you input a variable value such as an integer or string into a string print statement?
i.e. “The variable is ___”
you use a % sign followed by the first letter of the type of variable you are inputting. These are called format specifiers.
There are quite a few format specifiers, but to mention a few, %d is for integers, % is for strings, %f is for floats and apparently %b is for any type.
What is method overloading?
A feature allowing a class to have methods with the same name as long as the arguments lists are different.
You can differ argument lists by having different number of parameters, different data types or the sequence of data types.
Having different return types does not allow for method overloading.