Menu

Java_Fundamentals

Gregory M Lyon Jay Jay Billings Dasha
There is a newer version of this page. You can find it here.

A comprehensive overview of the fundamentals of the Java programming language.

Programming Styles

Procedural-Based: An old practice that avoids code reuse to be more direct. This approach saves a small % of efficiency by being direct, and loses a very large % of efficiency by being unorganized.


OO-oriented: Incorporating techniques such as data abstraction, encapsulation, polymorphism, and inheritance; allows data to be easily modified for changing requirements & updates, and keeps the data hidden. Although this does have a slightly more indirect nature to the program, it is a small cost of efficiency that will be made up later by being able to see where to improve the code later when profiling.


OO-based: OO-oriented, except there is no explicit inheritance, polymorphism, and only a small amount of the code are objects (typically the GUI components).


Prototype-based: OO-oriented, except classes define the type of data and functionality that objects will have (behavior and structure). Instances are based on the patterns of a particular class carrying object specific information (state).

OOP

Class: The blueprint for an object to be instantiated from, containing attributes and data structures.


Object: An instance of a class; necessary for encapsulation.


Encapsulation: Hides data so it is not exposed, and this indirect path loses a few% efficiency. This allows the user to easily make changes later when requirements change (no bottlenecking) and can easily view efficiency of the program.


Instantiate: To create a new instance of an object.


Inheritance: Creates a derived subclass as an extension of a superclass. The subclass gets all the public methods and all the attributes of the superclass. An “is a” relationship between the classes. This is done in the form—public class subclass extends superclass {}


Composition: Similar to inheritance, except is a “has a” relationship. This is done in the form— Bird red = new Bird();


Abstract class: At least one method of this class is abstract and has no implementation, so this is always a base class. The purpose of this is to pass general shared methods down to many other classes for code reuse and encapsulation.


Interface: An implicitly abstract type that classes must implement, used to define a contract for how classes interact with an object.


Refactoring: technique for restructuring an existing body of code, altering its internal structure without changing its external behavior by replacing all of a certain name with another. This can also be used for changing class from to super/sub class, moving methods, etc.


Inner (or Nested) Class: distinct from subclass which “is a” version of its parent, an inner class' only application is within the scope of its parents class


Constant Interface: The use of an interface solely to define constants for classing implementing it.


Enumerations: Data type consisting of a set of named values enumerators of the type. The enumerator names are identifiers that behave as constants in the language and do not have any particular concrete representation in the computer's memory; compilers and interpreters can represent them arbitrarily.

Design Patterns

    _**Creational**_:  Involved with the process of object creation.

Factory Method: Decouples the client system from the actual implementation classes through the abstract types and factories. The Factory Method creates objects through inheritance.


Abstract Factory: Decouples the client system from the actual implementation classes through the abstract types and factories. The Abstract Factory creates objects through composition.


Singleton: Restricts instantiation of a class to one object. This became popular for providing global access for some service, limiting creation of objects, and tightly coupling classes. However, global variables are inefficient and vague in their usage. Singleton also violates the Single Responsibility Rule; the class can use a factory to limit creation and maintain encapsulation. The restrictive nature of the singleton also makes testing extremely difficult, and because of its persistent state it may cause false positives in unit testing. The singleton can easily be replaced by more effective means.


Builder: Separates the construction of a complex object from its representation so that the same construction process can create different representations. The Builder pattern is the creation of objects step by step. The Factory Method pattern defines an interface and let the subclasses decide which object to instantiate.


    _**Structural**_:  Deals with the composition of classes or objects.

Adapter (Wrapper): Classes that wrap up primitive values in classes that offer utility methods to manipulate the values. Once assigned a value, the value of a wrapper class cannot be changed.


Facade: This is a style of design pattern which analyzes the system at face value ignoring all of the inner working and processes (a single interface to multiple underlying objects).


