Skip to content

Instantly share code, notes, and snippets.

View Madusudanan's full-sized avatar

Madusudanan.B.N Madusudanan

View GitHub Profile
public class RuntimeJavaErrorDemo {
public static void main(String[] args) {
String test = null;
System.out.println(test.length());
}
}
class Test {
var x = None
println(x.length)
}
class Person {
var age = -1
var name = "Unnamed"
}
def factorial(a:Int): Int = {
if(a <=1) 1 else a * factorial(a-1)
}
let factorial 0 = 1; factorial n = n * factorial (n - 1)
@Madusudanan
Madusudanan / FactorialScalaError.scala
Created January 17, 2016 06:54
Factorial Program to describe lack of global type inference
def factorial(a: Int) = {
if (a <= 1) 1 else a * factorial(a - 1)
}
val x = 20
//print to the console
//legit, gets inferred as an integer
println(x+10)
//Something stupid as below will throw compile time error
val z = 40
println(z * "justastring")
val t = 69
//Prints 'E' the ASCII value of E is 69
println(t.toChar)
val s = "Hello World"
//Just like String char at, prints l
//Trace leads to the same String class charAt method
println(s.charAt(2))
final ArrayList<Integer> arrList = new ArrayList<Integer>();
//This does not result in error, we are mutating the object itself
//If it were mutable it would result in error to something like it cannot be changed
arrList.add(20);
//This results in error as we are modifying the reference and not the object itself
arrList = null;
class HelloWorld {
def main(args : Array[String]){
println("Hello world")
}
}