Java for R Developers
Transfer R experience to Java through typed values, control flow, collections, methods and object contracts. Build a connected multi-file order processor and port a stock ledger.
R and Java side by side
The same program in R and in Java, from the lessons of this course.
Strings and Output
R cat writes supplied values with a configurable separator and needs an explicit newline; print uses a representation intended for inspection. Java System.out.println takes one completed value and adds a newline. Build the string before printing rather than…
R1name <- "Ada"; cat(paste0("Hi ",name," !"),"\n",sep="")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
R character == compares values element by element and can return several logical results or NA. Java == between String references compares identity, so use equals for content and Objects.equals when either reference may be null. R identical returns one…
R1a <- "ab";b <- paste0("a","b");cat(a==b,"\n",sep="")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
R vectors use one-based indexing, negative subscripts exclude elements, and replacement can extend a vector. Java arrays instead have fixed length and checked zero-based indexes; a negative index throws rather than excluding data. R [ can select several…
R1values <- c(2,4,6);cat(values[length(values)],"\n",sep="")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
R while uses a single non-missing condition, break exits a loop, and next skips the current iteration. Java spells that skip operation continue and requires a Boolean condition. Make sure a manual index advances on every continuing path. A stop marker should…
R1n <- 3;while(n>1) n <- n-1;cat(n,"\n",sep="")Java1public class Example {2 public static void main(String[] args) {3 String[] words = {"tea", "", " ", "cake", "STOP", "later"};4 for (String word : words) {5 if (word.equals("STOP")) {6 break;7 }8 if (word.trim().isEmpty()) {9 continue;10 }11 System.out.println(word);12 }13 }14}Static Methods and Returns
R functions are ordinary values and arguments are generally evaluated lazily when used. Java methods belong to classes and arguments are evaluated before the call. A static method needs no receiver and a non-void method must return on every normal path. Java…
R1hours <- function(total) total %/% 60;cat(hours(125),"\n",sep="")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
R readLines reads text from a connection and cat can emit exact output without print’s display annotations. The Java task supplies a BufferedReader driver and expects a returned string. Java Integer.parseInt rejects malformed text and does not trim whitespace…
R1cents <- 305;cat(sprintf("tea: %d.%02d",as.integer(cents %/% 100),as.integer(cents %% 100)),"\n",sep="")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 R Developers for?
For people who already write R and want to learn Java. Each lesson starts from R 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 R basics: variables, conditions, loops, functions and collections.
How many lessons does Java for R Developers have?
Java for R Developers contains 48 interactive lessons you can complete at your own pace.
What does Java for R 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 R Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.