Skip to content

Instantly share code, notes, and snippets.

View vaskoz's full-sized avatar
🏠
Working from home

Vasko Zdravevski vaskoz

🏠
Working from home
View GitHub Profile
@vaskoz
vaskoz / IsReflectionSlow.java
Created July 15, 2013 05:57
Is reflection in Java a performance killer? Run these tests to check for yourself. After compilation, just run with the command line below passing a list of any strings as command line input to args[].
import java.lang.reflect.*;
public class IsReflectionSlow {
public static double rand() {
return Math.random();
}
public static String constant() {
return "constant";
}
@vaskoz
vaskoz / Deps.java
Created July 17, 2013 07:40
An example of "jdeps" command line tool in Java 8 with JDeps usage help and 3 different outputs of -v (verbose) -P (profile) and -R (recursive)
import java.util.Set;
import java.util.HashSet;
public class Deps {
public static void main(String[] args) {
System.out.println(Math.random());
Set<String> set = new HashSet<>();
}
}
@interface Foo {}
interface Bar {}
public class Annotation {
public static void main(String[] args) {
System.out.println("Foo annotation super interfaces");
for (Class<?> klazz : Foo.class.getInterfaces())
System.out.println(klazz);
System.out.println("Bar interface super interfaces");
for (Class<?> klazz : Bar.class.getInterfaces())
System.out.println(klazz);
import java.util.Arrays;
import java.time.*;
public class PSort {
public static void main(String[] args) {
if (args.length != 2) {
System.err.println("Usage: PSort [seq|par] [data size int]");
System.exit(1);
}
boolean parallel = "par".equals(args[0]);
@vaskoz
vaskoz / FunctionDescriptorExamples.java
Last active December 20, 2015 22:58
Function Interface & Descriptor Examples from JLS 8 - JSR 335
// Function descriptor examples:
interface X { void m() throws IOException; }
interface Y { void m() throws EOFException; }
interface Z { void m() throws ClassNotFoundException; }
interface XY extends X, Y {}
interface XYZ extends X, Y, Z {}
// XY has descriptor ()->void throws EOFException
// XYZ has descriptor ()->void (throws nothing)
@vaskoz
vaskoz / Null.groovy
Created August 13, 2013 03:29
Examples of how 'null' is less than everything in Groovy.
println "null < ${Long.MIN_VALUE}: ${null < Long.MIN_VALUE}"
println "null < ${Boolean.FALSE}: ${null < Boolean.FALSE}"
println "false < true: ${false < true}"
println "null < new Object(): ${null < new Object()}"
println "null < null: ${null < null}"
def data = ["1", 2, 3L, null, "foo"]
println data.sort()
@vaskoz
vaskoz / EffectivelyFinal.java
Created August 14, 2013 01:30
Examples of Lambda Expressions,
void m1(int x) {
int y = 1;
foo(() -> x+y);
// Legal: x and y are both effectively final.
}
void m2(int x) {
int y;
y = 1;
foo(() -> x+y);
@vaskoz
vaskoz / MethodReferences.java
Created August 14, 2013 04:14
Java 8 Method Reference examples from JLS 8
System::getProperty
String::length
List<String>::size // explicit class type args
List::size // inferred class type args, or generic
int[]::clone
T::tvarMember
"abc"::length
foo[x]::bar
(test ? list.map(String::length) : Collections.emptyList())::iterator
@vaskoz
vaskoz / MethodReferences.java
Created August 14, 2013 06:00
An example of method references using Java 8 Comparator static methods
import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.stream.Collectors;
public class MethodReferences {
public static void main(String[] args) {
List<String> list = new ArrayList<String>(Arrays.asList("cat", null, "foo", "aces", "bar", null, "az", null, "bc"));
@vaskoz
vaskoz / Jvm8Changes.java
Last active December 21, 2015 12:08
JSR-335 4 bytecode updates: invokeinterface, invokespecial, invokestatic, and invokevirtual
interface I {
default String hi() { return "hello"; }
static String here() { return "i'm static"; }
// NEXT LINE WILL NOT COMPILE. But JLS8 says private default methods are allowed.
default private String you() { return "yoho"; } // Not implemented as of jdk8b103.
}
class C implements I {
}
public class Jvm8Changes {
public static void main(String[] args) {