Looks like no one added any tags here yet for you.
Strings
Important variable type that stores words and numbers, but cannot do any arithmetic operation
Characters
An 8 bit data type that stores a letter, number, symbol, space, etc.
Difference between integer, character, and string (in terms of syntax)
Integer: 3
Character: ‘3’
String: “3”
String Class
A class that includes numerous methods that can manipulate strings
str.charAt(index);
Returns a character at a specific index
str.length();
Finds the total length of the string (outputs an integer)
str.substring(int start); and str.substring(int start, int end);
Allows the Java to point at a specific index and output the remaining characters following that index.
Can pinpoint a start and end point
str.equals();
Comparing string values with another
Outputs a boolean value
str.equalsIgnoreCase();
Compares strings, but ignores the casings
Outputs boolean value
Index
A numerical assignment of each character in the string
Starts at 0, and is 1 less than the total length of the string
Errors with Indices
Since the final index is 1 less than the total length of the string, an error will occur if you input an index value greater than or less than what is there.
Anything < 0
Anything > str.length();
These are known as IndexOutofBound Errors
ASCII Table
American Standard Code for Information Interchange
A table of characters for the computer
Characters /w the ASCII Table
Every character is assigned a numerical value
Ex: ‘a’ has a value of 97
Case Sensitivity with Characters
Although we see ‘a’ and ‘A’ as the same letter but different casings, Java sees it as ‘97’ and ‘65’.
Meaning java casing matters in Java
Casting
Casting a data type allows Java to output either a number or a character value depending on what you ask for
Increments Dealing with Characters
These are not the Same:
letter++;
letter = letter + 1;
letter = letter + 1;
This is trying to add a character and an integer together
CompareTo() vs equals()
Both compare 2 different strings, however, they are slightly different
CompreTo()
Returns 0 if they are the same and returns less than or greater than 0 depending on the order.
CompareTo() analyzes it alphabetically
Equals()
Returns a boolean value (true or false)
Exception
An error that disrupts the flow of the code
Try-Catch Statements
A block of code that will catch any errors that may occur within the statement.
It will only catch the errors within the Try statement
Common use of Try-Catch Statements
InputMismatch Error
This is when the use inputs a different variable type than the one that is needed.
Exception-Handler
A try-catch statement is broken up into 2 parts
Try - Exception
The block of code that is being checked for errors
Catch - Exception Handler
Catches the error and handles it
Array
A data structure that can hold multiple of the same data type together.
Elements
This is the data within the array
Difference between variable and an array
An array points to a memory location which is why it is known as an object.
If we simply print the array, it will output the memory address of the array
How does an array work
An array splits up the elements using indices. Just like a string, every element has an index assigned to it.
Different parts of an array
int[] grades = new int[20];
Data Type: int
Array indicator: []
Name: Grades
Size: [20]
Syntax to Declare
int[] marks;
marks = new int[5];
Initializing an array
An array can be preset values to it, however, the syntax is a little different. Everything will be the same except after declaring the size you will use curly brackets {}, and write the values you want inside dividing it using commas.
Accessing elements within an array
Write the name of the array and surround the index/element with square brackets.
Changing a value in an array
Access the element you want and treat it as a variable
Length of an Array
Syntax is very similar to a string, thus being:
array.length;
Only difference is the array doesn’t have brackets at the end while strings do
Index Errors
Just like strings, you cannot ask for an element/index that does not exist. There will be an error telling you that element does not exist.
ArrayIndexOutofBounds Exceptions
Method Set Up
Access Levels:
Public
Private
Return Type:
Void
Data Types
Address:
Method name
Parameters:
The variables you want in the method
Void Method
Does not return values back to the main method, so whatever is in the method stays in the method.
Key word return is not needed
Passing data into methods
When initializing the method you only have to write the name of the variable in the brackets. However within the parameters of the method, the variable’s data type must line up with the ones on the initializer.
Order matters
Data within a method
The passed data is not the same as the data in main. Although in some cases the variable name is the same, they are not the same variable. Meaning whatever changes are made within the method, will only affect the variable in it and not main.
Return in Method
This is used when we want to return a value back into main using method.
The data type you are returning must match the return type in the method header
Arrays in Method
When passing arrays into methods this is known as a PASS-BY REFERENCE. This is because an array is an object that has a memory location/memory address.
Changes made to the array in a method will also be changed in main since both point to the same memory location
Pass By Value VS Pass By Reference
Pass By Value: when a variable is assigned to another variable, the value stored in the variable is copied into the new variable.
Pass By Reference: Passing objects to a method.