Session Facade: A higher-level business component that contains and centralizes complex interactions between lower-level business components decoupling lower-level business components from one another, making designs more flexible and comprehensible. This provides clients with a single interface for the functionality of an application.


Flyweight: Use sharing to support large numbers of fine-grained objects efficiently. Designing objects down to the lowest levels of system “granularity” provides optimal flexibility, but can be unacceptably expensive in terms of performance and memory usage.

  • Intrinsic state: Sharable
  • Extrinsic state: Not sharable

Proxy: a system that acts as an intermediary for requests between clients and servers. Also a class functioning as an interface to something else.

Dynamic Proxies: A class that implements a list of interfaces specified at runtime such that a method invocation through one of the interfaces on an instance of the class will be encoded and dispatched to another object through a uniform interface.

Proxy Pattern: Software design pattern of the following types

  • Virtual: Used to perform lazy or just-in-time instantiation of the real object.
  • Remote: Used to mask or shield the client object from the fact that the underlying object is remote

    _**Behavioral**_:  Characterize the ways in which classes or objects interact and distribute responsibility.

Observer: Any object that wishes to be notified when the state of another object changes.

Observable: Any object whose state may be of interest, and in whom another object may register an interest.

Visitor: A way of separating an algorithm from an object structure on which it operates. A practical result of this separation is the ability to add new operations to existing object structures without modifying those structures.

Polymorphism

Polymorphism: The ability to create a variable, a function, or an object that has more than one form. This makes inheritance and code reuse possible.

Extensibilty: When a program can add new functionality by inheriting new data types from the common base class.


Upcasting: Permits an object of a subclass type to be treated as an object of any superclass type, done automatically.

Downcasting: Permits an object of a superclass type to be treated as an object of any subclass type, done manually.


Overriding: Providing a different implementation of a method in a subclass of the class that originally defined the method.


abstract: A Java keyword used in a class definition to specify that a class is not to be instantiated, but rather inherited by other classes. An abstract class can have abstract methods that are not implemented in the abstract class, but in subclasses.

Containers

Sort: Algorithm used to organize data.

Search: Algorithm used to find data.

Collection: The Java classes in the Collections Framework encapsulate both the data structures and the algorithms associated with these abstractions.

Generic: Add stability to your code by making more of your bugs detectable at compile time.

Hashing: Any algorithm or subroutine that maps large data sets, called keys, to smaller data sets.

Reference: A variable data type in which the variable's value is an address.


Primitive: A variable data type in which the variable's value is of the appropriate size and format for its type: a number, a character, or a boolean value.

  • boolean: True or false value.
  • int: An integer number value (others include byte, short, long, and double used for different number sizes).
  • float: Used for non integer numbers.
  • String: A character value.

Data Structures:

  • ArrayList: Resizable-array implementation of the List interface. The capacity is the size of the array used to store the elements in the list and is always at least as large as the list size.
  • LinkedList: Linked list implementation of the List interface. Each piece within the list recognizes the directly surrounding pieces.
  • Vector: Synchronized implementation of the List interface.
  • List Choice: The LinkedList is efficiently used with binary algorithms for sets of data that only need to be walked through once.
  • Tree: A widely-used data structure that emulates a hierarchical tree structure with a set of linked nodes.
  • Node: A record consisting of one or more fields that are links to other nodes; used to build linked, often hierarchical, data structures such as linked lists and trees.

Basic Parts of Code

Static: Used to define a variable as a class variable. Classes maintain one copy of class variables regardless of how many instances exist of that class. static can also be used to define a method as a class method. Class methods are invoked by the class instead of a specific instance, and can only operate on class variables.

Main(): A method in the controlling class used to initialize the program

Method: A piece of a class, used to implement actual pieces of code

Method Signatures: Collection of information about the method including name, visibility, type(static or not), its arguments, and return type

Constructor: Initializes the already constructed (created) object, called right after memory is allocated for the object. Has no return (not even void) and has same name as the class.


Assign: To throw away the old value of a variable and replace it with a new one

