Tutorial by Examples

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...
Visible to the class, package, and subclass. Let's see an example with the class Test. public class Test{ public int number = 2; public Test(){ } } Now let's try to create an instance of the class. In this example, we can access number because it is public. public class Ot...
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 ...
To create an IntentService, create a class which extends IntentService, and within it, a method which overrides onHandleIntent: package com.example.myapp; public class MyIntentService extends IntentService { @Override protected void onHandleIntent (Intent workIntent) { //Do so...
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...
Set up Android Studio Start by setting up Android Studio and then open it. Now, you're ready to make your first Android App! Note: this guide is based on Android Studio 2.2, but the process on other versions is mainly the same. Configure Your Project Basic Configuration You can start a new ...
As you use generic types with utility classes, you may often find that number types aren't very helpful when specified as the object types, as they aren't equal to their primitive counterparts. List<Integer> ints = new ArrayList<Integer>(); Java SE 7 List<Integer> ints = new A...
Let's say you want to define an interface that allows publishing / consuming data to and from different types of channels (e.g. AMQP, JMS, etc), but you want to be able to switch out the implementation details ... Let's define a basic IO interface that can be re-used across multiple implementations...
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. /** * Brief summary of this class...
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. /** * Brief summary of method, end...
String s = "this is an example"; String a = s.substring(11); // a will hold the string starting at character 11 until the end ("example") String b = s.substring(5, 10); // b will hold the string starting at character 5 and ending right before character 10 ("is an") S...
A Pattern can be compiled with flags, if the regex is used as a literal String, use inline modifiers: Pattern pattern = Pattern.compile("foo.", Pattern.CASE_INSENSITIVE | Pattern.DOTALL); pattern.matcher("FOO\n").matches(); // Is true. /* Had the regex not been compiled case...
Sometimes you may wish to read byte-input into a String. To do this you will need to find something that converts between byte and the "native Java" UTF-16 Codepoints used as char. That is done with a InputStreamReader. To speed the process up a bit, it's "usual" to allocate a b...

Page 20 of 1336