R for Java Developers
Move from Java to base R: vectors, missing values, data frames, functions, S3 dispatch, S4 validity and closure state. Build a connected multi-file basket and port a stock ledger.
Java and R side by side
The same program in Java and in R, from the lessons of this course.
Text and Output
Java System.out.println adds a line ending, while print and printf give more explicit control. R cat needs an explicit final newline; paste and paste0 construct strings. Rscript executes an entry file and source evaluates supporting declarations. R print can…
Java1import java.util.*;2import java.util.function.*;3import java.util.regex.*;4public class Main {5 6public static void main(String[] args) throws Exception {7System.out.println("Hi"+", Ada");8}9}R1ready <- 32cat(paste0("Ready: ", ready), "\n", sep="")Comparisons and Recycling
Java array == compares references, Arrays.equals compares array contents, and String.equals compares text. R == is elementwise and may recycle shorter operands. Use all for a reduced decision or identical when storage types and attributes belong to equality.…
Java1import java.util.*;2import java.util.function.*;3import java.util.regex.*;4public class Main {5 6public static void main(String[] args) throws Exception {7int[] a={1},b={1}; System.out.println(a==b);8}9}R1cat(identical(1L, 1), all(c(2, 3) == c(2, 3)), "\n")Vector Subsetting
Java arrays and lists use zero-based indexes and reject negative positions. For a nonempty array, length minus one selects the final element. R positive indexes are one-based, negative indexes exclude positions and zero selects nothing. Do not mix positive…
Java1import java.util.*;2public class Main {3 4public static void main(String[] args){5int[] values={4,8,12};System.out.println(values[values.length-1]);6}7}R1values <- c(4, 8, 12)2cat(paste(values[-1], collapse=" "), "\n", sep="")Typed Iteration
Java streams describe a pipeline and a terminal operation such as toArray performs the traversal. R lapply returns a list immediately, sapply attempts simplification and vapply checks each result against a type-and-length prototype. Choose the expected result…
Java1import java.util.*;2public class Main {3 4public static void main(String[] args){5String[] words={"a","tea"};int[] lengths=Arrays.stream(words).mapToInt(String::length).toArray();System.out.println(lengths[0]+":"+lengths[1]);6}7}R1words <- c("a", "tea")2sizes <- vapply(words, nchar, integer(1), USE.NAMES=FALSE)3cat(paste(sizes, collapse=" "), "\n", sep="")Functions and Returns
Java methods have declared return types and explicitly return values; a lambda expression body is a separate syntax. R functions return their last evaluated expression by default and offer return for early exit. R uses lexical scoping and lazy argument…
Java1import java.util.*;2import java.util.function.*;3import java.util.regex.*;4public class Main {5static int twice(int x){return x*2;}6public static void main(String[] args) throws Exception {7System.out.println(twice(4));8}9}R1is_long <- function(text) nchar(text)>42cat(is_long("coffee"), "\n", sep="")String Parsing
Java Pattern and Matcher can check whole text with matches, while numeric parsing can still fail on invalid or out-of-range input. R trimws and strsplit operate on character vectors, and strsplit returns a list of token vectors. Fixed separators differ from…
Java1import java.util.*;2import java.util.function.*;3import java.util.regex.*;4public class Main {5 6public static void main(String[] args) throws Exception {7Matcher match=Pattern.compile("^([a-z]+)=([0-9]+)$").matcher("pen=12"); if(match.matches()) System.out.println(match.group(2));8}9}R1parts <- strsplit("tea,cake", ",", fixed=TRUE)[[1]]2cat(length(parts), "\n", sep="")
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsFiles and Atomic Values
6 lessonsLogic and Iteration
6 lessonsCollections and Ownership
5 lessonsData and Transformations
4 lessonsFunctions and Closures
5 lessonsParsing and S3 Dispatch
5 lessonsObjects and Invariants
6 lessonsCollaboration and Contracts
5 lessonsProject: Basket
4 lessonsFinal Challenges
2 lessonsFrequently asked questions
Who is R for Java Developers for?
For people who already write Java and want to learn R. Each lesson starts from Java you know and shows how R does the same thing, so you skip the beginner basics.
Do I need to know R already?
No. The course starts with your first R program. You need Java basics: variables, conditions, loops, functions and collections.
How many lessons does R for Java Developers have?
R for Java Developers contains 48 interactive lessons you can complete at your own pace.
What does R for Java Developers cover?
The course is organized into chapters covering: Files and Atomic Values, Logic and Iteration, Collections and Ownership, Data and Transformations, and more.
Is R 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.