Last active
December 27, 2021 00:16
-
-
Save Kev-in123/281b5ca64773e275c094bc3683edcdde to your computer and use it in GitHub Desktop.
Test: Strings & Methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Some of these may have errors as i was too lazy to check if they work or not | |
//Recursion exsists in java but i'm not adding them in these notes | |
public class MethodExamples { | |
//the main method, the entry point of the program | |
//methor name: main | |
//return type: N/A | |
//parameters: String[] args | |
public static void main(String[] args) { | |
System.out.println("Hello World"); | |
} | |
//a greeting method | |
//method name: greeting | |
//return type: String | |
//paramaters: String name | |
public static String greeting(String name) { | |
return "Hello " + name; | |
} | |
//add 2 numbers | |
//method name: add | |
//return type: double | |
//paramaters: double num1, double num2 | |
public static double add(double num1, double num2) { | |
return num1 + num2; | |
} | |
//By using method overloading, multiple methods can have the same name with different parameters | |
//subtract 2 numbers | |
//method name: subtract | |
//return type: int | |
//paramaters: int num1, int num2 | |
public static int subtract(int num1, int num2) { | |
return num1 - num2; | |
} | |
//subtract 2 numbers | |
//method name: subtract | |
//return type: double | |
//paramaters: double num1, double num2 | |
public static double subtract(double num1, double num2) { | |
return num1 - num2; | |
} | |
//You can use method overloading to achieve default paramaters | |
//multiply 2 numbers | |
//method name: multiply | |
//return type: double | |
//paramaters: double num1, double num2 | |
public static double multiply(double num1, double num2) { | |
return num1 * num2; | |
} | |
//multiply 2 numbers | |
//method name: multiply | |
//return type: double | |
//paramaters: N/A | |
public static double multiply() { | |
return 3.542 * 4.653; | |
} | |
//multiply 2 numbers | |
//method name: multiply | |
//return type: double | |
//paramaters: double num1 | |
public static double multiply(double num1) { | |
return num1 * 4.653; | |
} | |
//another example of a void method | |
//method name: print | |
//return type: N/A | |
//paramaters: String n | |
public static void print (String s) { | |
System.out.println(s); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Strings: | |
useful built-in functions for strings that were stolen from the docs | |
char charAt(int index) Returns the char value at the specified index. | |
int compareTo(String anotherString) Compares two strings lexicographically. | |
int compareToIgnoreCase(String str) Compares two strings lexicographically, ignoring case differences. | |
String concat(String str) Concatenates the specified string to the end of this string. | |
boolean contains(CharSequence s) Returns true if and only if this string contains the specified sequence of char values. | |
static String copyValueOf(char[] data) Equivalent to valueOf(char[]). | |
boolean endsWith(String suffix) Tests if this string ends with the specified suffix. | |
boolean equals(Object anObject) Compares this string to the specified object. | |
boolean equalsIgnoreCase(String anotherString) Compares this String to another String, ignoring case considerations. | |
static String format(String format, Object... args) Returns a formatted string using the specified format string and arguments. | |
int indexOf(int ch) Returns the index within this string of the first occurrence of the specified character. | |
boolean isEmpty() Returns true if, and only if, length() is 0. | |
int lastIndexOf(int ch) Returns the index within this string of the last occurrence of the specified character. | |
int lastIndexOf(int ch, int fromIndex) Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index. | |
int lastIndexOf(String str) Returns the index within this string of the last occurrence of the specified substring. | |
int lastIndexOf(String str, int fromIndex) Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index. | |
int length() Returns the length of this string. | |
boolean matches(String regex) Tells whether or not this string matches the given regular expression. | |
String repeat(int count) Returns a string whose value is the concatenation of this string repeated count times. | |
String replace(char oldChar, char newChar) Returns a string resulting from replacing all occurrences of oldChar in this string with newChar. | |
String replaceAll(String regex, String replacement) Replaces each substring of this string that matches the given regular expression with the given replacement. | |
String replaceFirst(String regex, String replacement) Replaces the first substring of this string that matches the given regular expression with the given replacement. | |
String[] split(String regex) Splits this string around matches of the given regular expression. | |
boolean startsWith(String prefix) Tests if this string starts with the specified prefix. | |
String strip() Returns a string whose value is this string, with all leading and trailing white space removed. | |
String stripLeading() Returns a string whose value is this string, with all leading white space removed. | |
String stripTrailing() Returns a string whose value is this string, with all trailing white space removed. | |
String substring(int beginIndex) Returns a string that is a substring of this string. | |
String substring(int beginIndex, int endIndex) Returns a string that is a substring of this string. | |
char[] toCharArray() Converts this string to a new character array. | |
String toLowerCase() Converts all of the characters in this String to lower case using the rules of the default locale. | |
String toUpperCase() Converts all of the characters in this String to upper case using the rules of the default locale. | |
String trim() Returns a string whose value is this string, with all leading and trailing space removed, where space is defined as any character whose codepoint is less than or equal to 'U+0020' (the space character). | |
static String valueOf(boolean b) Returns the string representation of the boolean argument. | |
static String valueOf(char c) Returns the string representation of the char argument. | |
static String valueOf(char[] data) Returns the string representation of the char array argument. | |
static String valueOf(double d) Returns the string representation of the double argument. | |
static String valueOf(float f) Returns the string representation of the float argument. | |
static String valueOf(int i) Returns the string representation of the int argument. | |
static String valueOf(long l) Returns the string representation of the long argument. | |
Unicode and stuff: | |
Each character has a Unicode equivalent, which the character is translated to before storage. Example ‘A’ has the Unicode 65 and ‘a’ is 97. Because there are under 256 characters, any character value can be stored in a byte of storage space and thus this is the standard capacity used to store characters. | |
When characters are compared, their Unicode values are compared therefore, ‘a’ > ‘A’ is a true statement. | |
When strings are compared, they are compared lexicographically; from left to right, characters at the same position are compared until the characters at that position are different and these become the deciding character. Example "ab" < “ac” and “abs” > “ab” and “abcdefg” > “aa” | |
Notes on strings: | |
- Do not use '==' to compare 2 strings | |
- However '==' can be used to compre 2 chars | |
- Do not re-invent the wheel, there are many built-in functions for strings | |
- You can split the string to make it easier to work with | |
- in string concatenation, there is no space added, you must do it manually | |
Methods: | |
return types: | |
void - no return value | |
int - returns an integer | |
long - returns a long | |
String - returns a string | |
char - returns a char | |
double - returns a double | |
float - returns a float | |
boolean - returns a boolean | |
int[] - returns an array of integers | |
long[] - returns an array of longs | |
String[] - returns an array of strings | |
char[] - returns an array of chars | |
double[] - returns an array of doubles | |
float[] - returns an array of floats | |
boolean[] - returns an array of booleans | |
parameters: | |
- the values passed to the method | |
- seperated by commas | |
- the data type of the paramater must be specified | |
Since there is a coding part: | |
Naming conventions: | |
1) under 256 characters | |
2) only letters, digits and underscores (_) | |
3) can’t start with a digit | |
4) no spaces | |
5) no reserved keywords (refer to quiz 1 study notes for a list of reserved keywords) | |
Data types: | |
boolean - stores true or false | |
byte - stores whole numbers from -128 to 127 | |
char - stores a single character/ASCII values | |
int - stores whole numbers from -2,147,483,648 to 2,147,483,647 | |
long - stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | |
short - stores whole numbers from -32,768 to 32,767 | |
float - stores fractional numbers, sufficient for storing 6 to 7 decimal digits | |
double - stores fractional numbers, sufficient for storing 15 decimal digits | |
Assignment Operators: | |
a = b; | |
- assigns b to a | |
a += b; - same as a = a + b; | |
- can be used to increment a number by b | |
- or to concatenate strings | |
- note that if you do this to 2 chars, the chars will be converted to ints (their ascii value) | |
a -= b; - same as a = a - b; | |
- can be used to decrement a number by b | |
- note that if you do this to 2 chars, the chars will be converted to ints (their ascii value) | |
a *= b; - same as a = a * b | |
- can be used to multiply a number by b | |
- note that if you do this to 2 chars, the chars will be converted to ints (their ascii value) | |
a /= b; - same as a = a / b | |
- can be used to divide a number by b | |
- note that if you do this to 2 chars, the chars will be converted to ints (their ascii value) | |
a %= b; - same as a = a % b | |
- can be used to divide a number by b and return the remainder | |
- note that if you do this to 2 chars, the chars will be converted to ints (their ascii value) | |
Arithmetic Operators: | |
+ adds the numbers (also used to concatenate strings to strings/numbers/chars) | |
- subtracts the numbers | |
* multiplies the numbers | |
/ divides the numbers (if both numbers used are both integers, drop the decimal once done dividing) | |
% divides the numbers and returns the remainder | |
++ increments the number by 1 | |
-- decrements the number by 1 | |
Logical Operators: | |
|| - or | |
&& - and | |
! - not | |
Comparison Operators: | |
== - equals to (does not apply to strings; however, you can use these to compare chars) | |
!= - not equals to (does not apply to strings; however, you can use these to compare chars) | |
> - greater than (does not apply to strings; however, you can use these to compare chars) | |
< - less than (does not apply to strings; however, you can use these to compare chars) | |
<= - less than or equal to (note the order of the angle bracket and the equals sign) (does not apply to strings; however, you can use these to compare chars) | |
>= - greater than or equal to (note the order of the angle bracket and the equals sign) (does not apply to strings; however, you can use these to compare chars) | |
Catch-Exception: | |
try - tries to execute some code | |
catch - catches the exception raised in try (if applicable) | |
finally - executes no matter if the try-except failed or passed | |
throw - throw an error | |
throws - what error may be thrown | |
Since I have trust issues: | |
switch - The expression is compared with the values of each case, if none match, then the default is used, if there isn't a default, nothing happens | |
case - Used in a switch-case statement to specify a case | |
default - The default block of code in a switch statement, if it's the last statement, it doesn't need a break | |
break - exits the loop/switch-case | |
continue - stops the current iteration of the loop and moves on to the next | |
if - used to check a condition, if true execute the code once | |
else if - if the condition in the if statement is false and the condition passed is true, execute the code | |
else - if the condition in the if/else-if statement(s) is false, the code is executed | |
while - similar to if, however, continually executes the code until the boolean is false | |
do - used together with while to create a do-while loop (please don't do this, just use a while loop normally) | |
for - typically used to perform a task a specific number of times or be used to loop through an array | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StringExamples { | |
//Ignore the duplicate local variable errors | |
public static void main(String[] args) { | |
String s = "Hello World"; | |
System.out.println(s.charAt(0)); | |
// output: H | |
String s = "Hello World"; | |
String s2 = "Hello There"; | |
System.out.println(s.compareTo(s2)); | |
// output: 3 | |
// ('W' - 'T' = 3) | |
String s = "Hello World"; | |
String s2 = "Hello world"; | |
System.out.println(s.compareToIgnoreCase(s2)); | |
// output: 0 | |
// (cases are ignored) | |
String s = "Hello World"; | |
String s2 = "Hi"; | |
System.out.println(s.concat(s2)); | |
// output: Hello WorldHi | |
String s = "Hello World"; | |
String s2 = "Hello"; | |
System.out.println(s.contains(s2)); | |
// output: true | |
String s = "Hello World"; | |
String s2 = "Hi"; | |
System.out.println(s.contains(s2)); | |
// output: false | |
String s = "Hello World"; | |
String s2 = "World"; | |
System.out.println(s.endsWith(s2)); | |
// output: true | |
String s = "Hello World"; | |
String s2 = "Hi"; | |
System.out.println(s.endsWith(s2)); | |
// output: false | |
String s = "Hello World"; | |
String s2 = "Hello World"; | |
System.out.println(s.equals(s2)); | |
// output: true | |
//cases are checked in .equals | |
String s = "Hello World"; | |
String s2 = "hello world"; | |
System.out.println(s.equals(s2)); | |
// output: false | |
//cases are checked in .equals | |
String s = "Hello World"; | |
String s2 = "hello world"; | |
System.out.println(s.equalsIgnoreCase(s2)); | |
// output: true | |
String s = String.format("%d + %d = %d", 1, 2, 3); | |
System.out.println(s); | |
// output: 1 + 2 = 3 | |
// might as well do | |
System.out.format("%d + %d = %d", 1, 2, 3); | |
// output: 1 + 2 = 3 | |
// String.isEmpty() is self explanatory but oh well | |
String s = ""; | |
System.out.println(s.isEmpty()); | |
//output: true | |
String s = "hi"; | |
System.out.println(s.isEmpty()); | |
//output: false | |
String s = "hello world"; | |
System.out.println(s.lastIndexOf("l")); | |
// output: 9 | |
//returns -1 if not found | |
String s = "hello world"; | |
System.out.println(s.lastIndexOf("j")); | |
// output: -1 | |
// String.length() is self explanatory but oh well | |
String s = "hello world"; | |
System.out.println(s.length()); | |
// output: 11 | |
String s = "Hello World"; | |
System.out.println(s.matches("Hello(.*)")); | |
// output: true | |
String s = "Hi"; | |
System.out.println(s.repeat(3)); | |
// output: HiHiHi | |
String s = "Hello World!"; | |
s = s.relpace("l", "a"); | |
System.out.println(s); | |
//Output: "Heaao Worad!" | |
String s = "Hello World"; | |
System.out.println(s.replaceAll("(.*)World", "AHHHHHHHHHHH")); | |
// output: AHHHHHHHHHHH | |
String s = "Hello World"; | |
System.out.println(s.replaceFirst("l", "a")); | |
// output: Healo World | |
String s = "Hello World"; | |
String[] str = s.split(" "); | |
for (String s1 : str) { | |
System.out.println(s1); | |
} | |
// output: Hello | |
// cont'd: World | |
String s = "Hello World"; | |
System.out.println(s.startsWith("Hello")); | |
// output: true | |
// using backticks to show where the string starts and ends for the next few | |
// examples | |
String s = " Hello World "; | |
System.out.println(s.strip()); | |
// output: `Hello World` | |
String s = " Hello World "; | |
System.out.println(s.stripLeading()); | |
// output: `Hello World ` | |
String s = " Hello World "; | |
System.out.println(s.stripTrailing()); | |
// output: ` Hello World` | |
String s = "Hello World"; | |
System.out.println(s.substring(5)); | |
// output: ` World` | |
String s = "Hello World"; | |
System.out.println(s.substring(3, 7)); | |
// output: lo W | |
String s = "Hello World"; | |
char[] chars = s.toCharArray(); | |
for (char c : chars) { | |
System.out.println(c); | |
} | |
// output: H | |
// cont'd: e | |
// cont'd: l | |
// cont'd: l | |
// cont'd: o | |
// cont'd: | |
// cont'd: W | |
// cont'd: o | |
// cont'd: r | |
// cont'd: l | |
// cont'd: d | |
String s = "Hello World"; | |
System.out.println(s.toLowerCase()); | |
// output: hello world | |
String s = "Hello World"; | |
System.out.println(s.toUpperCase()); | |
// output: HELLO WORLD | |
String s = " Hello World "; | |
System.out.println(s.trim()); | |
// output: Hello World | |
int n = 30; | |
String s = String.valueOf(n); | |
System.out.println(s + 10); | |
// output: 3010 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment