Java Flashcards

1
Q

The Java Development Kit contains what?

A

The Java Interpreter, The Java Compiler, and also the Java Virtual Machine

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

What is the Java Virtual Machine (JVM)

A

We are installing an operating system as a virtual machine that is dedicated to interpreting Java bytecode

Java bytecode is generated using the java compiler (javac) in the Java Development Kit (JDK). The JDK includes the Java Runtime Environment (JRE) which contains our JVM and allows us to run our Java programs.

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

What are .class and .jar files?

A

We need the JDK and we will compile java code into .class or .jar files. After compiling, we can run the bytecode with tools that are included in the JDK

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

What are some features about Java that are appealing?

A

1) Strong and Static Typing
2) Compiled Language
3) Object Oriented
4) Widely Used
5) Ecosystem
6) Java’s API

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

What is dynamic vs static?

A

These refer to when we assign types to variables: Languages with static tying assign a type to declared variables at compile time and set aside the right amount of memory that for that variable, while those with dynamic typing assign types only when the variable declaration code runs during execution and change the memory allocated for the variable as needed - this comes at the cost of program speed while it reorganizes things.

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

What are some dynamic and statically typed languages?

A

Java, C, Swift are statically typed languages. Javascript, Python, and Ruby are dynamically typed

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

What does it mean to be a compiled language?

A

It goes through analysis and optimization before it is run. The compiler can find repetitive code that you failed to optimize and do this for us, resulting in a faster program that we might otherwise have.

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

What does javac do in the terminal?

A

It compiles our code into bytecode and holds it in a file with a .class extension. Then when we call java and the file then it goes through.

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

What must be exactly the same as your class?

A

File name must be exactly the same as your class name

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

What is the main method?

A

Defines the entry point for an executable Java file. It is required for any file that is intended to be run from the command line.

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

Every application begins with a class definition

A

A class can contain multiple methods (which should be enclosed between the class opening and closing curly brace tag).

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

Explain public static void main

A

The entry point for an executable Java file. Three modifiers for the main method:

public: This is known as an access modifier. Any public method we write is accessible from any other class or method in our project
static: means that method belongs to and is called from the class itself rather than from an instance of the class.
void: the main() method does not return anything.

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

Why do we declare our types with variables?

A

This is to help with speed and safety. Speed, because all variables of the same type require the same amount of memory, and by declaring the type at initialization, Java is able to allocated only the proper amount. It helps with safethy, in that if we try to assign a vvalue of the wrong type to a variable, we get an error

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

What is the difference in syntax for primitive types and object types?

A

primitive types are lowercase, while object types are capitalized.

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

Why is it important to know the range of values that our data types can hold?

A
For type casting
1 - byte
2 - short, char
4 - int, float
8 - long, double
NA - boolean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is explicit cassting

A

Explicit casting is when we declare the type that we want to convert to in our source code.

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

What is implicit casting?

A

Implicit casting is the opposite, casting from a smaller data type to a larger

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

Explain importing

A

Java programs are heavily dependent on importing code that already has been created. It is uncommon to have Java programs that are completely standalone without any imports. Although reusing given classes and methods is common, you should always be checking the documentation for best practices and deprecation.

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

Explain Strings

A

Strings represent a sequence of characters. Strings, like any other object, belong to a class. INstances of the String class are immutable, so once you create a String, it cannot be modified

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

Methods are…

A

just functions that are attached to an object. Each method will have a code body and a method declaration, which includes the access level, return type, name, and parameter variables

RETURN MUST ALWAYS BE A CLASS, PRIMITIVE, OR VOID

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

What is overloading?

A

We can call two methods the same name. They could have different parameters

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

Describes arrays in Java?

A

Arrays in Java are a fixed-size sequential collection of elements of the same tyoe with a zero-based index. To use an array, e first need to declare it, initiliizar and add elements or values to it.

NOTE: In Java an array has a fixed size (after initialization), meangint that you canmot add or remove items from an array. In the example above, we declared that this arrray would contain five items. You cannot decrease or increase the number of elements in this array after initialization.

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

What are generics?

A

It tells our ArrayList of the acceptable types it can hold. If we did not include this (always include it!), then we could add anything to our ArrayList - strings, integers, and all manner of things. Generics helps to keep our compiler

24
Q

What is a good way to think of exception handling?

A

If you were to ask a friend if they could deliver a message for you, and they said, “Sure, but my car might run out of gas - if it does, I’ll call you and tell you’. In that scenario, your friend delivering the message is the risky operation, the car running out of gas is the exceptiional situation, and their calling you is the throwing of the exception

25
Q

What is an Object?

A

A collection of behaviors and properties that all revolve around the same concept; this collection of behaviors and properties are called instance members.

Objects were originally used to model real-world objects or situations, but can and area used for all sorts of applications. They are reusable and are created from blueprints known as classes

26
Q

Use a solar system to describe objects

A

If someone wanting to model the behavior of a solar system might create objects to represent the stars, planets, moon, and asteroids involved. Each object would be able to have fields, also known as attributes, to store data about the boject as well as methods, which are the actions the object can perform. A planet might have fields such as mass, rotationSpeed, and currentPosition, as well as methods like determineNewPosition()

27
Q

Why do we even want to make anything private?

A

So we control the procedures and flow of our logic. We are able to encapsulate this.

28
Q

What is naming conventions?

A

Class should be pascalcase with nouns and methods should be camelcase with verbs

29
Q

What is the access modifier? What is the return? What is the function name? Parameters?

A

components of a method

AM - public, private, protected..default is public
Return - Type of return…can be void if nothing returned
Function name - verb, camelcase
Parameters

