CSC300: Python to Java: Variable declarations [30/40] Previous pageContentsNext page

PythonJava
01
02
name = "Bob"
print ("Hello " + name)
01
02
03
04
05
06
07
08
09
package algs11;
import stdlib.*;
public class Hello {
  public static void main (String[] args) {
    String name;
    name = "Bob";
    StdOut.println ("Hello " + name);
  }
}

In python, values are typed, but variables are not.

In java, both values and variables are typed. Variable types must be explicitly declared.

The declaration and initialization can be combined into a single statement.

01
02
    String name = "Bob";
    StdOut.println ("Hello " + name);

In both languages, + is used to represent string concatenation.

Here's another version which does not use concatenation.

PythonJava
01
02
03
name = "Bob"
print ("Hello ", end="")
print (name)
01
02
03
04
05
06
07
08
09
package algs11;
import stdlib.*;
public class Hello {
  public static void main (String[] args) {
    String name = "Bob";
    StdOut.print ("Hello ");
    StdOut.println (name);
  }
}

Previous pageContentsNext page