Kode Java – Learn Java by Examples
Learn Java by Examples
- Website
- kodejava.org · all feeds from this site
- Feed URL
https://kodejava.org/feed/
Read it in your FeedListing reader
Or add to: Feedly Inoreader Raw feed
Latest posts
-
How do I animate a fractal tree in Java Swing?
You can draw a fractal tree by creating a branch and recursively drawing two shorter branches at its endpoint. Repeating this rule produces an intricate tree from a small amount of code. In this SwingLoops example, the branches grow over time, sway gently, and fade away before…
-
How do I sort a list of objects by a field in Java?
You can use Comparator.comparing() to sort objects by a field exposed through an accessor method. In this example, you will sort a list of books alphabetically by title. Source code Save the following code as SortObjectsExample.java: import java.util.ArrayList; import…
-
How do I create, load, and display an image in Java?
In this example, you will draw a blue card in memory, save it as a PNG file, open that file, and show the loaded image in a Swing window. The program creates its own image. What is a pixel? A raster image is a grid of pixels. In a BufferedImage, the top-left pixel is at […]
-
How do I get the first and last elements of a List in Java?
You can use getFirst() and getLast() to read the first and last elements of a list without calculating their indexes. In this example, you will read the first and last programming language names from a list. Source code Save the following code as FirstLastListExample.java:…
-
How do I keep a recent files list with SequencedMap in Java 21?
A recent files menu should move a reopened file to the top instead of showing it twice. Once the menu is full, opening a new file should remove the oldest entry. In Java 21, a LinkedHashMap can do both through the SequencedMap interface. See the Java 21 SequencedMap API. 1. Move…
-
How do I build a string using StringBuilder in Java?
You can use StringBuilder to build text by appending values to a mutable sequence of characters. In this example, you will combine a book’s title, author, and publication year into one string. Source code Save the following code as StringBuilderExample.java: public class…
-
How do I build my first Android App with Kotlin and Jetpack Compose?
In this first article of the Android with Kotlin and Jetpack Compose series, you will create Task Notes, a small app for keeping track of daily tasks. Today you will run the starter project and build the screen users see when their task list is empty. You will reuse this project…
-
How do I animate wave interference in Java Swing?
Drop two pebbles into water and the ripples overlap. Where two crests meet, they reinforce each other; where a crest meets an equally strong trough, they cancel. In this SwingLoops example, we use two sinusoidal waves to recreate that pattern pixel by pixel. The result is a…
-
How do I split a string into words in Java?
You can use the split() method to divide a string into smaller parts. The method accepts a regular expression that defines where to split and returns an array of strings. In this example, you will split a sentence into words using whitespace as the separator. Source code Save…
-
How do I get started with 2D game programming in Java?
Game programming is a fun way to learn Java because it combines programming concepts with visual results. Instead of writing programs that only print text to the console, we can create characters, draw objects, and eventually build interactive games. In this tutorial, we will…
-
How do I use a method reference in Java?
A method reference lets you use an existing method as the implementation of a functional interface. It can make a lambda expression shorter when the lambda only calls that method. In this example, you will use a method reference to print each language in a list. Source code Save…
-
What Is Hibernate ORM?
When you use JDBC to read a row from a database, you normally write a SQL query, execute it, and copy the values from the ResultSet into a Java object. If your application has classes such as Book, Author, and Publisher, you repeat this kind of mapping for each class. Hibernate…
-
How do I calculate a moving average with Stream Gatherers in Java 24?
A moving average smooths out short-term changes in a series of readings. Instead of averaging all the readings once, you average the first few readings, move one position forward, and repeat. In this example, you’ll calculate an average for every three consecutive temperature…
-
How do I convert byte arrays to hexadecimal and back in Java 17?
When you inspect binary data, a hexadecimal string can be easier to read than a list of decimal byte values. You might use it when troubleshooting a file format or checking a message exchanged between applications. This tutorial shows a complete conversion in both directions,…
-
How do I write a lambda expression in Java?
A lambda expression provides a concise implementation of a functional interface. A functional interface has one abstract method. In this example, you will use a lambda expression to greet someone by name. Source code Save the following code as LambdaExample.java: public class…
-
How do I morph text with particles in Java Swing?
You can turn text into a particle animation by drawing each word into a transparent image and treating its visible pixels as destinations. In this example, 840 glowing dots form JAVA, change into SwingLoops, and return to the original cloud. Everything is drawn with Java Swing…
-
How do I create a generic method in Java?
A generic method uses a type parameter to work with different reference types. This lets you reuse the same method without writing a separate version for each type. In this example, you will create a method that prints the elements of an array. The same method will handle arrays…
-
How do I create interactive fireworks using Java Swing?
You can create interactive fireworks in Java Swing by combining Graphics2D, mouse events, and a javax.swing.Timer. Rockets travel toward the position you click, then explode into colorful particles affected by gravity, damping, and fading. The effect does not require an external…
-
How do I create a custom exception in Java?
You can create a custom exception to describe a problem specific to your application. Give it a meaningful name so calling code can recognize and handle the problem. In this example, you will create an InvalidAgeException and throw it when someone under 18 tries to register.…
-
How do I implement the Factory Method pattern in Java?
The Factory Method pattern is a Gang of Four (GoF) creational design pattern. It defines an operation for creating a product while allowing subclasses to choose the concrete product returned. This lets a base class reuse a workflow without hard-coding the product implementation…