Tutorial by Examples: a

TL;DR == tests for reference equality (whether they are the same object) .equals() tests for value equality (whether they are logically "equal") equals() is a method used to compare two objects for equality. The default implementation of the equals() method in the Object class returns...
import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class User { private long userID; private String name; // getters and setters } By using the annotation XMLRootElement, we can mark a class as a root element of an XML file. import java.io.File; ...
To read an XML file named UserDetails.xml with the below content <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <user> <name>Jon Skeet</name> <userID>8884321</userID> </user> We need a POJO class named U...
Arrays are objects which provide space to store up to its size of elements of specified type. An array's size can not be modified after the array is created. int[] arr1 = new int[0]; int[] arr2 = new int[2]; int[] arr3 = new int[]{1, 2, 3, 4}; int[] arr4 = {1, 2, 3, 4, 5, 6, 7}; int len1 = ar...
A boolean can store one of two values, either true or false boolean foo = true; System.out.println("foo = " + foo); // foo = true boolean bar = false; System.out.println("bar = " + bar); // bar = false boolean notFoo = !foo; System.out.prin...
Server: Start, and wait for incoming connections //Open a listening "ServerSocket" on port 1234. ServerSocket serverSocket = new ServerSocket(1234); while (true) { // Wait for a client connection. // Once a client connected, we get a "Socket" object // that c...
A float is a single-precision 32-bit IEEE 754 floating point number. By default, decimals are interpreted as doubles. To create a float, simply append an f to the decimal literal. double doubleExample = 0.5; // without 'f' after digits = double float floatExample = 0.5f; // with 'f' aft...
By default, any files that you save to Internal Storage are private to your application. They cannot be accessed by other applications, nor the user under normal circumstances. These files are deleted when the user uninstalls the application. To Write Text to a File String fileName= "hellowor...
Buttons fire action events when they are activated (e.g. clicked, a keybinding for the button is pressed, ...). button.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Hello World!"); } }); ...
Buttons can have a graphic. graphic can be any JavaFX node, like a ProgressBar button.setGraphic(new ProgressBar(-1)); An ImageView button.setGraphic(new ImageView("images/icon.png")); Or even another button button.setGraphic(new Button("Nested button"));
Don't just use Optional.get() since that may throw NoSuchElementException. The Optional.orElse(T) and Optional.orElseGet(Supplier<? extends T>) methods provide a way to supply a default value in case the Optional is empty. String value = "something"; return Optional.ofNullable(v...

Map

Use the map() method of Optional to work with values that might be null without doing explicit null checks: (Note that the map() and filter() operations are evaluated immediately, unlike their Stream counterparts which are only evaluated upon a terminal operation.) Syntax: public <U> Option...
Use the orElseThrow() method of Optional to get the contained value or throw an exception, if it hasn't been set. This is similar to calling get(), except that it allows for arbitrary exception types. The method takes a supplier that must return the exception to be thrown. In the first example, the...
When a Java class overrides the equals method, it should override the hashCode method as well. As defined in the method's contract: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, ...
By default, WebView does not implement JavaScript alert dialogs, ie. alert() will do nothing. In order to make you need to firstly enable JavaScript (obviously..), and then set a WebChromeClient to handle requests for alert dialogs from the page: webView.setWebChromeClient(new WebChromeClient() { ...
Once we've gotten the Connection, we will mostly use it to create Statement objects. Statements represent a single SQL transaction; they are used to execute a query, and retrieve the results (if any). Let's look at some examples: public void useConnection() throws SQLException{ Connection ...
OptionalDouble, OptionalInt and OptionalLong work like Optional, but are specifically designed to wrap primitive types: OptionalInt presentInt = OptionalInt.of(value); OptionalInt absentInt = OptionalInt.empty(); Because numeric types do have a value, there is no special handling for null. Empt...
Enum can be considered to be syntax sugar for a sealed class that is instantiated only a number of times known at compile-time to define a set of constants. A simple enum to list the different seasons would be declared as follows: public enum Season { WINTER, SPRING, SUMMER, FA...
An enum can contain a method, just like any class. To see how this works, we'll declare an enum like this: public enum Direction { NORTH, SOUTH, EAST, WEST; } Let's have a method that returns the enum in the opposite direction: public enum Direction { NORTH, SOUTH, EAST, WEST; ...
String getText(String url) throws IOException { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); //add headers to the connection, or check the status if desired.. // handle error response code it occurs int responseCode = conn.getResponseCod...

Page 18 of 1099