Tutorial by Examples: e

The AppCompat support library provides themes to build apps with the Material Design specification. A theme with a parent of Theme.AppCompat is also required for an Activity to extend AppCompatActivity. The first step is to customize your theme’s color palette to automatically colorize your app. I...
Using BufferedReader: System.out.println("Please type your name and press Enter."); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try { String name = reader.readLine(); System.out.println("Hello, " + name + "!"); } catch(I...
The Arrays.asList() method can be used to return a fixed-size List containing the elements of the given array. The resulting List will be of the same parameter type as the base type of the array. String[] stringArray = {"foo", "bar", "baz"}; List<String> stringL...
It is possible to define an array with more than one dimension. Instead of being accessed by providing a single index, a multidimensional array is accessed by specifying an index for each dimension. The declaration of multidimensional array can be done by adding [] for each dimension to a regular a...
For basic prototypes or basic command-line behavior, the following loop comes in handy. public class ExampleCli { private static final String CLI_LINE = "example-cli>"; //console like string private static final String CMD_QUIT = "quit"; //string for exit...
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...
Updating the state of a Swing component must happen on the Event Dispatch Thread (the EDT). The javax.swing.Timer triggers its ActionListener on the EDT, making it a good choice to perform Swing operations. The following example updates the text of a JLabel each two seconds: //Use a timer to updat...
In the ActionListener attached to a javax.swing.Timer, you can keep track of the number of times the Timer executed the ActionListener. Once the required number of times is reached, you can use the Timer#stop() method to stop the Timer. Timer timer = new Timer( delay, new ActionListener() { priv...
public interface MyInterface { public void foo(); int bar(); public String TEXT = "Hello"; int ANSWER = 42; public class X { } class Y { } } Interface members always have public visibility, even if the public keyword is omitted. So both foo...
Following snippet shows an example of passing a method group (as opposed to a lambda) when a delegate is expected. Overload resolution will now resolve this instead of raising an ambiguous overload error due to the ability of C# 6 to check the return type of the method that was passed. using System...
It is possible to generate a Stream that does not end. Calling a terminal method on an infinite Stream causes the Stream to enter an infinite loop. The limit method of a Stream can be used to limit the number of terms of the Stream that Java processes. This example generates a Stream of all natural...
The ArrayIndexOutOfBoundsException is thrown when a non-existing index of an array is being accessed. Arrays are zero-based indexed, so the index of the first element is 0 and the index of the last element is the array capacity minus 1 (i.e. array.length - 1). Therefore, any request for an array e...
The String type provides two methods for converting strings between upper case and lower case: toUpperCase to convert all characters to upper case toLowerCase to convert all characters to lower case These methods both return the converted strings as new String instances: the original String o...
private visibility allows a variable to only be accessed by its class. They are often used in conjunction with public getters and setters. class SomeClass { private int variable; public int getVariable() { return variable; } public void setVariable(int variable) { ...
With no modifier, the default is package visibility. From the Java Documentation, "[package visibility] indicates whether classes in the same package as the class (regardless of their parentage) have access to the member." In this example from javax.swing, package javax.swing; public abs...
Protected visibility causes means that this member is visible to its package, along with any of its subclasses. As an example: package com.stackexchange.docs; public class MyClass{ protected int variable; //This is the variable that we are trying to access public MyClass(){ var...
If you need to extract a part of string from the input string, we can use capture groups of regex. For this example, we'll start with a simple phone number regex: \d{3}-\d{3}-\d{4} If parentheses are added to the regex, each set of parentheses is considered a capturing group. In this case, we a...
To check whether a particular String a is being contained in a String b or not, we can use the method String.contains() with the following syntax: b.contains(a); // Return true if a is contained in b, false otherwise The String.contains() method can be used to verify if a CharSequence can be fou...
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...
By using startActivityForResult(Intent intent, int requestCode) you can start another Activity and then receive a result from that Activity in the onActivityResult(int requestCode, int resultCode, Intent data) method. The result will be returned as an Intent. An intent can contain data via a Bundle ...

Page 17 of 1191