Tutorial by Examples: ar

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...
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 ...
Starting a service is very easy, just call startService with an intent, from within an Activity: Intent intent = new Intent(this, MyService.class); //substitute MyService with the name of your service intent.putExtra(Intent.EXTRA_TEXT, "Some text"); //add any extra data to pass to the s...
String str = "My String"; System.out.println(str.charAt(0)); // "M" System.out.println(str.charAt(1)); // "y" System.out.println(str.charAt(2)); // " " System.out.println(str.charAt(str.length-1)); // Last character "g" To get the nth charac...
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...
Enum can be considered to be syntax sugar for a sealed class that is instantiated only a number of times known at compile-time to define a set of constants. A simple enum to list the different seasons would be declared as follows: public enum Season { WINTER, SPRING, SUMMER, FA...
One use of SharedPreferences is to implement a "Settings" screen in your app, where the user can set their preferences / options. Like this: A PreferenceScreen saves user preferences in SharedPreferences. To create a PreferenceScreen, you need a few things: An XML file to define the av...
Java's Reflection API allows the programmer to perform various checks and operations on class fields, methods and annotations during runtime. However, in order for an annotation to be at all visible at runtime, the RetentionPolicy must be changed to RUNTIME, as demonstrated in the example below: @i...
Calendar, Date, and LocalDate Java SE 8 before, after, compareTo and equals methods //Use of Calendar and Date objects final Date today = new Date(); final Calendar calendar = Calendar.getInstance(); calendar.set(1990, Calendar.NOVEMBER, 1, 0, 0, 0); Date birthdate = calendar.getTime(); ...
Calendar objects can be created by using getInstance() or by using the constructor GregorianCalendar. It's important to notice that months in Calendar are zero based, which means that JANUARY is represented by an int value 0. In order to provide a better code, always use Calendar constants, such as...
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"); // Define a global variable engine.put("textToPrint", "Data defined in Java."); // Print the global var...
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...
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...
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...
The Java language provides 7 operators that perform arithmetic on integer and floating point values. There are two + operators: The binary addition operator adds one number to another one. (There is also a binary + operator that performs string concatenation. That is described in a separate e...

Page 4 of 218