Java for C++ Developers
Transfer C++ experience to Java through values, control flow, collections, methods, exceptions, files, and object contracts. Build a connected multi-file order processor.
C++ and Java side by side
The same program in C++ and in Java, from the lessons of this course.
Strings and Output
C++ stream insertion chains values using <<. Java calls System.out.println with one value; use concatenation or formatted output to assemble a line. Java execution starts in a static main method on a class rather than a free main function.
C++1#include <iostream>2#include <optional>3#include <string>4#include <vector>5#include <array>6#include <map>7#include <set>8#include <algorithm>9#include <numeric>10#include <functional>11#include <stdexcept>12 13int main() {14 std::cout << std::boolalpha;15 std::cout << "Hello, " << "Ada" << "!\n";16}Java1import java.util.*;2import java.util.function.*;3import java.util.stream.*;4public class Example {5 6public static void main(String[] args) {7String name="Ada";System.out.println("Hi "+name+"!");8}9}String Equality
C++ value types often define operator==; Java object == compares references. Java text and collection values generally need equals. Primitive comparisons still use ==. Do not infer object equality from identical printed fields.
C++1#include <iostream>2#include <optional>3#include <string>4#include <vector>5#include <array>6#include <map>7#include <set>8#include <algorithm>9#include <numeric>10#include <functional>11#include <stdexcept>12 13int main() {14 std::cout << std::boolalpha;15 std::string a="pen",b="pen"; std::cout << (a==b) << "\n";16}Java1import java.util.*;2import java.util.function.*;3import java.util.stream.*;4public class Example {5 6public static void main(String[] args) {7String a=new String("ab"),b=new String("ab");System.out.println(a.equals(b));System.out.println(a==b);8}9}Fixed-size Arrays
C++ std::array has a length in its type; Java arrays have a runtime length that cannot change after allocation. Java checks indexes and throws on invalid access. Java primitive arrays are initialized to zero, whereas uninitialized local C++ storage must not…
C++1#include <iostream>2#include <optional>3#include <string>4#include <vector>5#include <array>6#include <map>7#include <set>8#include <algorithm>9#include <numeric>10#include <functional>11#include <stdexcept>12 13int main() {14 std::cout << std::boolalpha;15 std::array<int,3> values{2,4,6}; std::cout << values.size() << "\n";16}Java1import java.util.*;2import java.util.function.*;3import java.util.stream.*;4public class Example {5 6public static void main(String[] args) {7int[]a={2,4,6};System.out.println(a[a.length-1]);8}9}While and Early Exit
The usual while, break, and continue behavior transfers from C++ to Java. Ensure each path either advances the loop or exits it. A break leaves the loop; continue skips to the next condition check, which can matter for manual indexes.
C++1#include <iostream>2#include <optional>3#include <string>4#include <vector>5#include <array>6#include <map>7#include <set>8#include <algorithm>9#include <numeric>10#include <functional>11#include <stdexcept>12 13int main() {14 std::cout << std::boolalpha;15 int n=3; while(n>1) --n; std::cout << n << "\n";16}Java1public class Example {2 public static void main(String[] args) {3 String[] words = {"red", "", "blue", "STOP", "green"};4 int index = 0;5 while (index < words.length) {6 String word = words[index++];7 if (word.equals("STOP")) break;8 if (word.isEmpty()) continue;9 System.out.println(word);10 }11 }12}Static Methods and Returns
Java has no free functions: reusable operations are static or instance methods in classes. Return types are explicit, and non-void methods must return on every normal path. A static helper does not require constructing a utility object.
C++1#include <iostream>2#include <optional>3#include <string>4#include <vector>5#include <array>6#include <map>7#include <set>8#include <algorithm>9#include <numeric>10#include <functional>11#include <stdexcept>12int twice(int n) { return n*2; }13int main() {14 std::cout << std::boolalpha;15 std::cout << twice(4) << "\n";16}Java1import java.util.*;2import java.util.function.*;3import java.util.stream.*;4public class Example {5static int hours(int total){return total/60;}6public static void main(String[] args) {7System.out.println(hours(125));8}9}Input and Exact Formatting
C++ formatted extraction resembles Java Scanner token reads, while getline resembles nextLine. Mixing token and line readers can leave a newline to consume. Keep input plumbing in the supplied Java driver and put domain logic in editable methods.
C++1#include <iostream>2#include <optional>3#include <string>4#include <vector>5#include <array>6#include <map>7#include <set>8#include <algorithm>9#include <numeric>10#include <functional>11#include <stdexcept>12 13int main() {14 std::cout << std::boolalpha;15 int n; std::cin >> n; std::cout << n*2 << "\n";16}Java1import java.util.*;2import java.util.function.*;3import java.util.stream.*;4public class Example {5 6public static void main(String[] args) {7int cents=305;System.out.println(String.format(Locale.ROOT,"tea: %d.%02d",cents/100,cents%100));8}9}
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsGetting Started in Java
6 lessonsValues and Decisions
5 lessonsCollections and References
5 lessonsIteration and Pipelines
5 lessonsMethods and Callbacks
5 lessonsInput, Errors, and Files
5 lessonsClasses and State
6 lessonsComposition and Contracts
5 lessonsProject: Order Processing
4 lessonsFinal Challenges
2 lessonsFrequently asked questions
Who is Java for C++ Developers for?
For people who already write C++ and want to learn Java. Each lesson starts from C++ you know and shows how Java does the same thing, so you skip the beginner basics.
Do I need to know Java already?
No. The course starts with your first Java program. You need C++ basics: variables, conditions, loops, functions and collections.
How many lessons does Java for C++ Developers have?
Java for C++ Developers contains 48 interactive lessons you can complete at your own pace.
What does Java for C++ Developers cover?
The course is organized into chapters covering: Getting Started in Java, Values and Decisions, Collections and References, Iteration and Pipelines, and more.
Is Java for C++ Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.