So sometimes I forget how to run a java program...
First compile it.
javac ClassName.java
Then run it.
java ClassName
public class ClassName{
public static void main (String[] args) {
[Main program goes here]
}
}- The program starts executing from the main method. There can be several methods.
import javax.swing.JOptionPane; //import javax.swing.*; works too
public class WelcomeInMessageDialogBox {
public static void main (String[] args) {
//Display 'TEXT' in a message dialog box
JOptionPane.showMessageDialog ( null, "TEXT");
}
}- It's standard to use null as the first argument, and then your text as the second.
- If you don't make 7 in 7/21 into 7.0/21, Java will interpret it as an integer division and give you an incorrect result (if you were intending to do regular division).
Read user input
import java.util.Scanner;
public class Scanner{
public static void main (String[] args) {
Scanner in = new Scanner (System.in); //This creates a new scanner called 'in'
in.close(); //It's good practice to close off user input at the end of your program!
}
}When using the scanner that you created, 'in', there are some common methods that you can use:
in.next()= reads a string that ends before a whitespace characterin.nextLine()= reads a line of text (ends when the Enter key is pressed)in.nextDouble()= reads an number of the double typein.nextInt()= reads an integer of the int typein.nextFloat()= reads a number of the float typein.nextLong()= reads an integer of the long typein.nextShort()= reads an integer of the short typein.nextByte()= reads an integer of the byte type
Print with a line break after the statement
public class PrintBreak{
public static void main (String[] args) {
System.out.println("Wow this will have a line break after the statement.");
}
}Print without a line break after the statement
- You can also do the same thing by typing \n within the " " in your print statement
public class PrintNoBreak{
public static void main (String[] args) {
System.out.print("Wow this will not have a line break after the statement,"
+ "but you could make some like \n\n\n");
}
}Print a formatted output
public class PrintFormat{
public static void main (String[] args) {
System.out.printf("I don't really know how to do this properly yet, but once I do, I'll update this...");
}
}You must declare and initialize variables before you can use them!
int variable1; //Declare one at a time
variable1 = 3; //Initialize it in a separate step
int yo = 4; //Declare a variable and initialize it in one single step.
int variable2, variable3, variable4; //Or do multiple in one go
variable2 = variable3 = variable4 = 0; //Initialize multiple variables, to have the same value, in one step.
int variable5 = 0, variable6 = 1; //Declare and initialize variables of the same type in one step.There are multiple variable types at your disposal:
intdoublebyteshortlongfloatcharboolean
Just know that these variable types vary in storage size. Depending on which variable you use, an amount of memory will be allocated.
Going from smallest to largest storage size: byte, short, int, long, float, & double
Note that = is the assignment operator.
Let's say you want to keep a fixed number and use it throughout your program. How would you go about it? You'd create a named constant.
Its format works like this:
public class NamedConstants{
public static void main (String[] args) {
final datatype CONSTANTNAME = value; //declare the constant value
int useThatValue = CONSTANTNAME; //use the constant value
}
}+Addition-Subtraction*Multiplication/Division%Remainder
Did you notice that there isn't a ** for exponent operations, like there is in Python? If you want to represent an exponent, do the following:
public class Exponent{
public static void main (String[] args) {
double exponent = Math.pow(2, 3); //Raise 2 to the 3rd power to get 8.0
int exponent2 = Math.pow(2.5, 2); //Square 2.5 to get 6.25
}
}A literal is a constant value that appears directly in the program.
eg. int soLiteral = 3;, where 3 is the literal
If your integer literal is super long, use the long type. How? Put L at the end of the super long number.
eg. int reallyLiteral = 4182491781212L;
Weirdly enough, floating-point literals are declared as doubles, not floats. Make a number a float or double by adding F or D at the end of the number.
eg. System.out.println( 1.0F / 3.0F); gives you 0.333333334 whereas you'd get 0.3333333333 without the F
Write 1E2 to get 1 x 10^2
+== Addition assignment-== Subtraction assignment*== Multiplication assignment/== Division assignment%== Remainder assignment
int increment = 0;
increment++; //Use increment's value, then add 1
++increment; //Add 1 to increment's value, then use it
int decrement = 0;
decrement--; //Use decrement's value, then subtract 1
--decrement; //Subtract 1 from decrement's value, then use itConvert floating-point numbers to integers, and vice versa, using explicit casting
System.out.println( (int) 1.7 );
System.out.println( (double) 1 / 2 ); // if you don't do this, the answer will display as 0A character data type represents a single character.
char letter = 'A';
char number = '1';You can convert characters into any numeric type & vice versa, but you need to keep in mind that only the lower 16 data bits are used when an integer is casted into a character.
char ch = (char)0XAB0041; // lower 16 bits hexcode is 0041, so that is assigned to ch
System.out.println(ch); // displays A- Floating-point values --> char
- Procedure: floating-point becomes an integer, then a char.
char ch = (char)55.05; // 55 is assigned to ch
System.out.println(ch); // displays A- char --> int
int i = (int)'B';
System.out.println(i); //this prints out B's UnicodeYou can use implicit casting (type variable = '[letter]') if the result of the casting fits into the range of the type.
For example, in byte b = 'a';, a's unicode is 97 & that fits w/i a byte. That wouldn't be the case if you were doing byte b = '\uFFF4', though. It'd be too big.
To force an assignment, cast the type like this: byte b = (byte) '\uFFF4';
To get out of how your code would regularly be interpreted, you can use escape characters.
System.out.println("Escape your speech \"to say something\"!"); \ \" together represent one characterOther escape characters work as follows:
\b= Backspace. Unicode: \u0008. Decimal value: 8.\t= Tab. Unciode: \u0009. Decimal value: 9.\n= Linefeed. Unicode: \u000A. Decimal value: 10.\f= Formfeed. Unicode: \u000C. Decimal value: 12.\r= Carriage Return. Unicode: \u000D. Decimal value: 13.\\= Backslash. Unicode: \u005C. Decimal value: 92.\"= Double Quote. Unicode \u0022. Decimal value: 34.
String = sequence of characters.
You can concatenate strings with + and +=.
Whitespace characters include ' ', \, \f, \r, or \n.
Caution:
System.out.println("Food " + 1 + 2); //produces: Food 12
System.out.println("Food " + (1 + 2)); //produces: Food 3If you prefer a graphical dialog box instead of one in the console, you can do this:
import JOptionPane.*;
public class GraphicalInput{
public static void main (String[] args) {
String answer = JOptionPane.showInputDialog(x); //Where x is the string that creates the message
}
}Convert a string to an int
int intValue = Integer.parseInt (123456);Convert a string into a double
double doubleValue = Double.parseDouble (123456)Two values: true or false
boolean stayAwake = true;<= less than<== less than or equal to>= greater than>== greater than or equal to=== equal to!== not equal to
//You could just have a one-way if statement
if (CONDITION == IS MET) {
[execute this code];
}
//Or you could do a two-way if statement like this (if-else)
if (CONDITION == IS MET) {
[execute that code];
}
else{
[execute that code];
}
//And you could even do a multi-way if-else statement like this
if (CONDITION == IS MET){
[execute those code];
}
else if (CONDITION2 == IS MET){
[execute these code];
}
else {
[execute them code];
}//You don't need to do this
if (variable == true) {
[some code];
}
//When you could do this
if (variable){
[some code];
}Math.random(); returns a "random" number between 0.0 & 1.0, excluding 1.0.
What if the number you want is 1.0 or bigger?
//For instance, this will generate a random number from 0 to 9
int var1 = (int)(Math.random() * 10);
//This is the same as
int var2 = Math.floor( Math.random() * 10 );Using the random method in conjunction with other Math methods is great.
Math.cos(),Math.sin(),Math.tan(),Math.asin(),Math.acos(),Math.atan()Math.toRadians(),Math.toDegrees()Math.exp(),Math.log(),Math.log10()Math.pow(),Math.sqrt()Math.ceil(),Math.floor(),Math.round()Math.min(),Math.max()Math.abs()
Boolean operators:
!= not (logical negation)&&= and (logical conjunction)||= or (logical disjunction)^= exclusive or (logical exclusion)- The exclusive or,
^, of 2 Boolean operands is true if & only if the two operands have different Boolean values.
- The exclusive or,
If there're multiple cases that you can foresee (due to you giving options, or w/e), then you can use switch statements to make your life easier.
public class Switchie{
public static void main (String[] args) {
switch (expression) {
case answer1: [Do this]; break;
case answer2: [Do this]; break
}
}
}So for instance, you want the user to pick 1 of 3, and depending on which card they pick, your program does something. Part of your program would look like this:
int card = 0;
[get user input]
switch (card){
case 11: System.out.println("You chose card 11."); break;
case 13: System.out.println("You chose card 13."); break;
case 17: System.out.println("You chose card 17."); break;
}Conditional expressions are weird as hell. Let's say you want to assign a number to y depending on the value of x. You type this statement:
int x = 50;
int y = (x > 0) ? 1 : -1;This is essentially saying, if x is greater than 0, then assign y to 1 (y = 1). Otherwise, assign y to -1 (y = -1).
The general format for conditional expressions is as follows:
boolean-expression ? expression1 : expression2;
Format specifiers:
%ba boolean%ca character%da decimal integer%fa floating-point number%ea number in standard scientific notation%sa string
So let's say you have a print statement, and you want to format it, because you happen to be using printf, you'd use:
System.out.printf ("Hello, that will be $%10.2d for those %15s", cost, item); %-10dwill left-justify
import JOptionPane.*;
...
int option = JOptionPane.showConfirmDialog (null, "[TEXT TO USER]");Do something while the statement is true
while (x == 10) {
[Your code that will be executed while x is equal to 10];
}If you want to run the code once before checking the condition (to decide whether to continue), you can use this
do {
[this code];
} while (loop-continuation-condition);for (int i = initialValue; i < endValue; i++){
[Code to be executed];
}If there's only one statement in the loop body, you can omit the braces {}
The following are equivalent:
for ( ; ; ) {
[Code to be executed];
}
for ( ; true; ){
[Code to be executed];
}
//The best one to use
while (true){
[Code to be executed];
}breakwill exit the loop that it's incontinuewill end the current iteration & go to the next one
These keep doing something until the value is changed to 0. (eg. you tell a user to type '0' when they're done.
- input redirection: uses data from another file
- type this into the command line
java SentinelValue < input.txt
- type this into the command line
- output redirection: sends the output to a file rather than displaying it on the console
- type this into the command line
java ClassName > output.txt
- type this into the command line
import JOptionPane.*;
public class ContinueConfirmation{
public static void main (String[] args) {
int option = JOptionPane.YES_OPTION;
while (option == JOptionPane.YES_OPTION){
//do this
option = JOptionPane.showConfirmationDialog (null, "Continue?")
}
}
}Syntax for method definition:
modifier returnValueType methodName (list of parameters with their types) {
//Method body
}
//For instance:
public static void Example (int [] list){
//Method body
}- You can have methods with the same name as long as the arguments you feed into it have different types.
- Call a method by typing
method(argument1, argument2)
voidmethods don't return any values... it sends whatever you do within the function to the abyss once the function has terminated
- Arguments must match the parameters in order, number, and compatible type.
- After you invoke a method w/ an argument, the values of the argument are passed to the method
- Use methods to modularize code to allow you to reuse stuff and manage stuff
- overloading methods lets you use the same name for methods, as long as their signatures are different (the parameters it takes in)
- scope of a variable the part of the program where the variable can be referenced
- an example lies in
forloops; any variables declared in there are local variables, because they only function within their loops
- an example lies in
pow(a, b)= a^bsin([some value in radians]),cos([some value in radians]),tan([some value in radians]),toDegrees([some value in radians])toRadians([degrees]),asin([proportion]),acos([proportion]),atan([proportion])
exp([double x])= returns e raised to the power of xlog([double x])= returns the ln of x ( ln(x) = LOGe(X) )log10([double x])= returns the base 10 log of x ( LOG10(x) )pow([double a], [double b])= a^bsqrt([double x])= returns sqrt of x for x >= 0
ceil([double x])= rounds x to the nearest integer, returned as a double valuefloor([double x])= x is rounded down to nearest integer, returned as a double valuerint([double x])= x rounded to nearest integer; if x is equally close to 2 integers, the even one is returned as a double
min&maxreturn the minimum, or maximum, number of two numbers
(Math.random() * scaleDatWithNumberHere)
Example usage
(int)(Math.random() * 10) //Returns a random integer between 0 & 9- an array's size is fixed once it's created
- access arrays via its index
array[0]
//Basic declaration is like this
elementType[] arrayVariableName;
//For instance
double[] myList;- declaration of array variables don't allocate any memory space for the array; only a storage allocation
- but once an array has been declared, you can do this:
arrayVariableName = new elementType[arraySize];- You can declare a variable & give it a size at the same time
//Way 1
elementType[] arrayRefVar = new elementType[arraySize];
//Way 2
elementType arrayRefVar[] = new elementType[arraySize];
//eg.
double[] myList = new double[10];- get access to the size of an array variable by doing
arrayVariable.length
- You can create an array with its values set, but remember that you can't extend the size:
elementType[] arrayVariable = {value0, value1, value2, ..., valuek};- Since you know the size of an array by using
.length, you should useforloops to process them:
for (int i = 0; i < list.length; i++){
//Do this
}you can iterate through an array by doing this, for example:
for (double u: myList) {
System.out.println(u); //This prints all of the stuffs in the array
}-
Doing
list1 = list2will copy the reference values from list1 to list2, it won't actually copy the contents of the array -
Ways to actually copy arrays
-
Loop through the array, copying elements one by one
-
Use the static
arraycopymethod in theSystemclassSystem.arraycopy(sourceArray, src_position, targetArray, tar_position, length); //example System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);
-
- arrays are passed by sharing to methods, so you can see a change in the array outside of the method
- it's called pass-by-sharing: the array in the method is the same as the array being passed; the reference value is passed to the method, not just the value
public static int[] reverse (int[] list) {
int[] result = new int[list.length];
for (int i = 0; j = result.length - 1; i < list.lngth; i++, j--) {
result[j] = list[i];
}
return result;
}You can treat multiple variables as an array????
public class VarArgsDemo {
public static void main(String[] args) {
printMax(34, 3, 3, 2, 56.5);
printMax(new double[]{1, 2, 3});
}
public static void printMax(double... numbers) {
if (numbers.length == 0) {
System.out.println("No argument passed");
}
return;
}
double result = numbers[0];
for (int i = 1; i < numbers.length; i++)
if (numbers[i] > result)
result = numbers[i];
System.out.println("The max value is " + result);
}elements are compared to a key; if a match is found, the index of the element is returned; otherwise, -1 is returned
public class LinearSearch {
/** Method for finding a key in the list */
public static int linearSearch(int[] list, int key) {
for (int i = 0; i < list.length; i++) {
if (key == list[i])
return i;
}
return -1;
}
}- elements of the list must be ordered already for binary search to work
- this is how it works... if this even makes sense
- If the key is less than the middle element, you continue to search for the key in only the first half of the array
- If the key is equal to the middle element, the search ends with a match
- If the key is greater than the middle element, you need to continue to search for the key only in the second half of the array
Version 1
public static int binarySearch(int[] list, int key) {
int low = 0;
int high = list.length - 1;
int mid = (low + high) / 2;
if (key < list[mid])
high = mid - 1;
else if (key == list[mid])
return mid;
else
low = mid + 1;
}Version 2
public static int binarySearch(int[] list, int key){
int low = 0;
int high = list.length - 1;
while (high >= low) {
int mid = (low + high) / 2;
if (key < list[mid])
high = mid - 1;
else if (key == list[mid])
return mid;
else
low = mid + 1;
}
return -1; //Not found :(
}- Searches the entire list for the smallest element, then swaps it with the 1st element in the list;
- Then searches through the remainder of the list, and does the same thing
- Repeat ^^^
public class SelectionSort {
/** the method for sorting the numbers */
public static void selectionSort (double[] list) {
for (int i = 0; i < list.length - 1; i++) {
//Find the minimum in the list [i ... list.length-1]
double currentMin = list[i];
int currentMinIndex = i;
for (int j = i + 1; j < list.length; j++) {
if (currentMin > list[j]) {
currentMin = list[j];
currentMinIndex = j;
}
}
//Swap list[i] with list[currentMinIndex] if necessary
if (currentMinIndex != i) {
list[currentMinIndex] = list[i];
list[i] = currentMin;;
}
}
}
}- Sort a list by repeatedly inserting a new element into a sorted sublist until the whoe list is sorted
- What the hell does this description even mean... look this up later, please, future self.
public class InsertionSort {
//The method for sorting numbers
public static void insertionSort (double[] list) {
for (int i = 1; i < list.length; i++) {
//Insert list[i] into a sorted sublist list[0 ... i-1], so that list[0 ... i] is sorted
double currentElement = list[i];
int k;
for (k = i - 1; k >= 0 && list[k] > currentElement; k--) {
list[k + 1] = list[k];
}
//Insert the current element into list[k + 1]
list[k + 1] = currentElement;
}
}
}Compare lists
java.util.Arrays.equals(list1, list2) tells you whether two arrays have the same content
Fill a list
java.util.Arrays.fill(list1, 5); //Fill the whole array with 5's
java.util.Arrays.fill(list2, 1, 3, 8); //Fill a part of the array with 8's- object a real-life entity that can be distinctly identified
- objects have properties/attributes, which are represented by data fields w/ their current values
- eg. circle has attribute for radius
- behavior/actions of object is defined by methods
- eg. you can use getArea() for circle objects
- objects have properties/attributes, which are represented by data fields w/ their current values
- objects of the same type have the same class, & there can be many instances of a class
- creating an instance = instantiation
- variables define data fields & methods to define actions
- a class has methods called constructors
- constructors are designed to perform initializing actions (eg. initializing the data fields)
- you can put 2 classes in 1 file, but 1 class can be a public class
public class TestSimpleCircle {
}
class SimpleCircle {
}constructors:
- must have the same name as the class itself
- they don't return a type, not even void
- are invoked using the
newoperator when an object is created (they initialize objects)
- object's data & methods can be accessed through the
.operator via the object's reference variable
- object's reference variables reference the object; syntax:
ClassName objectRefVar;- reference type: a class
Circle myCircle = new Circle(); //myCircle is declared to be of Circle type, then an object is created & referenced to myCircle;- the
.dot operator that invokes methods is also called the object member access operator objectRefVar.dataFieldreferences data field in objectobjectRefVar.method(arguments)invokes method
uninitialized data fields hold the value null
class Student {
String name; //default value null
int age; //default value 0
boolean isScienceMajor; //default value false
char gender; //gender has default value '\u0000'
}- When you create a variable, the value is different based on type
- primitive type: the value is of the primitive type
- reference type: the value is a reference to the object's memory location
- when you make a Random object, you need to specify a seed/use the default seed
- seed: a number used to initialize a pseudorandom number generator
Random random1 = new Random(3);
System.out.print("From random1: ");
for (int i = 0; i < 10; i++)
System.out.print(random1.nextInt(1000) + " ");
Random random2 = new Random(3);
System.out.print("From random2: ");
for (int i = 0; i < 10; i++)
System.out.print(random1.nextInt(1000) + " ");Both random1 & random2 have the same sequence of random int values
- static variables shhared by all objects of the class
- static methods can't access instance members of the class
Declaring static variables methods, and constants:
static int numberOfObjects;
static innt getNumberObjects() {
return numberOfObjects;
}
final static double PI = 3.14;publicmodifier allows the classes/methods/data fields to be accessed from other classes- If you don't use a modifier, the default is that: classes, methods, and data fields are accessible by any class in the same package (called package-private or package-access)
- can always access data fields & methods from inside the class
public class C {
private boolean x;
public static void main(String[] args) {
C c new C();
System.out.println(c.x);
System.out.println(c.convert());
}
private int convert() {
return x ? 1 : -1;
}
}- making data fields private protects data & makes the class easy to maintain
- data field encapsulation: declaring data fields private to prevent direct modifications
Methods have naming conventions to explain their purpose
getmethods
public returnType getPropertyName()booleanmethods
public boolean isPropertyName()setmethods
public void setPropertyName (dataType propertyValue)- passing an object to a method is to pass the reference of the object
- arrays can hold objects as well as primitive type values
- eg.
Circle[] circleArray = new Circle[10]; - you need to use a for-loop to initialize the array
- each element of the array references an object
- Create a string from a string literal w/ this syntax:
//Way 1
String newString = new String (stringLiteral);
//Way 2
String message = new String ("Welcome to Java");
//Way 3
String message = "Welcome to Java";
//Way 4
char[] charArray = {'W', 'e', 'l', 'c', 'o', 'm', 'e', ' ', 't', 'o', ' ', 'J', 'a', 'v', 'a'};
String message = new String(charArray);- Once a string is created, you can't change it.
- Interned string is when Java creates a unique instance for string literals w/ the same character sequence to improve efficiency & save memory
String s1 = "Welcome to Java"; //This is an interned string object for "Welcome to Java"
String s2 = "Welcome to Java"; //This is an interned string object for "Welcome to Java"; s1 = s2==only checks whether two strings refer to the same object, it doesn't tell you if the content is the same- You use the
equalsmethod to check whether they have the same content
- You use the
if (string1.equals(string2))
System.out.println("string1 and string2 have the same contents");
else
System.out.println("string1 and string2 are not equal");compareTois the way to compare two strings lexicographically (depends on Unicode)- You use this rather than comparison operators
>,<, etc. - this will return a value depending on the relation of string1 to string2
- returns
0if s1 is equal to s2 - returns a value less than
0s1 is lexicographically less than s2 - returns a value greater than
0if s1 is lexicographically more than s2
- returns
- You use this rather than comparison operators
String s1 = "abc";
String s2 = "abg";
System.out.println(s1.compareTo(s2)); //returns -4It returns -4 because the first 2 characters are the same, but when c & g are compared, c is 4 less than g, which creates -4.
More String class methods:
equalsIgnoreCasecompareIgnoreCaseregionMatches(compares portions of 2 strings for equalityequalsIgnoreCasestr.startsWith(prefix)str.endsWith(suffix)
- get length of the string:
message.length() s.charAt(index)retreieve a particular character in strings- get a substring from a string
message.substring([start index], [index it goes up to, but not including])'
Concatenate two strings
String s3 = s1.concat(s2); //one way
String s3 = s1 + s2; //another wayAny changes you make to a string just look like changes, but in reality, you get a new string derived from the original
message.toLowerCase()message.toUpperCase()message.trim()(eg."\t Good Night \n"is trimmed down toGood Night)message.replace("this with", "that")message.replaceFirst("first instance of this letter with", "that")message.replace('a', 'b')message.split("#")split at instances of # or whatever other string
- matches: `message.matches("Some string here");
- matches is more powerful than
.equals()b/c it can check whether strings follow a pattern.
- matches is more powerful than
//Check whether there is the string beginning with "Java" followed by 0+ characters; if there is, it evaluates to true
"Java is fun".matches("Java.*");
"Java is cool".matches("Java.*");
"Java is powerful".matches("Java.*");
//Check whether it follows this format
"440-02-4534".matches("\\d{3}-\\d{2}-d{4}"} //evaluates to true- replace all:
message.replaceAll("replace this", "with this"); - split:
"Java,C?C%, C++".split("[.,:;?]");This will split it into the array tokens Java, C, C#, C++ - find the index of a character:
"Hey".indexOf('H')returns 0. - find the index of a character from a certain index:
message.indexOf("string", [index]);
To convert a string to an array:
char[] chars = "Java".toCharArray(); //Each character in the string is a new element of the array.Copy particular parts of the string and convert that into an array, where each character is an element of the array:
message.getChars( [(int) beginHere], [(int) endHere], [char[] dst], [(int) dstBegin] )char[] dstindicates which array you want to put the elements of the substring intodstBeginindicates which indices which indices of the destination array that you want to replace/add the new substring to
String.valueOf([some number or characters]); will return an array of characters converted into strings
eg. String.valueOf(5.44) --> '5', '.', '4', '4' in an array
The syntax to format strings is String.format(format, item1, item2, ..., itemk), which is similar to the format for print statements.
- Create a
Characterobject from acharvalue. (eg.Character character = new Character('a');)
Different Character methods:
variable.isDigit(char)variable.isLetter(char)variable.isLetterOrDigit(char)variable.isLowerCase(char)variable.isUpperCase(char)variable.toLowerCase(char)variable.toUpperCase(char)variable.countLetters(char)(count the occurrences of each letter in a string)
StringBuilderandStringBuffercan be used wherever strings are used, the only difference is that they're more flexible thanStrings.- can add new content into the
StringBuilderandStringBufferobjects (so they are changeable) - Difference between
StringBuilderandStringBuffer: StringBuilders are synchronized, meaning you can only use one task to execute the methods- But it's more efficient if you only need a single task
StringBuffercan be accessed by multiple tasks concurrently- Both the
StringBuilderandStringBufferhave the same methods & constructors;
StringBuilder stringBuilder = new StringBuilder();stringBuilder.append("some text");stringBuilder.insert(11, "some text");(inserts the text in index 11)stringBuilder.delete(indexStart, indexEnd);(delete things in that range)stringBuilder.reverse()stringBuilder.replace(indexStart, indexEnd, "Words");(replace the characters in that range)stringBuilder.setCharAt(0, 'w');(changes a character at the index specified)
stringBuilder.capacity();(returns the # of characters the string builder can store w/o having to increase in size)stringBuilder.length();stringBuilder.setLength(newLength);(sets the length of the string builder; string will be truncated if it's less than the current length) (if it's greater, there'll be null characters added)stringBuilder.charAt(index);(tells you what character is at a certain indexstringBuilder.trimToSize();(This will reduce the capacity to the actual size, getting rid of all of the parts that aren't being used)new StringBuilder (initialCapacity)creates a StringBuilder with a specified initial capacity
- immutable classes --> immutable objects
- eg.
Stringclass
- eg.
- contents of immutable objects are ... immutable
- immutable classes properties:
- all data fields must be private & can't have any
setmethods aspublic
- all data fields must be private & can't have any
In order to create an immutable class, meet these criteria:
- all data fields must be private
- there can't be any mutator methods for data fields
- mutator method: a method that changes a variable; known as
setter method
- mutator method: a method that changes a variable; known as
- (talking about scope in terms of class, not loops)
- scope of instance & static variables: the entire class
- this is the class regardless of where the variables are declared
- you can put a class's variables & methods in any order
- exception: when a data field is initialized based on a reference to another data field
- then the other data field needs to be declared first
- it's good practice to declare data fields @ beginning of a class
- can declare a class's variable many times, but can use the same variable name in a method in different nonnesting blocks
- hidden variable: a local variable w/ the same name as a class' variable takes precendence, & the same-named class variable is hidden (example below)
- exception: when a data field is initialized based on a reference to another data field
public class F {
private int x = 0; //Instance variable
private int y = 0;
public F() {
}
public void p() {
int x = 1; //Local variable
System.out.println("x = " + x); //This x is x = 1, rather than x = 0;
System.out.println("y = " + y); //This is y = 0 because y wasn't declared locally
}
}thisrefers to the object itself; it can be used inside a constructor to invoke another constructor of the same class... yeah- usually,
thisreference is omitted; but you need thethisreference to reference hidden data fields or invoke an overloaded constructor
- usually,
eg:
public class Circle {
private double radius;
...
public double getArea() {
return this.radius * this.radius * Math.PI;
}
public String toString() {
return "radius: " + this.radius + "area: " + this.getArea();
}
}Using this to Reference Hidden Data Fields
- hidden data field: a data field name is used as a parameter in a
setmethod --> the data field is hidden in thesetmethod- in this case, you need to use
thisin order to access the data field
- in this case, you need to use
public class F {
private int i = 5;
private static double k = 0;
public void setI(int i) {
this.i = i;
}
public static void setK(double k) {
F.k = k; //the value passed to the function is set as the value of k of this class, F
}
}- use
thisto invoke a constructor of the same class, like:
public class Circle() {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public Circle() {
this(1.0);
}
}- class abstraction: the separation of class implementation from the use of a class
- class encapsulation: hiding the details of a class' implementation
More about class abstraction:
- class's contract: the instructions the developer writes to tell the user how functions are used
- a class is known as an abstract data type (ADT) b/c you can find & compute things of a class without knowing how they're actually computed
- in OOT, the focus is on designing methods; you use methods & data together in objects
- procedural paradigm: focus on designing methods; (data & methods are separate)
- OO paradigm: data & methods are coupled into objects
- objects can create another object
- composition: the relationship between the two objects
- composition is a case of the aggregation relationship
- aggregation models has-a relationships & shows ownership relationships between 2 objects
- aggregating object: the ower object
- may be owned by several other aggregating objects
- if an object is owned by only 1 aggregating object, the relationship b/t them is called a composition
- aggregating class: the aggregating object's class
- aggregated class vs aggregating class
- aggregating object: the ower object
- multiplicity: a number/interval specifying how many of the class's objects are involved in the relationship; (* = unlimited)
- each class involved in a relationship may specify a multiplicity
- classes describe a single entity & its class operations work toward a coherent purpose
- place data declarations before the constructor
- place constructors before methods
- consistantly provide a public no-arg constructor to construct a default instance
- you should document the reason for not including one if you decide not to include one
- if no constructors are explicitly defined, a public default no-arg constructor w/ an empty body is assumed
- encapsulating data fields: class should use
privatemodifier to hide data from direct access by clients
- design classes that allow the user to freely use it however they want
- always reference static variables & methods from a class name (Not a reference variable) to improve readibility & avoid errors
- use
setmethods to change static data fields - a constructor is always instance b/c it's used to create a specific instance
- a static variable/method can be invoked from an instance method, but an instance variable/method can't be invoked from a static method
- can wrap primitive data types in their respective wrapper objects
- eg
intinInteger,doubleinDouble,charinCharacter;
- eg
- wrapper classes don't have no-arg constructors
- thus, once the objects are created, internal values can't be changed
- conversion methods, & other common methods are on p.395
- primitive type values can be converted to object using a wrapper, & vice versa
- boxing: converting a primitive value to a wrapper object
- unboxing: converting a wrapper object to a primitive type
- autoboxing & autounboxing: the Java compiler automatically boxes a primitive value that appears in a context requiring an object & unboxes an object that appears in a context requiring a primitive value
- these are used to represent huge integers and decimal numbers... who would've known
- they are both immutable!!! Immutable. Immutable.
- can use
new BigInteger(String)&new BigDecimal(String)to create instances of BigInteger & BigDecimal to useadd,subtract,multiple,divide, &remaindermethods on, andcompareTo
- inheritance: defining new classes from existing classes