CSC 335 - Java Basics

CSC 335 - Notes


Primitive Types are types built into the language (think integers, floats, doubles, etc.), and are referenced by value as opposed to reference.


Reference Types are types that, when variables are made for them, contain a reference to the object itself. 


Alias - Two variables that refer to the same reference are called aliases


New - Java keyword that allocates memory for a new object (specifically, on the heap).


What does the new keyword do? When used alongside a valid constructor call, the new keyword returns a reference to a location in memory where the object’s fields are stored.


A Class defines an object; does so by defining what data the object contains (instance variables or fields), what the object can interact do (methods)


Dereferencing refers to accessing an object’s data or methods through the object name (book.title, this.xxx)


An Instance is what we call an individual object of a class. For example, a student object called “studentOne” is an instance of the Student class, and is a Student-type object.


A static method is a class-defined method that does not require an object of that class type in order for it to be used


A static variable is a member variable that is common amongst ALL objects of that given class. For example, students at a college would all have the same member for “collegeName”, so we could call that a static variable.


How do you change a static variable? You can change static variables by using a static method call that is able to change an object’s static member. However, this will change the static member across all objects of that class, not just the one instance (because static variables are shared across all objects of a given class, and thus changing this value will affect all objects of that class)


The private keyword is an access modifier that limits the use of an attribute, method, or constructor to only the class that it is declared within


The public keyword is an access modifier that allows an attribute, method, or constructor to be freely used by another class


Global variables are variables declared within a program (outside of any methods) that can be accessed from anywhere in the program. Global variables are sort of like a “Class variable” in that they are the same as declaring a static instance variable that applies to all instances of a class.


The final keyword is a non-access modifier (a modifier that is explicitly not an access modifier), that is used in general to restrict what can be done to a variable, method, or class.


A final variable is essentially a constant variable that can not be changed once it has been initialized.

EX: final int x = 2;

Sets an integer value x to be unmodifiable since it has already been initialized.


A final method is a method that can not be overridden by a sub-class or child-class. This is useful when you do not want inherited classes to be able to change a parent class method.


A final class is a class that can not be inherited or extended from. That is, it can not have any sub-classes or child-classes created from it. Useful when you want a class to be used standalone, and do not want it to be extended.


If you decide to use a global variable / static instance variable, it is wise to make sure it is non-writable (so that it can not be edited)


You are expected to not create any global variables whatsoever in 335. Using global constants like PI and E from the Math class is OK


public is an access modifier that makes something visible everywhere (that is, visible outside of a class)


The default visibility in Java is what is used when you do not specify an access modifier explicitly. This makes something visible from anywhere in the package


The protected keyword makes something visible anywhere in the package AND to any subclasses (whether or not they are in the same package doesn’t matter)


An instance method is a method that belongs to an object, which specifies what the object can do and how it interacts with other objects


An instance method can generally be marked private if it is only needed within the class itself, EX helper methods


An instance method should be marked as public if it is part of the API


The import statement is used to reference one or more classes in a package, so that their simple names can be used instead of their fully-qualified names everytime they are used in code.


The fully qualified name of a class or method in Java refers to its full package-prefixed name, for example java.util.List


Parametric polymorphism refers to the ability for a single variable to interoperate with multiple types. For example, the ArrayList class is defined in the API as ArrayList<E>, where E is a type variable, which is what allows ArrayList’s to hold any object type.


The diamond operator is used when instantiating generic class objects. EX: ArrayList<String> specifies String for the type, since ArrayList takes a generic object type as its argument (uses parametric polymorphism)


The collections framework is a set of classes that have to do with Collection-type objects


~~~~~Some Collections framework stuff


Interfaces

Set - Specifies a collection that does not allow duplicate values

List - A sequence, in which each item has a “position”

Map - A collection of key-value pairs


Implementations of These Interfaces

HashSet - An implementation of Set interface backed by a hashtable

ArrayList - An implementation of the List interface that is backed by an array

HashMap - An implementation of the Map interface that is backed by a hashtable

TreeMap - An implementation of the Map interface that is backed by a red-black tree


~~~~~~



Throwable is the root of the exception hierarchy, and provides the exception and error subclasses. It is the superclass of all errors and exceptions in the Java language


The throw keyword is used for explicitly throwing a single exception


The throws keyword is used alongside a methods name to denote that the function is capable of throwing a specific exception


The try / catch keywords are used to surround a block of code that might throw an exception, and the catch block is what runs in the event that the exception is actually thrown.


If a method throws an exception, any code that calls this method must handle it by either: catching the exception using a try-catch block, or passing the exception on by making the calling method throw it


In Java, subclasses can inherit from only one parent class. Java does not support ‘multiple inheritance’


If you write a class that does not extend another class, then this class automatically extends the Object class. All classes in Java ‘trace back’ to the Object class 


All classes in Java inherit from the Object class, which includes the equals, toString, and hashCode methods (these are capable of being overridden so that they can do something more specific to the object in question)


equals is an Object class method that is used to compare references (i.e returns true if this object and parameter object refer to the same object)


toString is an Object class method that returns a String representation of the object


hashCode is an Object class method that returns a hash value (integer) for the object


Polymorphism in Java has to do with type flexibility, aka the ability for something to take on multiple forms


Overloading refers to methods or operators that have multiple definitions but have the same name.


Parameter Coercion is when an argument for a method/operator is implicitly changed to fit the type expected by the method/operator. This happens automatically in Java when dealing with primitives that are the same or of smaller size than the type expected (avoids data loss).


Subtype Polymorphism is created through inheritance. When classes B and C extend a parent class A, any method that expects type A should also work for type B and C.