Tutorial by Examples: c

Snippet public class SomeClass<TItem> { public void PrintTypeName() { Console.WriteLine(nameof(TItem)); } } ... var myClass = new SomeClass<int>(); myClass.PrintTypeName(); Console.WriteLine(nameof(SomeClass<int>)); Console Output TItem S...
Create a property with getter and/or setter and initialize all in one line: public string Foobar { get; set; } = "xyz";
public string Foobar { get { return _foobar; } set { _foobar = value; } } private string _foobar = "xyz";
class Example { public string Foobar { get; set; } public List<string> Names { get; set; } public Example() { Foobar = "xyz"; Names = new List<string>(){"carrot","fox","ball"}; } }
Create a new file in your text editor or IDE named HelloWorld.java. Then paste this code block into the file and save: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } Run live on Ideone Note: For Java to r...
Say we have a button (we can create it programmatically, or bind it from a view using findViewbyId(), etc...) Button btnOK = (...) Now, create an anonymous class and set it inline. btnOk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ...
Declaration of an interface using the interface keyword: public interface Animal { String getSound(); // Interface methods are public by default } Override Annotation @Override public String getSound() { // Code goes here... } This forces the compiler to check that we are overri...
Collect with toList() and toSet() Elements from a Stream can be easily collected into a container by using the Stream.collect operation: System.out.println(Arrays .asList("apple", "banana", "pear", "kiwi", "orange") .stream() .filter...
An exception can be caught and handled using the try...catch statement. (In fact try statements take other forms, as described in other examples about try...catch...finally and try-with-resources.) Try-catch with one catch block The most simple form looks like this: try { doSomething(); } ...
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...
Generics enable classes, interfaces, and methods to take other classes and interfaces as type parameters. This example uses generic class Param to take a single type parameter T, delimited by angle brackets (<>): public class Param<T> { private T value; public T getValue() ...
This is an example of a default build.gradle file in a module. apply plugin: 'com.android.application' android { compileSdkVersion 25 buildToolsVersion '25.0.3' signingConfigs { applicationName { keyAlias 'applicationName' keyPassword 'password...
The following example shows the basics of throwing an exception: public void checkNumber(int number) throws IllegalArgumentException { if (number < 0) { throw new IllegalArgumentException("Number must be positive: " + number); } } The exception is thrown on the 3...
An abstract class is a class marked with the abstract keyword. It, contrary to non-abstract class, may contain abstract - implementation-less - methods. It is, however, valid to create an abstract class without abstract methods. An abstract class cannot be instantiated. It can be sub-classed (exten...
Product flavors are defined in the build.gradle file inside the android { ... } block as seen below. ... android { ... productFlavors { free { applicationId "com.example.app.free" versionName "1.0-free" } paid { ...
Dependencies can be added for a specific product flavor, similar to how they can be added for specific build configurations. For this example, assume that we have already defined two product flavors called free and paid (more on defining flavors here). We can then add the AdMob dependency for the ...
Resources can be added for a specific product flavor. For this example, assume that we have already defined two product flavors called free and paid. In order to add product flavor-specific resources, we create additional resource folders alongside the main/res folder, which we can then add resourc...
Basic cases int[] numbers1 = new int[3]; // Array for 3 int values, default value is 0 int[] numbers2 = { 1, 2, 3 }; // Array literal of 3 int values int[] numbers3 = new int[] { 1, 2, 3 }; // Array of 3 int values initialized int[][] numbers4 = { { 1, 2...
A Java class can implement multiple interfaces. public interface NoiseMaker { String noise = "Making Noise"; // interface variables are public static final by default String makeNoise(); //interface methods are public abstract by default } public interface FoodEater { ...

Page 10 of 826