[Java] Differences between the two main languages that people who learned Java after learning Ruby felt

2 minute read

I started to study Java grammar and there are many complicated rules compared to Ruby, so make a note of it.


System.out.println "Java"; // This is the same role as Puts in Ruby.

Variables and data types

To define a variable in Java, (1) Specify the data type of the value to be put in the variable (2) Decide the name of the variable I need two.

int number = 7;

String name = "Sato"; //String type is an uppercase letter s

double syosu = 3.14;

Cast (forced type conversion)

 int number1 = 7;
 int number2 = 2;

 System.out.println(number1/number2); // result is 3
 System.out.println((double)number1 / number2); // result 3.5

Boolean value boolean type

  System.out.println(12 / 4 == 3); //print true

  System.out.println(12/4 != 3); //output as false

if statement

Write the conditional expression in () after if

  if (true) {
    System.out.println("Printed because the conditional expression is true");
  if (false) {
    System.out.println("Not output because the conditional expression is false");

switch statement

int number = 13;
    // Process when no case matches using default
    switch (number %5) {
      case 0:
        System.out.println("It's Daikichi");
      case 1:
        System.out.println("I am Nakayoshi");
      case 4:
        System.out.println("Good luck");


First, specify the element type of the array. The array is {} curly braces.

 String names[] = {"YUI","YUKI","YOU"};

Extended for statement (simply write the for statement)

   String[] names = {"dog", "cat", "turtle"};
    for (String name: names){
      System.out.println("My pet is "+name+"");

Classes and methods

Java executes classes instead of files. The flow in which there is a Main method in the Main class and the hello method in it is executed.

class Main {
// method definition void means that there is no return value
  public static void main(String[] args) {
  public static void hello() {
    // Replace "Hello World" with "Hello Java"
    System.out.println("Hello Java");

Return value (when you want to use the processing result of the method in the method caller)

class Main {
  public static void main(String[] args) {
    // Assign the result of fullName method to variable name
    String name = fullName("Kate" ,"Jones");
    // Please change the argument of printData
    printData(name, 27);
    // do not rewrite this
    printData("John Christopher Smith", 65);

  public static void printData(String name, int age) {
    System.out.println("My name is "+ name + "");
    System.out.println("age is" + age + "is age");

  // define fullName method
  public static String fullName(String firstName ,String lastName){
    return firstName + "" + lastName;

Overload (if the type and number of arguments are different, you can define a method with the same name)

// Computer can judge as separate methods

hello(); // hello method with no arguments

hello("Java"); //method with one argument hello

boolean return value

Conditional branch based on the return value (true/false value) of the isHealthy method
Method 1
    if (isHealthy(bmi)){
      System.out.println("Not healthy");

Method 2
  public static boolean isHealthy(double bmi){
    return bmi >= 18.5 && bmi <25.0;


You can also “type” a value into the console and use that value in your program. A library called Scanner is used to receive the input to the console.

//import required
import java.util.Scanner;

class Main {
  public static void main (String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Name: ");
    // define the variable name, receive the string from the console and substitute
    String name = scanner.next();
    // Please be output as "Hello ◯◯-san"
    System.out.println ( "Hello" + name + "Mr.");

Following Part 2…