Tuesday, December 22, 2015

Java Training Class Notes

While at work, I took an intermediate Java and Servlets training course and these were some of the notes I took during the class. Enjoy!


========================================================================


Instance Variables are a field

Encapsulation is when you put stuff in a class and everything is private

A class is a blueprint that uses classes with objects

Static classes = put static keyword next to it; you can’t make an object of static classes; you also don’t make objects because the fields and methods may not exist and would cause a null pointer error

The java term for CREATING an object is to INSTANTIATE it

Servlet = a java class

Types of access modifiers: Public (most visibility), Private (least visibility), Protected (can’t be used in another class outside of the package or subclasses (“child classes” derived from inheritance), Default (not putting one)

If there are no access modifiers (ex int x = 5), you can use those types of variables and methods within subclasses in the same package only

Primitive data types (all 8 of them): Byte, int (4 bytes), short (a type of int), long (8 byte integer aka a long number), Boolean (true/false in all lower case), char (single character with 2 bytes per character), float (4 bytes), double (8 bytes)

How to make a long variable: long x = 1234567890L (use an upper case L because a lower case one will get confused with a “1”)

Doubles and longs have floating points

How to make float variable: float x = 1234.567f (the F can be upper or lower case, but most people use lower) – there is also no perfect 0 with floats and it goes to .0000…1

Not having an “f” with a float variable causes the IDE to assume that the variable is a double

Floats are also not completely accurate because the rounding is different/off because it calculates based on logarithms (ex, float .27f x 100 is not the same as .27f)

Double doesn’t have perfect 0 either

Don’t use a primitive data type when needing to modify data

Passing by value vs by reference: Passing by value is for a primitive data type and by reference (address) is for an object

Division: / = dividing, % = modulus

String = an object and NOT a primitive data type, not mutable (not able to be changed) – if you want to change a string, you have to create a new object

Characters are a single letter only

Garbage collector = system.gc

The operating system has tons of tables

Rounding = math.round(); can take in a float or double in order to round to the nearest int (ex math.round(8/5)); returns a number

Java.lang package = doesn’t need to be imported (comes with Eclipse and most items are static)

Decimal format = NOT static or java.lang; need to import and make an object of it; decimal format is used for rounding; it returns a string; part of the java.text package

Naming conventions: Class = start with an upper case and the rest of the words are upper case; method/variable = start with a lower case and the rest of the words are upper case; methods are called with xyzMethod(); -- you always need the () in order to make it a method

Constructors = Name needs to match the classname (otherwise it violates “normal naming conventions”) – it’s the method that runs when the object is instantiated; it provides us tools for the object

Static methods are invoked with the classname and objects are invoked with the reference name (the name you called the object that you made)

Qualified name = everything in the storage hierarchy

Import java.x.* = import the entire package at the level where the “*” is and NOT below it

Resolving external reference: referring to something outside of a class that is trying to be brought in and then an error appears (and you have to fix it)

Constructor = runs first when an object/instance variables are instantiated; you need to initialize local variables

No arg constructor (default constructor) = calls the superclass super – super(); -- the super class involves parent and child classes (super classes are parents, subclasses are children) – the user can enter values or have everything initialized to default – you can add a default constructor by right clicking in the code area and selecting “source” – the super constructor is referenced implicitly in the default constructor, but adding it into the constructor is a good coding habit

Overloading a method: Same method with different parameters and different variables and types

Scope = where the code parts can be seen (ex within the method, within the class)

Duration = the time in memory

Mutator = changes the value of the instance variables (another name for a setter)

The object class does NOT have a super class

Constructors don’t have a return type

Whatever you put inside of () determines which constructor you run (when you’re calling one)

You need a parent class for inheritance

A servlet is just a java class (a regular old java class)

Java classes need: fields (variables), setters and getters that the constructors use, constructors, a toString() method, a place to test the code (ex a test app), and other (test) methods as needed

Legal Values: Private instance variables, public setters and getters; you define a method to give users access

You need OBJECTS and a super class for the “trickle down effect”

Always override the toString

Don’t put the test program and actual program in the same package!

Accessor = getter (grab values from variables) – call it by saying getTheMethod()

Mutator = a setter

Getting a Boolean variable – don’t call it “getSomething”, call it “isSomething” (ex isPositive, isEnabled)

This = this OBJECT and does NOT refer to the instance variable (hides the instance variable)

Setters/getters/constructors = in the eclipse source (you can generate it automatically)

Parameter = local variable = method (local variable) = variable in parameter

Always have access to instance variables

This.value = value  this = the instance variable; value = the local (parameter) variable

Memory address same vs different – NOT used to compare contents of 2 objects (use “.equals”, which is used with Booleans)

Equal vs equivalent  equal = same value (1=1); equivalent = references the same object

Pass an object = pass an address; variable = value

instanceOf = relational operator = sees if object type is the same… if no, then need to type cast (ex myClass(object)) – casting allows the types of the variables to match

Same type = comes from the same class

instanceOf also sees if something is an instance (variable) and sees if the variables are in the same class AND same original object  ex apple a = new apple, orange b = new orange, apple c = new apple  a and C are true for instanceOf, whereas a and b are not

Comparing to a primitive data type – make a method – returns an overloaded method in the example and can use an “==” and compare your object fields to primitive data types

Object – NOT an “==” and use it with primitive data types and numbers (use “=”)

hashCode = value generated when objects are created and use when comparing to other collections (ex arraylist, vectors)

equalsIgnoreCase = checks for capital letter vs small letter

vector class = synchronized =  1 user at a time, use on a servlet, is serializable now

arraylist = expandable array, not synchronized, servlets have multiple users at a time, use on standalone apps

serializable = read/write from disk byte by byte (Java does NOT do that); objects can be written to disk byte by byte to get the unique address

capacity = get more space when needed

double link list – java updates pointers by itself

data structure = arraylist or stuff linked together; based on pointers; java doesn’t do that for you and you need to link classes together

Clone objects = use assignment operator (=) (ex a = 3, b = 5, a = b) is NOT a clone, but sets a equal to b

There is NOT a copy method right now

You need to implement the “cloneable” method (implement cloneable) – an object can be cloned now and would need a try/catch block to ensure the clone you want to do is ok and “valid” – clonenotsupportedexception = forgot to implement cloneable

Deep/shallow copy  shallow = good if stuff is only primitive data types (bad with object references because it would only reference the original and not the cloned object); deep = makes a clone of class/object references where shallow copies ONLY primitive data types – no referenced object = null pointer error

Exceptions are events

Finalize = system.gc() – analyze free space

Final = constant

Composition/aggregation = “has a” relationship; component parts die with the method and they go out of scope (depends on one parent) – base out of scope, components go out of scope too – components within the parent

Aggregation = pass addresses into the method, base out of scope = components are OK because separate pointers – addresses passed to parents, but they’re separate params of the constructor

No object of “static”

Inheritance/polymorphism use different packages and public/protected

Methods and params need to match; super = first call in a constructor

Main uses object as its superclass because it’s static

No comments:

Post a Comment