The J2SE Development Kit (JDK) 8.0 Update 20 (for Windows x86) is used to compile and run solutions written in Java. You can download JDK and read online documentation on the official site.
Java programs are executed on the server with the following command line:
java -client -Xmx544m -Xss64m -DONLINE_JUDGE YourClassName
How to write a Java solution
A solution written in Java must contain a single public class. This class may have any name and must contain the method "public static void main(String[] args)
". Furthermore, the solution may contain any number of nested classes and global non-public classes. At the same time the package
declaration is not supported. Here is an example of A + B problem solution:
import java.io.*;
import java.util.*;
public class Sum
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int a = in.nextInt();
int b = in.nextInt();
out.println(a + b);
out.flush();
}
}
The following solution is also correct:
import java.util.*;
class YouCanUseSuchClasses {}
public class Sum2
{
class AndSuchClassesToo {}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println(in.nextInt() + in.nextInt());
}
}
Localization
The locale Locale.US
is used at the server by default. Setting another locale in your solution may have an influence on how your solution reads/writes floating point numbers.
Input/output
Input/output operations in Java may be very slow in the case of wrong usage. Here are some rules which should be followed to avoid input/output performance problems:
Scanner
is suitable to read input data for the most of problems, but it is very slow. You should use it to read small input data only.BufferedReader
provides quite fast read operations for almost all problems. But this class may be used to read single characters and lines only. To read tokens and numbers you should use StringTokenizer
or StreamTokenizer
.PrintWriter
is preferred in all cases and works rather fast. But its method printf
is too slow as well as calls like println(a + " " + b)
. You should output variables one-by-one to reach the highest performance.
Here is an example of a proper usage of StreamTokenizer
and PrintWriter
classes (the problem is to input the integer numbers A and B and then to output their sum and difference separated by single space):
import java.io.*;
import java.util.*;
public class SumDif
{
public static void main(String[] args) throws IOException
{
new SumDif().run();
}
StreamTokenizer in;
PrintWriter out;
int nextInt() throws IOException
{
in.nextToken();
return (int)in.nval;
}
void run() throws IOException
{
in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
solve();
out.flush();
}
void solve() throws IOException
{
int a = nextInt();
int b = nextInt();
out.print(a + b);
out.print(" ");
out.println(a - b);
}
}
To read and write ASCII characters with codes more than 127 in the same way as in other programming languages, you should use the following constructors:
Scanner scanner = new Scanner(System.in, "ISO-8859-1");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, "ISO-8859-1"));
PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out, "ISO-8859-1"));
It should be done since the default server charset does not guarantee a proper input in this case. If a default charset is used, some characters with codes from 128 to 255 might be converted to Unicode characters with codes more that 255. And then such characters may be printed as, for example, question marks.
Using property ONLINE_JUDGE
Programs are executed on the server with the defined property ONLINE_JUDGE
. This property can be used to determine if the program is run on the server. You can use it, for example, to decide whether to work with files or with the standard i/o:
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
Reader reader = oj ? new InputStreamReader(System.in) : new FileReader("input.txt");
Writer writer = oj ? new OutputStreamWriter(System.out) : new FileWriter("output.txt");
StreamTokenizer in = new StreamTokenizer(new BufferedReader(reader));
PrintWriter out = new PrintWriter(writer);
Earlier compilers
- Since June, 1 2006 till February, 2 2009 the JDK 5.0 was used.
- The JDK 6.0 was used until January, 18 2013
- The JDK 7.0 was used until October, 3 2014.