From Java to Ceylon
BASICS
Print
Java
System.out.print("Hello, World!");
System.out.println("Hello, World!");
Ceylon
print("Hello, World!");
Variables I
Java
final int x;
final int y = 1;
Ceylon
Integer x;
value y = 1;
Variables II
Java
int w;
int z = 2;
z = 3;
w = 1;
Ceylon
variable Integer w;
variable Integer z = 2;
z = 3;
w = 1;
Null I
Java
final String name = null;

String lastName;
lastName = null;
Ceylon
String? name = null;

variable String? lastName;
lastName = null;

String firstName;
firstName = null; // Compilation error!!
Null II
Java
if(text != null){
  int length = text.length();
}

int length = text != null ? text.length() : 0;
Ceylon
if (exists text) {
    value length = text.length;
}

value length = text?.length else 0;
Strings I
Java
String name = "John";
String lastName = "Smith";
String text = "My name is: " + name + " " + lastName;
String otherText = "My name is: " + name.substring(2);
Ceylon
value name = "John";
value lastName = "Smith";
value text = "My name is: ``name`` ``lastName``";
value otherText = "My name is: ``name[2...]``":
Strings II
Java
String text = "First Line\n" +
              "Second Line\n" +
              "Third Line";
Ceylon
value text = "First Line
              Second Line
              Third Line";
Ternary Operator
Java
String text = x > 5 ? "x > 5" : "x <= 5";
Ceylon
value text = x > 5 then "x > 5" else "x <= 5";
BASICS
Bits Operations
Java
final int andResult  = a & b;
final int orResult   = a | b;
final int xorResult  = a ^ b;
final int rightShift = a >> 2;
final int leftShift  = a << 2;
Ceylon
value andResult  = a.and(b);
value orResult   = a.or(b);
value xorResult  = a.xor.(b);
value rightShift = a.rightLogicalShift(2);
value leftShift  = a.leftLogicalShift(2);
Is As In
Java
if(x instanceof Integer){ }

final String text = (String) other;

if(x >= 0 && x <= 10 ){}
Ceylon
if (is Integer x) { }

assert (is String text = other);

if (0 <= x <= 10) {}
if (x in 0..10) {}
Smart Cast
Java
if(a instanceof String){
  final String result = ((String) a).substring(1);
}
Ceylon
if (is String a) {
  value result = a[1...];
}
Switch / When
Java
final int x = // value;
final String xResult;

switch (x){
  case 0:
  case 11:
    xResult = "0 or 11";
    break;
  case 1:
  case 2:
    //...
  case 10:
    xResult = "from 1 to 10";
    break;
  default:
    if(x < 12 && x > 14) {
      xResult = "not from 12 to 14";
      break;
    }

    if(isOdd(x)) {
      xResult = "is odd";
      break;
    }

    xResult = "otherwise";
}



final int y = // value;
final String yResult;

if(isNegative(y)){
  yResult = "is Negative";
} else if(isZero(y)){
  yResult = "is Zero";
}else if(isOdd(y)){
  yResult = "is Odd";
}else {
  yResult = "otherwise";
}
Ceylon

value x = // value
value result = switch (x) 
case (0 | 11) "0 or 11"
case ((1..11).contains(x)) "yes" 
case (!(12.14).contains(x)) "no" 
else otherwise";

value y = // value
value result = switch (y) 
case (isNegative(y)) "is Negative"
case (isZero(y)) "is Zero"
case (isOdd(y)) "is odd"
else otherwise";
For
Java
for (int i = 1; i < 11 ; i++) { }

for (int i = 1; i < 11 ; i+=2) { }

for (String item : collection) { }

for (Map.Entry<String, String> entry: map.entrySet()) { }
Ceylon
for (i in 1..10) { }

for (i in (1..10).by(2)) {}

for (item in collection) {}
 
for (key->item in map)  {}
Collections
Java
final List<Integer> numbers = Arrays.asList(1, 2, 3);

final Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");
Ceylon
value numbers =  Array{ 1,2,3,4 };

value map = HashMap {
    1 -> "One",
    2 -> "Two",
    3 -> "Three"
};
Collections
Java
for (int number : numbers) {
  System.out.println(number);
}

for (int number : numbers) {
  if(number > 5) {
    System.out.println(number);
  }
}
Ceylon
numbers.each((Integer number) => print(number));

numbers.filter((Integer number) => number == 2);
Collections
Java
final Map<String, List<Integer>> groups = new HashMap<>();
for (int number : numbers) {
  if((number & 1) == 0){
    if(!groups.containsKey("even")){
      groups.put("even", new ArrayList<>());
    }

    groups.get("even").add(number);
    continue;
  }

  if(!groups.containsKey("odd")){
    groups.put("odd", new ArrayList<>());
  }

  groups.get("odd").add(number);
}
Ceylon
value groups = numbers.group((number) => number.even then "even" else "odd");
Collections
Java
final List<Integer> evens = new ArrayList<>();
final List<Integer> odds = new ArrayList<>();
for (int number : numbers){
  if ((number & 1) == 0) {
    evens.add(number);
  }else {
    odds.add(number);
  }
}
Ceylon
//partition funtion is part of collection module
value result = partition(numbers, (Integer number) => number.even);
Collections
Java
final List<User> users = getUsers();

Collections.sort(users, new Comparator<User>(){
  public int compare(User user, User otherUser){
    return user.lastname.compareTo(otherUser.lastname);
  }
});

// or

users.sort(Comparator.comparing(user -> user.lastname));
Ceylon
value users = getUsers();
users.sort(byDecreasing((User user) => user.lastname));