Basics_Java Flashcards

1
Q

Where do we put parameters for a function?

A

we use parameters to pass values to function

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

Where do we put the methods for our functions?

A

Inside the curly braces.

In Java the curly brace is on line with function name and return type

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

what is a good function (method name)?

A

clearly identifies the purpose of the function

camelCase notation

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

What is a function?

A

A block of code that performs a task

Think of buttons on a remote

In Java, functions are the smallest building blocks

Ex. functions to validate user info, convert weight to kgs

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

What is a package?

A

A concept for grouping related classes.

We will have hundreds or thousands of classes.

We should organize these classes into packages.

Java creates a namespace for our classes:

com.namespace

*every class we create in our program will belong to this package

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

Where do we specify a return type?

A

A function is a block of code that performs some task.

We specify the return type before the name.

Some functions don’t return anything.

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

Where is the entry point into our program?

A

Every Java program must have one function.

The main function is executed first.

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

What is a class?

A

A container for related functions.

We use classes to organize our code.

Ex. The supermarket has related products in different “classes” or sections.

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

What class must every Java program have?

A

Every program in Java has a main class that contains the main function.

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

What is a method?

A

A method is a function that is part of a class

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

What is an access modifier?

A

A special keyword that determines if other classes and methods can access these classes and methods.

Private

Public

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

What is the naming convention for classes and methods?

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

What are the two steps involved in running a Java program?

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

How do we get byte code?

A

Java Compiler changes our source code into byte code

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

Who developed Java?

A

At Sun Microsystems.

Sun Microsystems was later acquired by Oracle in 2010.

It was originally called Oak named after the tree outside Goslings office.

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

Where can our Java byte code run?

A

Java byte code is platform-independent

It can run on Linux, mac, windows or any operating systems that have a Java Run-Time Environment

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

What is java byte code?

A

The instruction set for the Java Virtual Machine

Assembled code is runnable on a CPU with a specific instruction set, while bytecode can be executed in a virtual machine (such as the Java runtime) on any CPU that can run the VM. Assembly code is (represents) the native code for the processor you are programming.

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

What is a Java Run Time Environment?

A

The Java Runtime environment has a component called Java Virtual Machine (JVM)

JVM translates our byte code into the native executable language for various platforms (Mac, Windows, Linux)

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

What is byte code?

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

What does the Java Virtual Machine (JVM) perform?

A

Converts our Java Byte code into the native code for the operating system we are using like a mac, windows or linux operating system.

This makes applications portable.

Can execute on Linux, Mac or any other operating systems that have a Java Run-Time Environment.

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

Who owns Java?

A

Oracle

Oracle Corporation is the current owner of the official implementation of the Java SE platform, following their acquisition of Sun Microsystems on January 27, 2010.

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

How many editions are there of Java?

A

Four Editions for different types of applications

SE - all the libraries all Java developers must learn

EE - provides additional libraries for building fault-tolerant multi distributed systems

ME - libraries specific for mobile phones

Java Card - for smart cards

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

How many worldwide developers does Java have?

A

9 million developers world wide

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

How many mobile phones run Java?

A

