Tutorial by Examples: ant

public static Unsafe getUnsafe() { try { Field unsafe = Unsafe.class.getDeclaredField("theUnsafe"); unsafe.setAccessible(true); return (Unsafe) unsafe.get(null); } catch (IllegalAccessException e) { // Handle } catch (IllegalArgumentExcept...
public class UnsafeLoader { public static Unsafe loadUnsafe() { return Unsafe.getUnsafe(); } } While this example will compile, it is likely to fail at runtime unless the Unsafe class was loaded with the primary classloader. To ensure that happens the JVM should be loaded with...
Current file You can get the name of the current PHP file (with the absolute path) using the __FILE__ magic constant. This is most often used as a logging/debugging technique. echo "We are in the file:" , __FILE__ , "\n"; Current directory To get the absolute path to the di...
When you use or, it will either return the first value in the expression if it's true, else it will blindly return the second value. I.e. or is equivalent to: def or_(a, b): if a: return a else: return b For and, it will return its first value if it's false, else it r...
function meridiem(d:Date):String { return (d.hours > 11) ? "pm" : "am"; }
We can use Predefined Constants for Date format in date() instead of the conventional date format strings since PHP 5.1.0. Predefined Date Format Constants Available DATE_ATOM - Atom (2016-07-22T14:50:01+00:00) DATE_COOKIE - HTTP Cookies (Friday, 22-Jul-16 14:50:01 UTC) DATE_RSS - RSS (Fri, 22...
Program units often make use of literal constants. These cover the obvious cases like print *, "Hello", 1, 1.0 Except in one case, each literal constant is a scalar which has type, type parameters and value given by the syntax. Integer literal constants are of the form 1 -1 -1_1 ...
App::uses('YourModel', 'Model'); $model_1 = new YourModel(array('ds' => 'default')); $model_2 = new YourModel(array('ds' => 'database2'));
A common pitfall of child classes is that, if your parent and child both contain a constructor(__construct()) method, only the child class constructor will run. There may be occasions where you need to run the parent __construct() method from it's child. If you need to do that, then you will need to...
A class in Scala is a 'blueprint' of a class instance. An instance contains the state and behavior as defined by that class. To declare a class: class MyClass{} // curly braces are optional here as class body is empty An instance can be instantiated using new keyword: var instance = new MyClas...
var a:Object; trace(a); // null trace(a.b); // Error 1009 Here, an object reference is declared, but is never assigned a value, be it with new or assignment of a non-null value. Requesting its properties or method results in a 1009 error.
Swallowing Exceptions One should always re-throw exception in the following way: try { ... } catch (Exception ex) { ... throw; } Re-throwing an exception like below will obfuscate the original exception and will lose the original stack trace. One should never do this! The st...
Open the Settings or Preferences dialog: On Windows or Linux, select File > Settings from the main menu. On Mac OSX, select Android Studio > Preferences from the main menu. Navigate to Build, Execution, Deployment > Compiler. In the text field next to Command-line Options, enter...
Move semantics are a way of moving one object to another in C++. For this, we empty the old object and place everything it had in the new object. For this, we must understand what an rvalue reference is. An rvalue reference (T&& where T is the object type) is not much different than a norma...
This example hides the red box (border) if the checkbox is not checked by making use of an IValueConverter. Note: The BooleanToVisibilityConverter used in the example below is a built-in value converter, located in the System.Windows.Controls namespace. XAML: <Window x:Class="StackOverflo...
As the static keyword is used for accessing fields and methods without an instantiated class, it can be used to declare constants for use in other classes. These variables will remain constant across every instantiation of the class. By convention, static variables are always ALL_CAPS and use unders...
Interfaces may have variant type parameters. interface IEnumerable<out T> { // ... } interface IComparer<in T> { // ... } but classes and structures may not class BadClass<in T1, out T2> // not allowed { } struct BadStruct<in T1, out T2> // not allo...
Delegates may have variant type parameters. delegate void Action<in T>(T t); // T is an input delegate T Func<out T>(); // T is an output delegate T2 Func<in T1, out T2>(); // T1 is an input, T2 is an output This follows from the Liskov Substitution Principle, w...
If a covariant type appears as an output, the containing type is covariant. Producing a producer of Ts is like producing Ts. interface IReturnCovariant<out T> { IEnumerable<T> GetTs(); } If a contravariant type appears as an output, the containing type is contravariant. Produc...
Let's say we have a class MyClass with no constructor argument: class MyClass In Scala we can instantiate it using below syntax: val obj = new MyClass() Or we can simply write: val obj = new MyClass But, if not paid attention, in some cases optional parenthesis may produce some unexpecte...

Page 3 of 11