Vocab Code Examples
More vocab examples in code
double x = 2.09;
double y = 6.52;
int z = (int) (y/x);
int aa = (int) (x*y);
System.out.println(z);
System.out.println(aa)
- Casting - Division - Seen above, casting can round the division for y/x so that it returns an integer, this is to allow for certain code beyond that that would only be able to take integer values for a specific reason.
- Casting - Rounding/Truncating - Seen in both examples above, casting is great for rounding a value to an integer or for another reason in order to simplify code.
- Wrapper Classes - Classes that are able to make primitive types into objects to be used in things such as ArrayLists that would not allow primitives. See below for an example.
// Wrapper classes example
ArrayList<Integer> ton = new ArrayList<Integer>();
// ArrayList<int> would not work for this
// Concatenation example
String a = "Hello";
String b = "World";
int c = 25;
String d = a + " " + b + " " + c;
System.out.println(d);
- Concatenation - Concatenation involves combining two or more strings together. This is done by using the + operator. Anything that's not a string is attempted to be converted to a string in order to be concatenated on the end, like the int in this example.
- Static/Class Variables - Static variables are variables that are shared across all instances of a class. This means that no matter how many objects you instantiate of the same class, modifying a static variable or method will update it for all instances. This is useful for things like a counter that you want to increment for every instance of a class.
- Inheritance/Extends - Using an extends method allows it to inherit attributes and methods from the class it is extending. This is useful when you want to modify or create a subclass of a class without having to rewrite all of the code for the class you are extending.
public class StaticExample {
public static int x;
public static void main(String[] args) {
x += 1;
System.out.println("Add 1. Result: " + x);
}
}
// Extends static example and modifies x value
public class ExtendsExample extends StaticExample {
public static void main(String[] args) {
x += 5;
System.out.println("Add 5. Result: " + x);
}
}
public class PolymorphismExample {
public void testOutput(int x) {
System.out.println("Integer: " + x);
}
public void testOutput(String x) {
System.out.println("String: " + x);
}
public void testOutput(double x) {
System.out.println("Double: " + x);
}
}
StaticExample se = new StaticExample();
ExtendsExample ee = new ExtendsExample();
se.main(null);
ee.main(null);
PolymorphismExample pe = new PolymorphismExample();
System.out.println("\nPolymorphism Example:");
pe.testOutput(5);
pe.testOutput("Hello");
pe.testOutput(5.5);
-
Polymorphism - Polymorphism is the ability to have multiple methods with the same name but different parameters. This is useful when you want to have multiple methods that do the same thing but with different parameters. This is also useful when you want to have a method that can take multiple types of parameters.
- Overloading is when you have multiple methods with the same name but different parameters.
- Overriding is when you have a method with the same name and parameters as a method in a superclass but you want to modify the method in the subclass.
- Late binding is when you have allow the compiler to determine which method to use at runtime instead of compile time. Abstract Class - Objects cannot be created from an abstract class, they can only be extended. This is useful when you want to create a class that can be extended but not instantiated.
abstract class Car {
public void distanceDriven() {
System.out.println("Car has driven 100 miles");
}
}
class SUV extends Car {
public void distanceDriven() {
System.out.println("The SUV has driven 80 miles");
}
}
class Truck extends Car {
public void distanceDriven() {
System.out.println("The truck has driven 50 miles");
}
}
// Instantiating the Car class would throw an error as it is abstract
SUV suv = new SUV();
Truck truck = new Truck();
suv.distanceDriven();
truck.distanceDriven();
- Subclass Constructor / Super - Extending a class and then constructing the new extends class will call the constructor of the superclass. This is useful when you want to have a constructor that will call the constructor of the superclass.
- Referencing Superclass - Using the super keyword allows you to reference the superclass of the current class. This is useful when you want to reference the superclass of the current class.
public class AClass {
public void aMethod() {
System.out.println("This is a method from AClass");
}
}
public class BClass extends AClass {
public void bMethod() {
super.aMethod();
}
}
// Constructing a BClass object will call the constructor of the AClass object
BClass bc = new BClass();
bc.bMethod();
// Referencing superclass method
AClass ac = new BClass();
ac.aMethod();
- Standard Methods - ToString is a standard method that is used to convert an object into a string. Equals is useful for comparing two objects to see if they are equal. HashCode is useful for getting a unique hash code for an object.
- Big O Notation - Implementing Big O notation is useful for determining the efficiency of an algorithm. Making algorithms more efficient is useful for making programs compile and run faster and likely reduce the load of it, which could be especially good for websites where users may be runnning the same function through an api many times.