3 billion mobile phones currently run Java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What is JDK?
**Java Development Kit** **Software Development Environment** Compiler Reusable Code Java Run Time Environment
26
How many TV sets run Java?
120 Million TV sets Every blue-ray device
27
What is JVM?
The JVM has **two primary functions**: to allow Java programs to run on **any device** or **operating system** (known as the "**Write once, run anywhere**" principle), and to **manage and optimize** **program memory**. When Java was released in 1995, all **computer programs** were **written to a specific operating system**, and program memory was managed by the software developer. So the JVM was a revelation.
28
What is the average **US salary** for a Java developer?
$101,929 USD Java is everywhere. There are many opportunities to get hired as a professional Java developer.
29
What topics does this course include?
Build clean code Fix bugs and errors Understand types Create algorithms Package and deploy programs so other people can use them!
30
What's included in **types**?
Variables and Constants Primitive and Reference Types Casting Numbers, Strings and Arrays Reading Input
31
What is a **variable**?
A variable is how we temporarily store data in computer memory.
32
What is an **identifier**?
The **name of a variable** "=" is **assignment operator**
33
What can we **do with variables**?
Copy the value of one into another
34
What is the naming convention for variables?
Camel case! lowercase first letter Uppercase other letters! Ex. int **h**er**A**ge = 30
35
What are the two types in Java?
36
What are the primitive types in Java?
int - allow us to store values up to 2 billion long - store numbers greater than 2 billion double - storing values with decimal values float - storing values decimal values char - storing characters boolean - store true or false
37
What is best practice for variables?
always use meaningful, descriptive names
38
What must we do to a long type in Java?
add and L after the value. By default Java compiler recognizes a number as an integer. Put an L after to tell Java compiler its a long!
39
What must we do to use a float type?
Java see's numbers with decimals as doubles by default. We have to add a suffix to represent the number as a float Add "F"
40
How do we initialize a char type?
surround **single characters** by **single quotes** string represents a series of characters
41
What are **reference types**?
objects + their members (data/functions)
42
What is **castin**g?
Type conversion
43
How do we initialize **reference types (objects)**?
use the **new operator** Allocate memory
44
How will **Java execute** this **statement**?
**First,** allocate some memory to store the point object **Next**, allocate a separate part in memory to store a reference (the address) to the point object **Finally,** assign this reference to the variable name
45
How are **primitive types** stored in memory?
**Value** is **stored** in the **variable at that memory location** x and y are **completely independent** of each other
46
What are we copying into **pointTwo** in this expression?
The **reference** to **pointOne** object in memory, **not the point object**
47
What is the **crucial difference** between how **primitive and reference types** are **stored in memory**?
When we declare a **primitive variable** like a byte the value we assign is **stored in that memory location**. When we use a **reference type** our variable **holds the reference (address) of the object** in memory **not the actual value**.
48
How does **Java** store **reference types** in **memory**?
Variable holds the **address or reference** of the object in memory, **not the actual value**. Two variables can **reference the same object**.
49
What should we remember about the differences between primitive and reference types?
Remember, reference types are copied by references. primitives are copied by values and are completed independent of each other.
50
What are strings in Java?
Strings are reference types. We use a short way to create them (can omit the new operator) Strings are a class so we can access methods with the dot operator!
51
What can we do with strings?
because strings are a class, we can access methods like ends with, length, indexOf(), replace etc. Ex. check length of user input string Ex. give the index of the first character
52
What is the difference between parameters and arguments?
**parameters** are the holes we define in our methods. **arguments** are actual values we pass to these methods.
53
What are strings in Java?
Immutable We cannot mutate them, we cannot change them any methods that modify a string create a new string object
54
What is a useful string method in Java?
the trim method used if a user types unnecessary white spaces in form fields
55
What don't **primitive types** have?
**Members** properties (data) or functions
56
What is an **object**?
**An instance** of **a** **class** **A reference type** **object/classes** have **members we can access** using the **dot operator**
57
What type are arrays?
Reference types We need the new operator we can access elements or items in the array via an index
58
By default, when we print an array what does Java return?
Java returns the string of the address in memory to see the actual items in the array we must use the array's class
59
What is a shortcut for generating **System.out.println()** ?
"sout" + Tab
60
How many **bytes** are used in **primitive types**?
61
How can we initialize string objects in Java?
String is a reference type however, we have a shortcut Can omit the new operator JavaRuntime will handle
62
How do we return the string representation of an array?
Use the array class .toString method there is a .toString for every primitive type
63
What two categories of types in java?
64
What is **one of the differences** between the **primitive and reference types** in java?
**Reference types** use the **new operator** to allocate memory for the reference variable **Primitive** **types** don't require us to **allocate memory** java run time environment does this for us!
65
What are the four most common String **escape sequences**?
**\"** = \"fileNames\" **\n** = new line **\** = \ **\t** = tab
66
What is a modern syntax for initializing an array when you know the values ahead of time?
67
What do we use arrays to do?
Store a list of items: numbers people message
68
Describe the Arrays class in Java?
Defined in Java.util **Has useful methods:** Arrays.toString
69
What can we create in Java?
Multi-dimensional array's Can create a 2D array to store a matrix or a 3D array to store data for a cube These are useful for scientific computations Ex. two rows and three columns
70
What is the convention for naming constants?
all CAPS add the final
71
What arithmetic operators do we have in Java?
All the same in math like "+" "-" "\*" "/"and "%" (modulus or remainder) / gives a whole number
72
What is method overloading?
In Java.util package Arrays Class Arrays.toString method - returns string rep of array defined for all primitive types
73
What is an expression?
A piece of code that produces a value
74
How can we execute a division to produce a decimal result?
By default Java returns integer values for integer division. Cast our input into double #s are called **operands**
75
What must we do when using division?
cast the numbers into a double
76
What is the incrememnt operator?
if we want to increment a value we can add ++ Can say ++x (prefix) OR x++ (suffix) same value
77
What is the **x, y and j** **values** be in this **expression** using **increment operator**?
78
What will the **value** of **x and y** by using the **increment operator (prefix)** in **this expression**?
**First**, Java will **increment x by one** x (=2) **Then**, Java will **copy x value** to y (=2)
79
How can we declare a constant in Java?
add final keyword cannot change value later in program
80
What will the value of **x and y** by using the **increment operator** **as a suffix** in **this expression**?
**First,** Java assigns the value of x (=1) to y **Then,** increments x by one x (=2)
81
What happens when we use the increment operation after the assignment operator?
if increment as a suffix x++ first value of x is copied to y then x is incremented by 1 if increment as a prefix ++x first, x is incremented by 1 then copied to y
82
What is the augmented (compound) assignment operator?
We can increment by 2 x+=2 will increment x by 2 and copy into x
83
What are the order of operations in Java
If we want a value calculated first, we can put it in parentheses.
84
What is implicit casting?
Automatic casting / automatic conversion Java allocates an anonymous variable in memory that variable is another primitive type (byte, int, long) Java copies value into this new anonymous variable If the value can be converted into a bigger type, Java automatically does this for us. short = two bytes int = four bytes
85
When does implicit data casting happen?
when there is no change for data loss when we are going from a smaller to a larger data type
86
What is explicit casting?
convert x to an integer w/o decimal point x will be added to 2 result = 3
87
When can explicit casting only happen?
between compatible types all number types cannot cast a string to a number
88
What are wrapper classes?
For **all primitive types**, have wrapper classes reference type defined in java.lang reference classes have methods like parseInt() which takes a string returns a short or int
89
Why does casting matter?
in most GUI data from the user is received as a string! If we have text boxes or drop down lists almost always we receive data as strings we must convert these strings to numerical representatives
90
What does the Math.max class return?
The greater of two values
91
How to comment out a block of code in Java?
Control + Shift + /
92
What is the Math.random ( ) method return?
A random number (float) to set between a value, multiple by range
93
How can we explicitly cast and round a random number?
pass the random method as an argument to the round method explicitly cast the value into an integer
94
How can we input data in Java?
use the scanner class Java will do implicit casting to make the int value of age into a string and concatenate
95
What is the shortcut for renaming a variable?
Shift + F6
96
How can we read a string using the scanner class?
using the scanner.next ( ) method
97
How do we read the whole line using the scanner method?
use the scanner.nextLine ( )
98
What is the **shortcut** for **renaming** a **variable**?
**Shift + F6**
99
How can we chain multiple methods to remove whitespace when reading a string in Java?
use the dot operator to access members (methods) of the string object call the trim method then store value in the name variable
100
How to implement a **mortgage calculator**?
First attempt No input validation Written as **procedural code**
101
What is **control flow**?
controlling the **flow of execution** of our programs
102
What is **control flow**?
controlling the **flow of execution**
103
Why do we use **loops**?
**executing code repetitively** Ex. **For loops** **For each** **While loops** **Do-while**
104
What is **covered** in this **section**?
**Comparison operators** comparing values Ex. **\>= \<= ==** **Logical operators** implementing real-world rules Ex. **&& != ||** **Conditional statements** making decisions around code execution in our programs Ex. **If, switch** statements **Loops** executing code repetitively Ex. **for, while** loops
105
What are **comparison operators**?
Used for **comparing values** greater than equal to less than \>= \<= ==
106
When do we use **comparison operators**?
To compare primitive values Ex. is x and y the same?
107
What is the equality operator?
a boolean expression returns true/false represented with two equal signs "=="
108
What is the inequality operator?
A boolean expression != returns a true/false
109
Why do we use **conditional statements**?
Making **decisions** in our **programs** ## Footnote **Ex. If statements**
110
What are other equality operators?
\>= (greater than or equal) \<= (less than or equal) These return boolean (true/false) values
111
How does Java evaluate **&&** logical operator?
Evaluates from left to right **First**, it looks at the leftmost condition if false, doesn't look at other conditions, returns false
112
What do we use **logical operators** for?
Implementing **real-world rules** & || !
113
What does a single "=" mean?
the assignment operator we use it to assign a value to a variable
114
When is the || operator true?
The or || operator is true if at least one condition is true.
115
When do we use the NOT operator?
To reverse a value if the value hasCriminalRecord = false; !hasCriminalRecord returns true
116
What is a clean way to simplify an if statement?
use a boolean operator wrap in ( ) to be clear
117
How can we **reformat** our **if-statement** so **it's easier to read**?
Remove { } braces from single statement clauses put else if on the line below ending curly brace ( { )
118
What's the problem with declaring a variable in our if-statement?
It's **only available** within that **code block** we **cannot access** outside
119
How can we **read data** from the user?
Use the **Scanner class**! Read from the **console**: **new Scanner (System.in)**
120
How do we implement an **if statement**?
If statement three clauses look at format Hierarchy parent w/ two children } else if (condition) } else
121
Why are if statements extremely powerful?
They allow build programs to make decisions based on certain conditions? Ex. Printing messages on console based on certain conditions
122
What do we type inside the brackets after an if statements?
A boolean value if the condition is true, the statements in curly braces are executed.
123
What is a clause in an if statement?
a section parent and children hierarchy If only one line of code in the if statement can omit the { } Technically, people say always have { }
124
What is the problem with this if statement?
The variable hasHighIncome is only available within the curly braces!
125
What is one way we can solve the issue of only being able to access a variable within curly braces?
Declare the variable outside the if-statement set the value to true within the statement \*\*this code look amateurish
126
How can we improve out if-statement?
we can set the variable to an initial value. Or we can set our variable to an expression and eliminate the if-statement entirely if the expression is true it will evaluate to true, otherwise it will evaluate to false.
127
Why does Mosh wrap the right side of the expression in parentheses?
To make it more clear now it's clear that on the right side of the assignment operator we have a boolean expression
128
What kind of code doesn't a professional programmer write?
This kind. One way to fix this is give the variable an initial value This can be simplified even further using a ternary operator!
129
How do we use the **ternary operator**?
**Condition (boolean expression)**? value #1 : value #2 We start with our condition then we add a "?" if the first condition is true, value after "?" is assigned otherwise the value after ":" is assigned String className = income \> 100\_000 ? "first" : "economy"
130
When do we use switch statements?
To execute different parts of code depending on the value of the expression. like an if-statement
131
Why do we add a "**break**" statement to our **switch statement**?
Java will **continue to execute** the **other cases** once it **evaluates a break statement** it jumps out of the **switch statement**
132
How do we implement a **switch-statement**?
add a **break** after **each case** **default** will execute if **no cases are met**
133
What's the correct solution to FizzBuzz?
134
How do we implement the FizzBuzz method?
Put the most specific statements on top Java evaluates top to bottom
135
What's the trade-off with this implementation of FizzBuzz?
It has a nested structure. Mosh prefers a flat structure. Additional nested structures make the code hard to read
136
What three things do we do inside the ( ) of a for loop?
1. **Declare a counter variable** ( i , j , k) - loop counters 2. **Declare a boolean expression** - declare how long loop is executed 3. **Increment loop counter** \*If a single expression, don't need curly braces but if multiple statements must declare a code block
137
How does Java execute a for loop?
1. first it initializes i to zero (first statement) 2. Then it evaluates the boolean (if true, executes code block) 3. At end of loop, control moves to increment i to ++
138
How can we implement a for loop using the decrement operator?
Output: Hello World 5 Hello World 4 Hello World 3 Hello World 2 Hello World 1
139
How does the **ternary operator work**?
start with a **condition** if the condition is true, evaluate and assign value after "?" otherwise, if the condition is false, evaluate and assign value after " : "
140
Why is this not the right solution for FizzBuzz?
Because of the way **Java executes** the if statement Sequentially executes Put most complex case first Simpler cases follow
141
What is the **basic syntax** for a **while loop**?
initialize a **loop variable** **set a condition** for termination **execute code** in the **block** **decrement the counter variable**
142
When should we use a for **loop** vs. **while** loop?
**For Loop:** We know how many times we want to execute one or more statements **While Loop:** We don't know exactly how long to execute
143
How can we write a **while loop** to continuously **ask a user** to enter something until they **write quit**?
144
What are **Do..while** loops?
Similar to while loops Gets executed at **least once** **the condition gets checked last**
145
What is the **syntax** for a **do-while loop**?
146
**While loops** vs. **Do...While loops**?
**While loops** check **conditions first**, if false it never gets executed. **do..While loops** we check the **condition last**! Do...while loops are rare and **we often use while loops**!
147
How does Java execute the **break statement**?
it **ignores everything** after and **break statement** and **exits out of the loop**.
148
What is an example of a **for loop**?
Set counter to **count down** or **count up**!
149
What is the **continue statement** in a **while loop**?
Java sees the continue statement moves the control to the beginning of the loop all statements after are ignored
150
What is a **common technique** used by **professional developers** when writing **while statements**?
set the **while condition** to (true) the loop is executed forever until the **break conditio**n is met! make sure to use a **break statement** otherwise, you have an **infinite loop**!
151
What must we **include** when writing always true **while statements**?
**while (true)** include a **break statement!** otherwise, you have **an infinite loop**!
152
What is the **basic structure** of a **for loop**?
use "**for**" keyword a **counter variable** and **initialize** **boolean expression** determines how many times loop is executed increment the counter variable
153
When do we use **For-Each loops**?
When **iterating** over an **array or collection**! Replaces **a for loop** we don't have to **write a numeric counter** we don't have to write a **boolean expression** we don't have to **increment our counter**
154
How do we declare a for-each loop?
inside for ( ) include the **type** of array (collection) we are iterating over declare a variable to hold the value of each item one at a time in our collection per iteration. **Ex**.
155
What are the **limitations** of **for-each** loops?
**For each loop:** 1. **Forward only** - Cannot iterate end to the beginning. 2. **Don't have access to index** - loop variable stores only one item **For Loop:** can access **values** by **index**
156
How can we iterate over an array backwards using a for ( ) loop?
Set the counter variable to length of the array as long as i is greater than 0 print value at the index of counter decrement counter
157
How can we implement a **while loop** to **handle error validation** in our **mortgage calculator**?
**Initialize** the **variables** to **zero** **outside the loops!**
158
What separates an **outstanding programmer** from an **average programmer**?
**Knowing how to** **write good code**: **Code** that is **clean and expressive**
159
What does Martin Fowler say about clean coding?
Mosh puts a lot of emphasis on **clean code**!
160
What is **Mosh** going to show us in this section?
The most **important techniques** for writing **clean code.**
161
What is a **parameter** variable just like?
It's just the same as if we declared a **local variable** inside it's like a **local variable** **inside the method**
162
How do we **create a method**?
camelCase for **method name** and **parameters** **parameter** is like a **local variable** inside **the method**
163
How do we create a **method that returns** a value?
Change return type call the method from the main class Give it an argument store the return value in a variable
164
What is the **syntax** for multiple **method parameters**?
**cameCase** for **parameter identifiers** separate with a **comma**
165
How do we return a value from our method?
Change return type from void to String return keyword to return value \*when calling this method can store the return value in a variable
166
What is refactoring?
Changing the structure of our code without changing its behavior. Extracting certain methods and putting them in other methods. **Extracting methods** **Refactoring Repetitive Patterns**
167
When refactoring, what **two things** should we look for?
1. **Look for conceptually close code** - lines of code that always go together. Bring lines together into a separate private method. Can re-use method in the future! 2. **Repetitive patterns in code** - code that uses repetitive loops asking the same questions using similar patterns
168
How long should our **ideal methods** be?
**5-10 lines of code!** No more than **20 lines**!
169
How can we **extract a method** for **calculating the mortgage**?
Create a **method** for **calculating mortgage** Call this **method** in the **main class**
170
How can we extract code into a method using an Intellij shortcut?
**Right-click** the lines Select **Refactor** Select **Extract** Choose **Method**
171
How can we **refactor** these **repetitive loops**?
Create a **readNumber** method!
172
What **debugging** and **deploying application** **topics** are covered?
173
What are **compile-time** errors?
Syntax errors. Prevent us from compiling our applications. Happen when we don't follow grammar or syntax of Java. Easy to **find and fix**!
174
What is a great resource for a syntax error you don't know?
Stackoverflow.com! Professional programmers and enthusiasts! Search for error message 99% of time you can find a page on this error!
175
How can we find **run time** errors?
Debugger! **Execute** our code **line by line**. Look at the **value** of **various variables**!
176
What are the two types of errors?
**Compile-time:** syntax or grammar errors, use IntelliJ or StackOverflow **Run-time errors:** use Debugger to look at variables
177
What are the **common compile-time** errors?
1. Forget to **specify the data type** of variables 2. Forget to **add a semi-colon** to the end of statements! 3. Call a **method** without using ( ) 4. Print a **string** without double quotes 5. **misspell or incorrectly capitalize** a variable name (Java is case sensitive) 6. using **reserve keywords** for identifiers 7. using a **single "=" to compare values**"=="
178
Do you have to pay to use **Java**?
**Yes** and **No**. **No,** it's **open-source**. **Yes**, there are additional **features/support** you can **pay to use.**
179
What is the call stack in the debugger?
An area where we can see all the methods that were executed in reverse order! Useful for debugging large applications! Can set a breakpoint and watch all the methods to get there!
180
What are **watches** in the **debugger**?
can **set watches** to view **specific variable values**
181
What does **deploying web** or **mobile apps** require?
**Additional steps** outside the **scope of this course.**
182
What is a **module**?
Another **layer of abstraction** **above packages**
183
What must we do if we want to share our **Java program** (console program) with someone else?
need to package it into a .jar file Java Archive - contains all code for distribution Anyone with Java Run Time environment can run it
184
How do we generate a .jar file in IntelliJ?
**File** -\> **Project Structure** -\> **Artifacts** ## Footnote **Build -\> build artifacts**
185
What is the **IntelliJ shortcut** for **multi-cursor** editing?
**Option (press)** + o**ption (hold)** + **down arrow**