Java Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is a Method?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a simple way of describing the definition of ‘class’?

A

A class is a blueprint of an object. It describes the behavior/state of that object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How would you define ‘object’?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an object instance?

A

An instance is an object that has a place in memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does ‘instantiating a class’ mean?

Why is it called this?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Private vs. Public

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does ‘static’ mean?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you create a new object instance?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you apply a method to an object?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you get user input in Java?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can you input a variable value such as an integer or string into a string print statement?

i.e. “The variable is ___”

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is method overloading?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly