Swift for Java Developers
Move from Java objects, interfaces and exceptions to Swift values, optionals, protocols and ARC. Practice with 48 multi-file tasks, build a parcel desk, and port two complete Java applications.
Java and Swift side by side
The same program in Java and in Swift, from the lessons of this course.
Bindings and Mutation
Java final prevents reassignment of a variable. If it holds an object reference, the object can still be mutable. Swift let also fixes a binding, but a value stored in a let binding cannot be mutated. Use var for a binding whose value changes.
Java1import java.util.*;2import java.util.function.*;3public class Main {4 5 public static void main(String[] args) throws Exception {6 final int saved = 18;7 int current = saved;8 current = 21;9 System.out.println(saved + " " + current);10 }11}Swift1import Foundation2let saved = 183var current = saved4current = 215print("\(saved) \(current)")Strings and Interpolation
Java builds a string with + or a formatting API. Swift string interpolation inserts an expression using \(expression) inside a string literal. The expression is evaluated and rendered at that location.
Java1import java.util.*;2import java.util.function.*;3public class Main {4 5 public static void main(String[] args) throws Exception {6 int percent = 40;7 System.out.println("Downloaded " + percent + "%");8 }9}Swift1import Foundation2let percent = 403print("Downloaded \(percent)%")Switch and Patterns
Java 17 switch expressions can use arrow cases to yield a value without fallthrough. Swift switch also has no implicit fallthrough. Each Swift switch must cover every possible value or include a default case.
Java1import java.util.*;2import java.util.function.*;3public class Main {4 5 public static void main(String[] args) throws Exception {6 String command = "?";7 String message = switch (command) {8 case "help", "?" -> "show help";9 case "quit" -> "close";10 default -> "unknown";11 };12 System.out.println(message);13 }14}Swift1import Foundation2let command = "?"3switch command {4case "help", "?": print("show help")5case "quit": print("close")6default: print("unknown")7}Functions and Labels
Java methods belong to classes; a static utility method needs no instance. Swift can declare free functions at module scope. In these examples free functions belong in Functions.swift, while main.swift only calls them.
Java1import java.util.*;2import java.util.function.*;3public class Main {4 static String line(String symbol) { return line(symbol, 3); }5 static String line(String symbol, int count) { return symbol.repeat(count); }6 public static void main(String[] args) throws Exception {7 System.out.println(line("*"));8 }9}Swift1import Foundation2func line(_ symbol: String, count: Int = 3) -> String {3 String(repeating: symbol, count: count)4}Dictionaries and Missing Keys
Java Map.get returns null for an absent mapping, which may be ambiguous when null values are permitted. Swift Dictionary lookup returns an optional value. Use optional binding or a fallback to handle a missing key.
Java1import java.util.*;2import java.util.function.*;3public class Main {4 5 public static void main(String[] args) throws Exception {6 Map<String, Integer> settings = new HashMap<>();7 settings.put("zoom", 2);8 settings.put("zoom", 3);9 System.out.println(settings.getOrDefault("zoom", 1) + " " + settings.getOrDefault("speed", 1));10 }11}Swift1import Foundation2var settings = ["zoom": 2]3settings["zoom"] = 34print("\(settings["zoom"] ?? 1) \(settings["speed"] ?? 1)")Structs and Independent Copies
Java ordinary classes are reference types. Java records are also reference types, even though their component fields are final. Swift structs have value semantics and can contain both stored state and methods.
Java1public class Theme {2 private String name;3 public Theme(String name) { this.name = name; }4 public void select(String value) { name = value; }5 public String name() { return name; }6}Swift1import Foundation2struct Theme {3 var name: String4 mutating func select(_ value: String) { name = value }5}
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsStarting Swift
5 lessonsText and Decisions
5 lessonsControl Flow
5 lessonsFunctions and Arrays
5 lessonsCollection Tools
5 lessonsValues and References
5 lessonsFailures and Abstraction
6 lessonsDesigning Types
6 lessonsParcel Desk Project
4 lessonsJava Ports
2 lessonsFrequently asked questions
Who is Swift for Java Developers for?
For people who already write Java and want to learn Swift. Each lesson starts from Java you know and shows how Swift does the same thing, so you skip the beginner basics.
Do I need to know Swift already?
No. The course starts with your first Swift program. You need Java basics: variables, conditions, loops, functions and collections.
How many lessons does Swift for Java Developers have?
Swift for Java Developers contains 48 interactive lessons you can complete at your own pace.
What does Swift for Java Developers cover?
The course is organized into chapters covering: Starting Swift, Text and Decisions, Control Flow, Functions and Arrays, and more.
Is Swift for Java Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.