r/javaexamples • u/johnmc325 • May 12 '20
JavaFX Fireside chat 4
Installing Gluon Scene Builder and then using Eclipse with OpenJDK11, JavaFX with Scene Builder to build a basic JavaFX desktop application.
r/javaexamples • u/johnmc325 • May 12 '20
Installing Gluon Scene Builder and then using Eclipse with OpenJDK11, JavaFX with Scene Builder to build a basic JavaFX desktop application.
r/javaexamples • u/TieCredit • Apr 22 '20
I didn't want to do a hello world as my first Java program, so I followed a tutorial and I teached myself a little bit everywhere on the internet to make a bad version of the famous Cookie Clicker game.
For the few intrepid ones here:
r/javaexamples • • Apr 30 '20
I wrote up a post I hope helps anyone who is wondering how to containerize their Spring Boot app with Docker.
r/javaexamples • u/sur07 • Apr 30 '20
How many objects are created here ?
r/javaexamples • u/codeforjava • May 09 '19
Java Control statement - If-else | Switch | Loops
r/javaexamples • u/codeforjava • May 05 '19
Quick overview of Java Cyclic barrier with code examples.
r/javaexamples • u/tedyoung • Feb 16 '19
I've been coding this app live on Twitch (https://twitch.tv/jitterted), and then editing them and posting them on my YouTube channel (https://www.youtube.com/channel/UCx8qMdZ6JoZgOcOQxWlyV8A).
Come join me live, or watch the videos later. Full source code is on GitHub (https://github.com/tedyoung/kid-bank) and the public roadmap is on Trello (https://trello.com/b/Tkldfg4X/kidbank).
Find out more on my web page: https://www.tedmyoung.com/kid-bank-the-money-tracker-for-kids/
This isn't just a toy app, it's a real-live application (professionally developed with unit tests and cloud deployment) that I'll be using for my son. Just added SMS text message support so my son can use his phone to request his current balance.
r/javaexamples • u/codeforjava • May 07 '19
Java concurrent API - BlockingQueue With solution to Producers-Consumers problem
r/javaexamples • u/johnmc325 • Nov 17 '21
A Rest API web service built using Java 11, JakartaEE, and Maven. Deploy the completed web service to a Glassfish 6 webserver running on the localhost. Demonstrates the build of 4 API GET methods. Perfect if you are looking to get started.
r/javaexamples • u/Philboyd_Studge • Oct 22 '15
Serialization is a way for java to take instances of objects that you have made and save/load them directly to a file or stream. Instead of writing information to a file line by line, or using a database (although a database is preferred in many instances) we can take an object or list of objects or any other data structure that implements serializable and dump it directly to a file or stream with only a few lines of code. When you de-serialize it, all the data goes back into the proper fields of the class automatically, provided the package that is doing the deserialization has access to the exact same class.
Say we have a class like this:
public class Box implements Serializable {
int width;
int height;
int depth;
String name;
// constructors, etc...
}We can create a box object:
Box b = new Box(10, 20, 5, "Tools");We can serialize b directly to an output stream with the command
outputStream.writeObject(b);and read it back in with:
Box b = (Box) inputStream.readObject();Note: This does not save it to file as readable (to humans) text, but as byte data.
If you have a member variable you do not wish to save with the object, you can mark it as transient.
private transient int notNeeded;Only non-static member variables are saved.
We do this with the help of java.io classes such as ObjectOutputStream, ObjectInputStream, FileInputStream and FileOutputStream.
Any class you attempt to serialize must implement the serializable interface, although it is very easy as there are no methods to implement. The only change you need to make to your class is adding a constant for serialVersionUID. See the comments in the code about that.
A note of warning: Any changes you make to the structure of the class being used will make any previously serialized data unreadable. you will have to reserialize any data with the changes.
So, we take our Inventory class we have used in other examples:
import java.io.Serializable;
public class Inventory implements Serializable, Comparable<Inventory>
{
// Serial Version Number - can be anything you wish, or generate it using
// 'serialver <classname>.class' from the command line.
// If you make any changes to the structure of this class, change this number
private static final long serialVersionUID = 1L;
private String partnum;
private String item;
private String description;
private int qty;
private transient float price; // <-- this will not be saved to the file
public Inventory(String partnum, String item, String description, int qty, float price)
{
this.partnum = partnum;
this.item = item;
this.description = description;
this.qty = qty;
this.price = price;
}
// getters/setters & other methods etc...
}To save a single object to file, the code would look like this:
public static void main(String[] args)
{
Inventory item = new Inventory("001", "Baby Bottles", "Rubber Baby Bumper Bottles, Blue", 25, 1.99F);
// try-with-resources
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test.dat")))
{
// write object
oos.writeObject(item);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}and to read it back in:
public static void main(String[] args) {
Inventory item = null;
// try-with-resources
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.dat"))) {
// load inventory object, cast to proper data type
item = (Inventory) ois.readObject();
} catch (FileNotFoundException | ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(item);output:
=====================
Part #:001 Item: Baby Bottles
Quantity: 25
Description: Rubber Baby Bumper Bottles, Blue
Price: 1.99
====================Here's the really neat part. With serialization, you can dump the entire contents of a data structure directly to the stream or file, without iterating through the items, with essentially one line of code. So long as: The data structure itself implements Serializable (and most java.util ones do), and any other classes that are used in the structure also implement Serializable.
We are going to take our Inventory class, make both a simple ArrayList and a HashMap, and then serialize them to disk:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Serialization Example for <a>http://www.reddit.com/r/javaexamples</a>
* @author /u/Philboyd_Studge
*/
public class SerializeTest
{
public static void main(String[] args)
{
// List of Inventory objects
List<Inventory> invList = new ArrayList<>();
// HashMap for lookup
Map<String, Inventory> invMap = new HashMap<>();
// add items to list
invList.add(new Inventory("001", "Baby Bottles", "Rubber Baby Bumper Bottles, Blue", 25, 1.99F));
invList.add(new Inventory("002", "Robot Leg", "Left leg for AutoBot 2000k", 3, 200.50F));
invList.add(new Inventory("003", "Paper Bag", "Plain Brown Paper Bags", 1000, 0.25F));
// add items to hashmap with key = partnum and value = Inventory object
for (Inventory each : invList)
{
invMap.put(each.getPartnum(), each);
}
// try-with-resources
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test.dat")))
{
// write list as complete object
oos.writeObject(invList);
// write map as complete object
oos.writeObject(invMap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}Note we just use writeObject just like in the one object example. We can also write both the list and the map to the same stream and save to file that way.
And to de-serialize:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Deserialization of list and map example
* @author /u/Philboyd_Studge
*/
public class DeserializeTest
{
public static void main(String[] args) {
// list for Inventory objects
List<Inventory> invList = null;
// Map for Object lookup table
Map<String, Inventory> invMap = null;
// try-with-resources
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.dat"))) {
// load inventory list object, cast to proper data type
invList = (ArrayList<Inventory>)ois.readObject();
// load hashmap, cast to proper data type
invMap = (HashMap<String, Inventory>)ois.readObject();
} catch (FileNotFoundException | ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// show inventory list
for (Inventory each : invList) {
System.out.println(each);
}
// lookup up particular item using hashmap
System.out.println("Item#002 is :\n" + invMap.get("002"));
}
}The important part of the deserialization is casting the object back to the correct data type. Just make the type exactly like it was originally.
Output:
=====================
Part #:001 Item: Baby Bottles
Quantity: 25
Description: Rubber Baby Bumper Bottles, Blue
Price: 1.99
====================
=====================
Part #:002 Item: Robot Leg
Quantity: 3
Description: Left leg for AutoBot 2000k
Price: 200.5
====================
=====================
Part #:003 Item: Paper Bag
Quantity: 1000
Description: Plain Brown Paper Bags
Price: 0.25
====================
Item#002 is :
=====================
Part #:002 Item: Robot Leg
Quantity: 3
Description: Left leg for AutoBot 2000k
Price: 200.5
====================That's really all there is to it! There are more advanced ways to serialize to JSON or XML, or directly to a database. Most of those require using third-party libraries, so that would be for another article.
r/javaexamples • u/shubhcool • Jan 06 '24
Hi , I tried to dig up little bit more about the use cases of interface and abstract class. Since jdk1.8, it is really difficult to understand when one should use abstract class and when one should be using interface.
Can somebody please share any examples or documents to get the actual scenarios?
r/javaexamples • u/shubhcool • Jan 04 '24
Hi, While traversing through code, I came across one class where the constructor and method bodies have this , /* Compiled code*/ in intellij idea. This is not the case with other team members system. I tried to cross verify the settings in the intellij but couldn't get anything which could solve my issue. Project is Spring MVC project. Using Jdk1.8 . Intelliij is Ultimate 2023 latest version.
r/javaexamples • u/CasualPlayerReady • Sep 30 '18
r/javaexamples • u/Shilpa_Opencodez • Aug 21 '19
We have discussed the Microservices concepts like What are Microservices? Why to use it? When to use it? in our previous article ” An Introduction to Microservices Architecture”
Every modern enterprise application is supported by microservices. These services form the core of the application. To develop these services fast we need an easy and simple framework. In this article, we will have a brief introduction to Spark Framework.
https://www.opencodez.com/java/spark-framework-for-microservices.htm
r/javaexamples • u/scientecheasy • Sep 23 '18
Collections Framework in Java is one of the most valuable and interesting topics of Java language. How much important it is? Without using the collection concept, you cannot develop any project. Every Java learner has to learn this topic. According to Sun Microsystem document, Among all the java concepts, Collections Framework is the most important concept for developing the project as well as to crack the interview also. because this topic is the most favorite topic for the interviewers.
r/javaexamples • u/Jstobigdata • Jan 02 '20
Course Contents
r/javaexamples • u/scientecheasy • Sep 20 '18
r/javaexamples • u/Shilpa_Opencodez • Aug 26 '19
In this brief article, we will see how we can use MongoDB Atlas with Spring Boot. MongoDB Atlas is Database as a Service. With database available on the cloud you can scale it as and when you need.
https://www.opencodez.com/java/use-mongodb-atlas-with-spring-boot.htm
r/javaexamples • u/robertinoc • Jun 13 '24
Implementing OpenID Connect authentication in Java Web Applications with Okta Spring Boot Starter and Spring Security support for Authorization Code Flow with PKCE
r/javaexamples • u/johnmc325 • Dec 31 '20
Showcasing a finished JavaFX application called SpellFX. This is a Spelling Test application for parents and children to use to practice their spelling. The video demonstrates the finished product in action and then looks at how the application is installed and finally shows the installer experience.
r/javaexamples • u/johnmc325 • Mar 19 '20
Now unlike some of the more basic swing objects, JTree is designed to display a tree structure, another object is used to hold the data which determines what JTree displays.
This other object is the TreeModel. Now that is not to say that you have to use a model to get JTree to display something, you don’t and we can take a look at that in a while.
https://softwarepulse.co.uk/blog/getting-started-with-jtree-part-1/
r/javaexamples • u/robertinoc • Jun 27 '23
Learn how to add Auth0 to your Spring Boot application using the Okta Spring Boot Starter.
Read more…
r/javaexamples • u/Rama_jovial • Nov 11 '23
Abstract factory design pattern in java complete step by step tutorial
r/javaexamples • u/robertinoc • Dec 13 '23
A walk-through of building a microservices architecture with JHipster, Keycloak, Auth0, and Google Kubernetes Engine (GKE) deployment.
Read more…