Module 1 Flashcards
Process of code
Source Code (.java files) –> Byte Code (.class files) –> Machine Code (JVM files)
Variable
A storage container paired with a name. It holds some known or unknown amount of info, called the value.
Data Type
Defines type of data stored in a variable and how its represented
Primitive Data Types
Byte, Char, Boolean, Short, Int, Float, Double, Long
Variable Name
Label that defines what the variable represents. It’s important to give context.
Variable Name Properties
Should be descriptive. Camel case. Case Sensitive.
Widening
Casting from a smaller data type to to a larger data type. Ex: int to a long
Casting is implicit, meaning we dont have to specify it.
Narrowing
Casting from a larger data type to a smaller one. Ex: double to int.
Is explicit meaning the dev has to tell java what new data type the value should be.
Truncation
When we go from larger to smaller data type + lose data in the process
Expression
Code that must be solved first. It is an “incomplete thought”. Evaluates to a single value.
Ex: x - y
Statements
Complete thoughts. Does not need to be solved. Statements end in a semicolon or curly braces. Ex: int x;
Blocks
Are groups of statements that need to execute as a single unit.
Method
A reusable block of code. Takes input and returns output that is related to input. Identified by a method signature that indicates: 1. Who can use it 2. What to call it 3. What it will return 4. What input it takes Some have no arguments.
Method Signature Syntax
accessor return_type name (parameters) {} • Accessor • Return type • Name •Parameters
Accessor
Keyword that identifies who can use the method
Return Type
A data type (int, double, String)
Name
Descriptive name
Parameters
A list of 0…..n variables
public int addNumbers (int x, int y)
Boolean Expression
An expression that evaluates to true or false. Used to conditionally decide what code to execute. Asks questions about the current state.
Logical Operators
&& - AND (both conditions must be true)
|| - OR (at least one condition must be true)
^ - XOR (Exclusive or, one condition must be true but not both)
! - NOT (not)
Ternary Syntax
(variable) = boolean ? true result : false result;
Array
- A series of values of the same data type that are held together in a wrapper.
- Variables for arrays are declared using data types.
- Must know how many items it can store and type of thing you’ll be storing IN ADVANCE.
- Values in an array are called elements
- Elements have an index (starts with 0)
Array syntax
String [] instructors = new String [5]
Static Initialization
Set array + create the values
Scope
- A variables scope defines where in a program that variable is valid.
- When code execution reaches a point where the variables can no longer be referenced then variable is out of scope
Loops
- Allow code to be repeated for a set number of times
* Use a boolean condition and execute the code while the condition is true and stop execution once condition is false
For Loop Syntax
for (int i = 0; i < 10; i++) { system.out.println(i); } • Incrementer variable • Boolean condition that is true • define an iterator expression
Break
Immediately ends a loop
Continue
Ends the current iteration early and continues the loop in the next iteration.
Streams
Steady flows of data.
Ex: system.in and system.out
Scanner Next Line()
Gets text from the input stream up until a newline (enter)
Parsing
Conversion of data between unlike data types
ex: string -> int
Scanner Next Int() Syntax
int userNum = scanner.nextInt();
scanner.nextLine();
Objects
An in-memory data structure that combines state and behavior into a usable and useful abstraction
• Live in a computers memory and only exist while a program is executing
• Each obj is distinct + separate from every other obj.
• Not written in source code
• everything in java is an object, except primitive data types
Class
Source code that defines how to create an object and what state and behavior it will have • A class is a blueprint • It only exists in source code
Steps of Creating an Object
- Declare a variable to hold the object. The class will be the data type
- Instantiate a new object from the class using the new keyword
- Initialize variables inside the object with initial values using the constructor
LegoPerson legoJohn = new LegoPerson(clothes/color, hasCap)
Two Data Types in Java
Value Data Type and Reference Data Type
Value Data Type
References a single static space in memory to hold the value. This memory is located on the stack.