Initialize: A special kind of assignment: the first. Before initialization objects have null value and primitive types have default values such as 0 or false. Can be done in conjunction with declaration.

Declare: A declaration states the type of a variable, along with its name. A variable can be declared only once. It is used by the compiler to help programmers avoid mistakes such as assigning string values to integer variables. Before reading or assigning a variable, that variable must have been declared.


Delete: removes an object, reference, primitive, etc. from the memory

Free: same as “delete” without invocation of destructor, and cannot be overloaded

New: creates a object, reference, primitive, etc. adding it to the memory

malloc: same as “new” without invocation of constructor, and needs specification of size, and doesn't throw failure


Access Control Modifier: Determines the visibility of the class, as the following explains

(Where visible) Class Package Subclass World

Public: Y Y Y Y

Protected: Y Y Y N

Package: Y Y N N

Private: Y N N N

Local Variables: A variable that is given local scope and is accessible only from the function or block in which it is declared.

Global Variables: Contrasting local variables, these variables are accessible in everywhere in the project. These are not good to use because they can lead to shadowing and other confusion, as well as continuously allocating memory which is just wasteful.


Fields: A data member of a class. Unless specified otherwise, a field is not static.

Method Overloading: Using one identifier to refer to multiple items in the same scope. In the Java programming language, you can overload methods but not variables or operators.


this: A Java keyword that can be used to represent an instance of the class in which it appears. this can be used to access class variables and methods.

final: A Java keyword. You define an entity once and cannot change it or derive from it later. More specifically: a final class cannot be subclassed, a final method cannot be overridden and a final variable cannot change from its initialized value.


Packages: A group of types (class or interface), and are declared with the package keyword.

Libraries: A set of dynamically loadable libraries that Java applications can call at run time.

Classpath: An environmental variable which tells the Java virtual machine and Java technology-based applications where to find the class libraries, including user-defined class libraries.


Casting: Explicit conversion from one data type to another.

Argument: A data item specified in a method call. An argument can be a literal value, a variable, or an expression.

Block: A sequence of statements, local class declarations and local variable declaration statements within braces.

Error Handling

catch: A Java keyword used to declare a block of statements to be executed in the event that a Java exception, or run time error, occurs in a preceding try block.

try: A Java keyword that defines a block of statements that may throw a Java language exception. If an exception is thrown, an optional catch block can handle specific exceptions thrown within the try block. Also, an optional finally block will be executed regardless of whether an exception is thrown or not.

finally: A Java keyword that executes a block of statements regardless of whether a Java Exception, or run time error, occurred in a block defined previously by the try keyword.


Exception: The customary way in Java to indicate to a calling method that an abnormal condition has occurred. In Java exceptions are objects.

Exception Handler: A block of code that reacts to a specific type of exception. If the exception is for an error that the program can recover from, the program can resume executing after the exception handler has executed.

Exception Hierarchy: Only objects descending from the Throwable class may be thrown. Two direct descendants of this class are Exception and Error.


Global Exception Handling: Rather than sprinkling try/catch blocks throughout your applications they can all be managed at one location. The first step is to define your own UncaughtExceptionHandler implementation class. The second step is to set the default uncaught exception handler.

Controlling Flow

loop: Repeats a specified section in the code according to a state or predefined number set by the programmer.

if-else: An operation used to say if A happen then this happens, otherwise that happens.

for: A loop that has a set number of iterations set by the programmer.

while: A loop that continues to iterate over a section while a certain state or property exists.

do: A while loop based on a boolean condition.

switch: a certain kind of if statement that allows many choices to be chosen from rather than just two (if or else)

Equality

Equality: One way for primitives, four for objects

a==b, a!=b: Will only be true if two references point to the same underlying object.

equals(): Will return true if two objects have the same value.

compare(): For comparing values.

compareTo(): Compares values returning int less than, equal, or greater than.


Equivalence: When !(a


Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.