Java Training Course/JT04

From tehowiki
Revision as of 09:54, 24 September 2017 by imported>Gfis
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Integer Arithmetic

  • Create a file DupInt.java by copying the program DupString.java from JT03, rename the class and replace lines 3 to 5 with:
       int st0 = Integer.parseInt(args[0]); // the 1st argument on the commandline
       int st1 = Integer.parseInt(args[1]); // the 2nd ...
       int st2 = st0 + st1;
  • Compile
  • Predict the output of:
java DupInt 2 4
java DupInt 2 -4
java DupInt 0 -1
  • Bonus question: What will happen if you run the program without any argument?

< Previous: JT03 Main Arguments and String Concatenation
> Next: JT05 Greatest Common Divisor