Tutorial by Examples: ect

We can create an ArrayList (following the List interface): List aListOfFruits = new ArrayList(); Java SE 5 List<String> aListOfFruits = new ArrayList<String>(); Java SE 7 List<String> aListOfFruits = new ArrayList<>(); Now, use the method add to add a String: ...
Sorting lists Prior to Java 8, it was necessary to implement the java.util.Comparator interface with an anonymous (or named) class when sorting a list1: Java SE 1.2 List<Person> people = ... Collections.sort( people, new Comparator<Person>() { public int compare(Pe...
Consider the following JSON string: { "title": "test", "content": "Hello World!!!", "year": 2016, "names" : [ "Hannah", "David", "Steve" ] } This JSON object can...
Create the JSONObject using the empty constructor and add fields using the put() method, which is overloaded so that it can be used with different types: try { // Create a new instance of a JSONObject final JSONObject object = new JSONObject(); // With put you can add a name/va...
// Create a new instance of a JSONArray JSONArray array = new JSONArray(); // With put() you can add a value to the array. array.put("ASDF"); array.put("QWERTY"); // Create a new instance of a JSONObject JSONObject obj = new JSONObject(); try { // Add the JSONAr...
Two methods in java.util.Collection create an array from a collection: Object[] toArray() <T> T[] toArray(T[] a) Object[] toArray() can be used as follows: Java SE 5 Set<String> set = new HashSet<String>(); set.add("red"); set.add("blue"); ...
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...
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...
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 ...
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; ...
Once we've gotten the Connection, we will mostly use it to create Statement objects. Statements represent a single SQL transaction; they are used to execute a query, and retrieve the results (if any). Let's look at some examples: public void useConnection() throws SQLException{ Connection ...
In your build.gradle file of main module(app), define your minimum and target version number. android { //the version of sdk source used to compile your project compileSdkVersion 23 defaultConfig { //the minimum sdk version required by device to run your app minSd...
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...
Date date = new Date(); System.out.println(date); // Thu Feb 25 05:03:59 IST 2016 Here this Date object contains the current date and time when this object was created. Calendar calendar = Calendar.getInstance(); calendar.set(90, Calendar.DECEMBER, 11); Date myBirthDate = calendar.getTime(); ...
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...
Unlike in languages like Python, static properties of the constructor function are not inherited to instances. Instances only inherit from their prototype, which inherits from the parent type's prototype. Static properties are never inherited. function Foo() {}; Foo.style = 'bold'; var foo = ne...
5 Object.keys(obj) returns an array of a given object's keys. var obj = { a: "hello", b: "this is", c: "javascript!" }; var keys = Object.keys(obj); console.log(keys); // ["a", "b", "c"]
Django is a web development framework based on Python. Django 1.11 (the latest stable release) requires Python 2.7, 3.4, 3.5 or 3.6 to be installed. Assuming pip is available, installation is as simple as running the following command. Keep in mind, omitting the version as shown below will install t...
print_r() - Outputting Arrays and Objects for debugging print_r will output a human readable format of an array or object. You may have a variable that is an array or object. Trying to output it with an echo will throw the error: Notice: Array to string conversion. You can instead use the print_r...

Page 2 of 99