Tutorial by Examples

A byte is a 8-bit signed integer. It can store a minimum value of -27 (-128), and a maximum value of 27 - 1 (127) byte example = -36; byte myByte = 96; byte anotherByte = 7; byte addedBytes = (byte) (myByte + anotherByte); // 103 byte subtractedBytes = (byte) (myBytes - anotherByte); // 89 ...
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...
A double is a double-precision 64-bit IEEE 754 floating point number. double example = -7162.37; double myDouble = 974.21; double anotherDouble = 658.7; double addedDoubles = myDouble + anotherDouble; // 315.51 double subtractedDoubles = myDouble - anotherDouble; // 1632.91 double scientif...
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() { ...
Since Java 6, the recommended way to access an SQL-based database in Java is via the JDBC(Java DataBase Connectivity) API. This API comes in two packagages: java.sql and javax.sql. JDBC defines database interactions in terms of Connections and Drivers. A Driver interacts with the database, and pr...
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 ...
filter() is used to indicate that you would like the value only if it matches your predicate. Think of it like if (!somePredicate(x)) { x = null; }. Code examples: String value = null; Optional.ofNullable(value) // nothing .filter(x -> x.equals("cool string"))// this is nev...
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 cannot have a public constructor; however, private constructors are acceptable (constructors for enums are package-private by default): public enum Coin { PENNY(1), NICKEL(5), DIME(10), QUARTER(25); // usual names for US coins // note that the above parentheses and the constructor...
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...
The Standard Edition of Java comes with some annotations predefined. You do not need to define them by yourself and you can use them immediately. They allow the compiler to enable some fundamental checking of methods, classes and code. @Override This annotation applies to a method and says that th...

Page 22 of 1336