CSC300: Python to Java: Function declarations [33/40] Previous pageContentsNext page

PythonJava
01
02
03
04
05
06
07
08
def addHello (x):
  return "Hello " + x

def main ():
  print (addHello ("Bob"))
  print (addHello ("Alice"))

main ()
01
02
03
04
05
06
07
08
09
10
11
package algs11;
import stdlib.*;
public class Hello {
  public static String addHello (String x) {
    return "Hello " + x;
  }
  public static void main (String[] args) {
    addHello ("Bob");
    addHello ("Alice");
  }
}

Java requires declaration of return type and parameter types.

Because of types, java compiler can catch more errors, as below. What's wrong?

PythonJava
01
02
03
04
05
06
07
08
def addHello (x):
  "Hello " + x

def main ():
  print (addHello ("Bob"))
  print (addHello ("Alice"))

main ()
01
02
03
04
05
06
07
08
09
10
11
package algs11;
import stdlib.*;
public class Hello {
  public static String addHello (String x) {
    "Hello " + x;
  }
  public static void main (String[] args) {
    addHello ("Bob");
    addHello ("Alice");
  }
}

Previous pageContentsNext page