Java Basics Flashcards

1
Q

The way Java works

A
  1. Source
  2. Compiler
  3. Output (Bytecode)
  4. Virtual Machines (JVM)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What goes in source file?

A

Class definition

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

What goes in a class?

A

Methods

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

What goes in a method?

A

A set of statements, and for now you can think of a method kind of like a function or procedure

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

Why does everything have to be in class?

A

Java is object-oriented, thus each object represents a set of blueprint

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

Do I have to put a main in every class I write?

A

Nope. You might use a dozens of classes, but only one main method to evoke the program

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

What is the difference between a class and an object?

A

A class is blueprint for an object.

A object is like one entry in your address book

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

What if I need global variables and method? How do I do that if everything has to go in a class?

A

There isn’t a concept of ‘global’ variables and methods in a Java OO program.

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

What is a Java program? What do you actually deliver?

A

A Java program is a pile of classes. In a Java application, one of the classes must have a main method, used to start-up the program

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

How is this object oriented if you can still make global functions and global data?

A

Everything in Java goes in a class. So the constant for pi and the method for random(), although both public and static, are defined within the Math class

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

Object Oriented Programming

A
  1. Flexibility

2. Extensibility

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

Instance Variable

A

The object’s state (the data), and can have unique values for each object of the type

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

Class

A

A blueprint that tells the virtual machine how to make an object of that particular type

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

Main

A
  1. To test your real class

2. To launch/start your Java application

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

Garbage-Collectible Heap

A
  1. When the object is created, Java allocates memory space on the heap according to how much that particular object needs
  2. When the object never in use anymore, then that object becomes eligible for garbage collection
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Primitive type

A

Hold fundamental values including integers, booleans, and floating point numbers

17
Q

Object reference

A
  1. Hold bits that represent a way to access an object.

2. Hold an address of the object

18
Q

Does all object references are the same size, regardless of the size of the actual objects to which they refer?

A

Yes. All references for a give JVM will be the same size regardless of the objects they reference, but teach JVM might have a different way of representing references on one JVM maybe smaller or larger than references on another JVM

19
Q

Can I do arithmetic on a reference variable, increment it, you know - C stuff?

A

No. Java is different from C

20
Q

Array

A

Arrays are always objects, whether they’re declared to hold primitives or object references

21
Q

How do Java pass its parameters?

A

Java is pass-by-value, meaning everything is a copy

22
Q

Can a method declare multiple return values? Or is there some way to return more than one value?

A

Sort of, A method can return an array

23
Q

Do I have to return the exact type I declared?

A

You can return anything that can be implicitly promoted to that type. So, you can pass a byte where an int is expected. The caller won’t care, because the byte fits just fine into the int the caller will use for assigning the result.
You must use an explicit cast when the declared type is smaller than what you’re trying to return

24
Q

Getter

A

A getter’s sole purpose in life is to send back, as a return value, the value of whatever it is that particular Getter is supposed to be Getting

25
Q

Instance variables

A
  1. Always get a default value

2. If no assignment for instance variable, then it still has a value

26
Q

Local variable

A

Do not get a default value. The compiler complains if you try to use a local variable before the variable is initialized

27
Q

Comparing Variable

A
  1. Use == to compare two primitives

2. Use the equals() method to see if two different objects are equal