Category java

Generics

Since the original 1.0 release in 1995, many new features have been added to Java. One that has had a profound impact is generics. Introduced by JDK 5, generics changed Java in two important ways. First, it added a new syntactical element…

Enumerations, Autoboxing and Static Import

Enumerations The identifiers HEART, SPADE, CLUB and DIAMOND are called enumeration constants. Each is implicitly declared as a public, static, final member of Suit. Furthermore, their type is the type of the enumeration in which they are declared, which is…

Multithreaded Programming

Multithreaded Fundamentals Process vs Thread Thread States The Thread Class and Runnable Interface Java’s multithreading system is built upon the Thread class, its methods, and its companion interface, Runnable. Thread encapsulates a thread of execution. Since you can’t directly refer to the ethereal state…

Using I/O

Java’s I/O is Built upon Streams Byte Streams and Character Streams The Byte Stream Classes The Character Stream Classes The Predefined Streams Using the Byte Streams Let’s explore FileInputStream and FileOutputStream by examining an example program named ByteStreamExample, which uses byte streams to copy test.txt, one…

Exception Handling

Exceptions are a mechanism used by many programming languages to describe what to do when something unexpected happens.  Typically, something unexpected is an error of some sort, for example when a method is invoked with unacceptable arguments, or a network…

Packages and Interfaces

Packages package <top_pkg_name>[.<sub_pkg_name>];  Packages and Member Access Understanding Protected Members private default protected private same class Y Y Y Y subclass same package N Y Y Y non-subclass same package N Y Y Y subclass different package N N Y…

Inheritance

Inheritance Basics Member Access and Inheritance Although a subclass includes all of the members of its superclass, it cannot access those members of the superclass that have been declared as private.  This program will not compile because the use of j inside…

Advanced OOP Topics

Controlling Access to Class Members Modifier Class Package Subclass World public Y Y Y Y protected Y Y Y N no modifier Y Y N N private Y N N N Tips on Choosing an Access Level Encapsulation Properly encapsulated…

More Data Types and Operators

Arrays One-Dimensional Arrays Multi-Dimensional Array Jagged Array The array created by this program looks like this:  Alternative Array Declaration Syntax Assigning Array References Using the length Member The foreach Style For Loop String String Declarations There are two ways to…