Tutorial by Examples: el

Addition Map<Integer, String> map = new HashMap<>(); map.put(1, "First element."); System.out.println(map.get(1)); Output: First element. Override Map<Integer, String> map = new HashMap<>(); map.put(1, "First element."); map.put(1, &quot...
When we create a button in layout, we can use the android:onClick attribute to reference a method in code to handle clicks. Button <Button android:width="120dp" android:height="wrap_content" android:text="Click me" android:onClick="handleCl...
Gradle (Module:app) Configuration android { .... dataBinding { enabled = true } } Data model public class Item { public String name; public String description; public Item(String name, String description) { this.name = name; this.descr...
if (i < 2) { System.out.println("i is less than 2"); } else if (i > 2) { System.out.println("i is more than 2"); } else { System.out.println("i is not less than 2, and not more than 2"); } The if block will only run when i is 1 or less. The else if...
int i = 0; while (i < 100) { // condition gets checked BEFORE the loop body executes System.out.println(i); i++; } A while loop runs as long as the condition inside the parentheses is true. This is also called the "pre-test loop" structure because the conditional stateme...
The do...while loop differs from other loops in that it is guaranteed to execute at least once. It is also called the "post-test loop" structure because the conditional statement is performed after the main loop body. int i = 0; do { i++; System.out.println(i); } while (i &l...
int i = 2; if (i < 2) { System.out.println("i is less than 2"); } else { System.out.println("i is greater than 2"); } An if statement executes code conditionally depending on the result of the condition in parentheses. When condition in parentheses is true it will ...
It is helpful to filter the logcat output because there are many messages which are not of interest. To filter the output, open the "Android Monitor" and click on the drop down on the top-right and select Edit Filter Configuration Now you can add custom filters to show messages which ar...
All Swing-related operations happen on a dedicated thread (the EDT - Event Dispatch Thread). If this thread gets blocked, the UI becomes non-responsive. Therefore, if you want to delay an operation you cannot use Thread.sleep. Use a javax.swing.Timer instead. For example the following Timer will re...
In order to get the length of a String object, call the length() method on it. The length is equal to the number of UTF-16 code units (chars) in the string. String str = "Hello, World!"; System.out.println(str.length()); // Prints out 13 Live Demo on Ideone A char in a String is UTF...
All Javadoc comments begin with a block comment followed by an asterisk (/**) and end when the block comment does (*/). Optionally, each line can begin with arbitrary whitespace and a single asterisk; these are ignored when the documentation files are generated. /** * Fields can be documented as ...
The ScheduledExecutorService class provides a methods for scheduling single or repeated tasks in a number of ways. The following code sample assume that pool has been declared and initialized as follows: ScheduledExecutorService pool = Executors.newScheduledThreadPool(2); In addition to the nor...
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...
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...
Needed imports: import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; This code will create a clip and play it continuously once started: Clip clip = AudioSystem.getClip(); clip.open(AudioSystem.getAudioInputStream(new URL(filename))); clip.start(); clip.loop(Clip.LOOP_CO...
add() and roll() can be used to increase/decrease Calendar fields. Calendar calendar = new GregorianCalendar(2016, Calendar.MARCH, 31); // 31 March 2016 The add() method affects all fields, and behaves effectively if one were to add or subtract actual dates from the calendar calendar.add(Calend...
// 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) { ...
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 2 of 145