Scratch to Java (Part-1)
Transitioning from block coding to text can be challenging. In this post, we'll look into the 1:1 code comparison of Scratch and Java. In the next post, we'll take a Scratch project and code it in Java.
![](https://blog.gocobi.com/content/images/2023/07/scratch-1.png)
Transitioning from block coding to text can be challenging. In this post, we'll look into the 1:1 code comparison of Scratch and Java. In the next post, we'll take a Scratch project and code it in Java.
Common challenges of transitioning from Scratch to Java:
- All code blocks are visible on Scratch. However, for Java, students will have to look up the Java syntax and commands from documentation websites or by searching online.
- Scratch doesn't produce syntax errors. Java on the other hand has strict syntax rules and the wrong syntax will result in errors.
- Scratch produces visual results and animations. Students may find Java less engaging at first.
Level of Abstraction
Block-based program | ![]() |
High-level program (Java) | class Triangle { … double area(float b, float h) { return b*h/2; } ... } |
Low-level program | LOAD r1, b LOAD r2, h MUL r1, r2 DIV r1, #2 RET |
Executable Machine code | 0000100101010001010001010 1000101010000110101001100 0101001000100010010101000 1010001010100010101000... |
Scratch to Java Code Examples
Output
![](https://blog.gocobi.com/content/images/2023/07/1643393073-09MS2CV0-blobid1643393089919.webp)
System.out.println("Hello World!");
Instantiating a variable
![](https://blog.gocobi.com/content/images/2023/07/1643392942-JzrTvuMJ-blobid1643392958494.webp)
int a = 5;
int b = 3;
Operators
![](https://blog.gocobi.com/content/images/2023/07/1643393468-37ZsAC6J-blobid1643393484431.webp)
int a = 5;
int b = 3;
System.out.println(a + b);
Conditional Statements
![](https://blog.gocobi.com/content/images/2023/07/1643393626-XlLslcMX-blobid1643393641926.webp)
if (a > 50) {
System.out.println("Great!");
}else{
System.out.println("Try again!");
}
For loop
![](https://blog.gocobi.com/content/images/2023/07/1643394886-Nj2U2u6N-blobid1643394902456.webp)
int a = 0;
for (int i = 0; i < 3; i++){
a = a + 1;
}
Getting user input
![](https://blog.gocobi.com/content/images/2023/07/1643395768-KPBT2u8g-blobid1643395784254.webp)
import java.util.Scanner;
public class Test {
public static void main(String[] args){
Scanner scanObj = new Scanner(System.in);
System.out.println("What's your name?");
String name = scanObj.nextLine();
System.out.println("Hi "+ name);
}
}
Function and function call
![](https://blog.gocobi.com/content/images/2023/07/1643395236-bl5cxH5k-blobid1643395252473.webp)
public static void main(String[] args){
hello("Julia");
}
public static void hello(String name){
System.out.println("Hello " + name);
}
Related Links: