Java Programming Examples
Algorithms
Java Basics
Game of Cards
Card.java | Deck.java | TestDeck.java |
Clean Code
GeneratePrimes.java | PrimeGenerator.java |
Object Cloning
B.java | CloningTest.java | MyObject.java |
Collections, Sets and Maps
Anagrams.java | Freq.java | HashCodeTest.java |
MapTest.java | SetTest.java | dictionary.txt |
Books and Library examples (part of Collections section)
Collections, HashSet Implementation in TDD style
HashSet.java | HashSetTest.java | Set.java |
Implementation of interfaces using generic algorithms
(“generic” is understood here in the sense of A. Stepanov’s STL: independent of concrete data structures, using only iterator)
ArrayIterator.java | BadOperation.java | Book.java |
Iterator.java | Library.java | ListIterator.java |
ListWithArray.java | ListWithLL.java | MyList.java |
MyUtilities.java | Node.java |
Default Methods in Interfaces
A.java | B.java | C.java |
Diamond of Death no more for default methods
A.java | B.java | C.java |
InhTest.java |
Default methods after an (unknown) upgrade of a third-party API
(based on Rafael Winterhalter Java 8 default methods can break your code)
ComplexInput.java | MyInput.java | SimpleInput.java |
ThirdPartyClass.java |
Default methods before an upgrade of a third-party API
(based on Rafael Winterhalter Java 8 default methods can break your code)
MyInput.java | SimpleInput.java | ThirdPartyClass.java |
Step-wise program development
FindRoots.java | FindRootsWithLambda.java |
Enumerated Types (enums)
Chess.java | ChessTest.java | Coins.java |
EnumValues.java | Planets.java | VarOpsEnum.java |
Bad enumeration “types” which rely on interface constants
Apples.java | BadEnums.java | Devious.java |
Fruits.java | Juices.java | Oranges.java |
Overriding equals()
to get equality right
A.java | TestingEquals.java |
Exceptions and Errors
A small program which catches exception inside a loop
PointList.java | PointTest.java | XYPoint.java |
Functional Programming
(will be added soon)
—|—|—
Generics (Parameterised Types)
Box.java | BoxBT.java | BoxDemo1.java |
BoxDemo2.java | BoxG.java | BoxWC.java |
Cell.java | ClassTest.java | GenMethods.java |
NewGenerics.java | OldGenerics.java | OldGenerics1.java |
Inheritance
Animal.java | Cat.java | ExtendShow.java |
InheritanceTest.java | More.java | Point.java |
SuperShow.java | Test.java | That.java |
Static methods under inheritance
(hiding, not overriding)
A.java | B.java | C.java |
Input/Output
New Input/Output API
ReadInFile.java |
A sample program from Oracle’s offcial example set
(works with files stored inside a zip or jar file)
Demo.java | README.txt | src.zip |
zipfs.jar |
Generating Documentation with javadoc
(unzip the clock_doc.zip file and follow the instructions in README)
clock_doc.zip |
UI and Graphics programming with JavaFX
JavaFX animation examples (from the official Oracle’s set)
Animation.java | AnimationBooleanInterpolator.java | AnimationInterpolator.java |
TimelineEvents.java |
Styled JavaFX application
Clock.java | clock.css |
Basic and not so basic examples of JavaFX binding
BindingDemo.java | BindingDemo2.java | BindingDemo3.java |
Counter application with command-line, Swing and JavaFX clients
CommandLineCounter.java | Counter.java | JavaFXCounter.java |
SwingCounter.java |
Event filtering example in JavaFX
(black art, don’t try this at home)
DraggablePanelsExample.java |
Testing with JUnit
Complex.java | Point.java | TestComplex.java |
TestPoint.java |
Old testing (without JUnit framework) technique
Point.java |
Lambda expressions and method references
Miscellaneous examples
Example.java | Foo.java | NonPureMethod.java |
Parameter.java | Planet.java | RegexTestHarness.java |
Reuse.java | ScannerExample.java | ScopeTest.java |
StaticParameter.java | StringBusiness.java | StringExample.java |
Module System: module declaration, access control etc
Entropy.java | README |
Object-oriented programming
Covariant Overriding examples
Main.java | MyComplexNumber.java | MyNumber.java |
Package structure and access
Alongsider.java | Example.java | Foo.java |
Makefile | Protected.java | README |
SamePackageDweller.java | SubProtected.java |
Polymorphism examples
Circle.java | Main.java | Point.java |
Shape.java | Square.java |
Reflections
A.java | B.java | ClassBrowser.java |
ClassSpy.java | Foo.java | HelloWorld.java |
ListParams.java | README | RunTests.java |
SubHelloWorld.java | Test.java | TestHelloWorld.java |
Splotch JavaFX example
(run make compile
to compile, make run
to run)
Makefile | splotch.zip |
Sources for Splotch program
(keep them inside splotch subdirectory)
Point.java | Smoother.java | Splotch.java |
Streams examples
CaesarCypher.java | Dish.java | LazyEagerBenchmark.java |
LazyStreams.java | ReductionExamples.java | StreamBasic.java |
StreamFactoring.java | StreamVsCollection.java | String2Chars.java |
Swing GUI programming examples, obsolete
(to run the applet, compile HelloWorldApplet.java
and open
appletviewer HelloWorld.html
; running in a browser is likely
disabled nowadays)
Counter.java | HelloWorld.html | HelloWorldApplet.java |
MouseSpy.java | TestCounter.java | clock1.jar |
clock3.jar |
“Classic” Swing application with MVC architecture
AnalogClockPanel.java | Clock.java | Controller.java |
Model.java | View.java |
Counter application of a certain (naive) kind
Counter.java | CounterApplication.java | README.txt |
Counter application of another (naive) kind
Counter.java | CounterApplication.java | IncrementButtonHandler.java |
README.txt | ResetButtonHandler.java |
Counter application of a yet another (naive) kind
Counter.java | CounterApplication.java | IncrementButtonHandler.java |
README.txt | ResetButtonHandler.java |
Counter application with some adornments
Counter.java | CounterApplication.java | IncrementButtonHandler.java |
README.txt | ResetButtonHandler.java | ResetItemHandler.java |
Threads examples
HelloRunnable.java | HelloThread.java | PingPong.java |
SimpleThreads.java | SleepMessages.java |
Trees and tries
(will be added later)
—|—|—