Tutorial by Examples: e

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 primitive data type such as int holds values directly into the variable that is using it, meanwhile a variable that was declared using Integer holds a reference to the value. According to java API: "The Integer class wraps a value of the primitive type int in an object. An object of type Int...
A short is a 16-bit signed integer. It has a minimum value of -215 (-32,768), and a maximum value of 215 ‑1 (32,767) short example = -48; short myShort = 987; short anotherShort = 17; short addedShorts = (short) (myShort + anotherShort); // 1,004 short subtractedShorts = (short) (myShort - an...
By default, long is a 64-bit signed integer (in Java 8, it can be either signed or unsigned). Signed, it can store a minimum value of -263, and a maximum value of 263 - 1, and unsigned it can store a minimum value of 0 and a maximum value of 264 - 1 long example = -42; long myLong = 284; long ano...
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 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!"); } }); ...
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...
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 ...
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...

Page 19 of 1191