30
Q

What is static?

A

We can use this method without the class being instantiated

31
Q

What is a method?

A

A method is just a function that belongs to a class. It is a sequence of statements that are grouped together to perform a certain task. A method can do things like print, get, set, or delete information, or anything else you can think of.

We name methods like getHeight, setSpeed, isValid

32
Q

Explain using public vs private methods

A

We can create objects with public methods that deal with how to interact with the object, but we keep all the details about the logic we are performing inside those functions private

This gives us a lot of control over what sort of information we broadcast, and it also allows us to write code that is more flexible and ready for change.

Kind of a old-fashioned radio. We would want anyone to be able to access the buttons and switches on the outside of the stero, but not share information about the algorithm we use to translate the sound input into something the speakers produce.

33
Q

What is a good example of a static modifier? What is the rule of thunb for static modifiers?

A

Class methods are useful for creating constants and class fields that need to be used throughout a program.

One rule of thumb: ask yourself ‘does it make sense to call this method, even if the Object has not been constructed yet?’

METHODS ARE ACTIONS THAT OBJECTS CAN CALL

34
Q

What are member variables?

A

object attributes or fields, or instance variables…ALL the same thing

35
Q

How are member variables accessed?

A

Member variables are typically declared inside the class before any method and are accessed via getter and setter methods.

36
Q

What access modifiers should member variables have?

A

Shoul almost always be declared as private to prevent access directly to the field

37
Q

What is a constructor?

A

A constructor method is a special function that gets called when an instance of an object is created. The differences between constructor method and the other methods are:

1) It doesn’t have a return type
2) The name of the constructor MUST be the same as the name of the class to which it belongs
3) A constructor is called automatically when a new instance of an object is created.

38
Q

What is overloading?

A

Overloading is just defining new method signatures for the same method name. Now we can instantiate objects in 3 different ways. REMEMBER THAT ANY METHOD CAN BE OVERLOADED

39
Q

What is ‘this’?

A

In Java, the this keyword refers to the current object within the context of an instance method or constructor

40
Q

There is one case where you MUST use this….

A

When your parameter variables shares the same name as one of your member variables

41
Q

Explain inheritence

A

Inheritance is one of the most important features of object oriented programming. It prevents us from having to rewrite a significant amount of code, as well as use classes written by other people without changing that class

42
Q

Overriding Method

A

You can change or extend the method of a superclass by just writing a method that has the same method signature and return type as the parent method. Then the new method will be called on the subclass whenever it is invoked.

Now humans will only “toss and turn” when they try to start sleeping. What if we want to give them some rest and just extend our parent class? Then we can use the super keyword to invoke the superclass method in our own:

43
Q

What is the difference between member variables and static variables?

A

member variables are only part of the instance of that class and then static variables are part of only that class NOT the instance

44
Q

Can static methods CALL other static methods?

A

NO

45
Q

Why use static?

A

Sometimes you want variables that are scared across ALL objects. This is achieved by creating class variables. These variables are in one fixed location in memory, and all objects have access to it. It is also accessible without even creating any instances of the class. For example, suppose that we want to keep track of how many objects from the Person class we have created. We need to use a class variable to hold on to the current count of person objects created.

46
Q

What is an interface?

A

Cat and dog in separate classes, but what if there is some functionality they share? What if they are both house pets? Wouldn’t you want to be able to giveOwnerAffection() and sleepInside()

An interface is NOT A CLASS! A class describes the attributes and behaviors of an object. An interface state BEHAVIORS that a class MUST implement.

47
Q

How is an interface SIMILAR to a class?

A

1) An interface can contain any number of methods
2) An interface is written in a file with a .java extension, with the name of the interface matching the name of the file
3) The bytecode of an interfae appears in a .class file
4) Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name

48
Q

How do you name packages?

A

The convention is that you name your package using a reverse domain name. So for our Human class above, written here at Coding Dojo, the package would be com.codingdojo.

49
Q

Why use packages?

A

Instead of having to save our Java classes into a single project folder, we can use sub folders (packages) to separate our classes. Imagine a large project where every single class file was in the same folder…….

50
Q

How is an interface similar to a class?

A

1) An interface can contain any number of methods.
2) An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
3) The bytecode of an interface appears in a .class file.
4) Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

51
Q

What are annotations?

A

Annotations were introduced to help developers make certain notes about the code.

We use @Override. You can also use @ Deprecated when you think the method the developer used will be phased out.

52
Q

What are POJOS and JavaBeans?

A

POJOS are ‘plain old Java objects’. POJOS are just normal objects instantiated from a class. JavaBeans are POJOs that conform to a series of rules:

1) Member variables must be private, but accessible using get and set
2) Must have a zero argument constructor
3) Must implement the serializable interface

53
Q

What are JavaServer Pages(JSP)?

A

Is a tool that Java uses to make dynamic applications

54
Q

What are servlets?

A

Servlets are Java programs that can respond to requests.

These are independent of Spring Boot. Although servlets are not tied to the web, they are most often used with the HTTP protocol

At its very core, a servlet is a Java class that will act as our application middleware to respond to the request with the appropriate information

55
Q

What do JSPs allow us to do?

A

It allows us to include server side Java code with HTML content. JSPs themselves are an abstraction of Java servlets

56
Q

Servlets and JSPs have been the way that Java developers build web applications. Why do you learn them before Spring boot?

A

Brings up your well roundedness

57
Q

Describe the way tomcat servlet container works

A

When our web server receives a request for a servlet, it cannot hand the request to the servlet class itself. Instead, all servlets will live inside of something called a Container. It is the container’s job to hand off requests to the right servlet and return responses given from a servlet