Tutorial by Examples

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 ...
With this class: class ObjectMemberVsStaticMember { static int staticCounter = 0; int memberCounter = 0; void increment() { staticCounter ++; memberCounter++; } } the following code snippet: final ObjectMemberVsStaticMember o1 = new ObjectMemberVsStati...
Android Studio is the Android development IDE that is officially supported and recommended by Google. Android Studio comes bundled with the Android SDK Manager, which is a tool to download the Android SDK components required to start developing apps. Installing Android Studio and Android SDK tools:...
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...
If you want to take a screenshot from the Android Emulator (2.0), then you just need to press Ctrl + S or you click on the camera icon on the side bar: If you use an older version of the Android Emulator or you want to take a screenshot from a real device, then you need to click on the camera ico...
If you have many tasks to execute, and all these tasks are not dependent of the result of the precedent ones, you can use Multithreading for your computer to do all this tasks at the same time using more processors if your computer can. This can make your program execution faster if you have some bi...
Once the SDK installed, you can open the AVD Manager from the command line using android avd. You can also access AVD Manager from Android studio using Tools > Android > AVD Manager or by clicking on the AVD Manager icon in the toolbar which is the second in the screenshot below.
private static final String MY_PREF = "MyPref"; // ... SharedPreferences prefs = ...; // ... SharedPreferences.Editor editor = prefs.edit(); editor.putString(MY_PREF, "value"); editor.remove(MY_PREF); editor.apply(); After the apply(), prefs contains "key&q...
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...
A Toolbar is a generalization of ActionBar for use within application layouts. While an ActionBar is traditionally part of an Activity's opaque window decor controlled by the framework, a Toolbar may be placed at any arbitrary level of nesting within a view hierarchy. It can be added by performing t...
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...

Page 19 of 1336