Tutorial by Examples: a

When desired XML format differs from Java object model, an XmlAdapter implementation can be used to transform model object into xml-format object and vice versa. This example demonstrates how to put a field's value into an attribute of an element with field's name. public class XmlAdapterExample { ...
"External" Storage is another type of storage that we can use to save files to the user's device. It has some key differences from "Internal" Storage, namely: It is not always available. In the case of a removable medium (SD card), the user can simply remove the storage. It i...
The getClass() method can be used to find the runtime class type of an object. See the example below: public class User { private long userID; private String name; public User(long userID, String name) { this.userID = userID; this.name = name; } } pu...
// Obtain an instance of JavaScript engine ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("nashorn"); // Define a global variable engine.put("textToPrint", "Data defined in Java."); // Print the global var...
// Obtain an instance of JavaScript engine ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("nashorn"); // Execute an hardcoded script try { engine.eval("print('Hello Nashorn!');"); } catch (ScriptException ex) { ...
// Required imports import javax.script.ScriptEngineManager; import javax.script.ScriptEngine; import javax.script.ScriptException; import java.io.FileReader; import java.io.FileNotFoundException; // Obtain an instance of the JavaScript engine ScriptEngineManager manager = new ScriptEngineM...
A Stream will only be traversed when there is a terminal operation, like count(), collect() or forEach(). Otherwise, no operation on the Stream will be performed. In the following example, no terminal operation is added to the Stream, so the filter() operation will not be invoked and no output will...
Static and member methods in Java can be marked as native to indicate that their implementation is to be found in a shared library file. Upon execution of a native method, the JVM looks for a corresponding function in loaded libraries (see Loading native libraries), using a simple name mangling sche...
Calling a Java method from native code is a two-step process : obtain a method pointer with the GetMethodID JNI function, using the method name and descriptor ; call one of the Call*Method functions listed here. Java code /*** com.example.jni.JNIJavaCallback.java ***/ package com.example....
Add the dependency as described in the Remark section, then add a RecyclerView to your layout: <android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content"/> ...
A char can store a single 16-bit Unicode character. A character literal is enclosed in single quotes char myChar = 'u'; char myChar2 = '5'; char myChar3 = 65; // myChar3 == 'A' It has a minimum value of \u0000 (0 in the decimal representation, also called the null character) and a maximum valu...
This Activity code will provide basic functionality for including a Google Map using a SupportMapFragment. The Google Maps V2 API includes an all-new way to load maps. Activities now have to implement the OnMapReadyCallBack interface, which comes with a onMapReady() method override that is execute...
Map Style Google Maps come with a set of different styles to be applied, using this code : // Sets the map type to be "hybrid" map.setMapType(GoogleMap.MAP_TYPE_HYBRID); The different map styles are : Normal map.setMapType(GoogleMap.MAP_TYPE_NORMAL); Typical road map. Roads, s...
To add markers to a Google Map, for example from an ArrayList of MyLocation Objects, we can do it this way. The MyLocation holder class: public class MyLocation { LatLng latLng; String title; String snippet; } Here is a method that would take a list of MyLocation Objects and place a M...
The shared remote interface: package remote; import java.rmi.Remote; import java.rmi.RemoteException; public interface RemoteServer extends Remote { int stringToInt(String string) throws RemoteException; } The server implementing the shared remote interface: package server; im...
You should be careful when comparing floating-point values (float or double) using relational operators: ==, !=, < and so on. These operators give results according to the binary representations of the floating point values. For example: public class CompareTest { public static void main...
Classes implementing Iterable<> interface can be used in for loops. This is actually only syntactic sugar for getting an iterator from the object and using it to get all elements sequentially; it makes code clearer, faster to write end less error-prone. public class UsingIterable { pub...
Button button = new Button("I'm here..."); Timeline t = new Timeline( new KeyFrame(Duration.seconds(0), new KeyValue(button.translateXProperty(), 0)), new KeyFrame(Duration.seconds(2), new KeyValue(button.translateXProperty(), 80)) ); t.setAutoReverse(true); t.setCy...
Annotation @XmlAccessorType determines whether fields/properties will be automatically serialized to XML. Note, that field and method annotations @XmlElement, @XmlAttribute or @XmlTransient take precedence over the default settings. public class XmlAccessTypeExample { @XmlAccessorType(XmlAccessT...
Annotations @XmlElement, @XmlAttribute or @XmlTransient and other in package javax.xml.bind.annotation allow the programmer to specify which and how marked fields or properties should be serialized. @XmlAccessorType(XmlAccessType.NONE) // we want no automatic field/property marshalling public cla...

Page 20 of 1099