F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

About Java

<< Back to F.A.Q
Since 29 September 2006 the Java programming language is supported at HDU Online Judge. J2SE Development Kit (JDK) 5.0 Update 8 is used to compile and run solutions written in Java. You may download JDK and read online documentation at java.sun.com.
Q How to write a Java solution ?
A Solutions written in Java must contain a single public class named Main which is case-sensitive. This class 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. Here is an example of A + B problem solution:
import java.io.*;
import java.util.*;

public class Main
{
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        int a, b;

        while(cin.hasNextInt())
        {
            a = cin.nextInt();
            b = cin.nextInt();
            System.out.println(a + b);
        }
    }
}
The following solution is also correct:
import java.util.*;

class YouCanUseSuchClasses {}
public class Main
{
    class AndSuchClassesToo {}
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        int a, b;

        while(cin.hasNextInt())
        {
            a = cin.nextInt();
            b = cin.nextInt();
            System.out.println(a + b);
        }
    }
}
Q Execution time and used memory space
A Due to some peculiarities of the Java programming language, Java solutions often work slower and use more memory than solutions written in other programming languages regardless of problem difficulty. Nevertheless, it is guaranteed that almost all HDU Online Judge problems may be solved in Java as well as in other programming languages. For Java solutions, the time limit of each problem is always longer than other languages, and the memory limit is also larger. You can find these limits under each problem's title.
Q Input/output
A 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.*;

public class Main
{
    public static void main(String[] args) throws IOException
    {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        int a, b;
        while(in.nextToken() != StreamTokenizer.TT_EOF)
        {
            a = (int)in.nval;
            in.nextToken();
            b = (int)in.nval;
            out.println(a + b);
        }
        out.flush();
    }
}
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.
Q Stack size
A Standard stack size for Java programs is quite small (less than 256 KB). To increase the maximum stack size Java programs are executed on the server with the following command line arguments:
java -Xss64m YourClassName
It allows all the program threads to use up to 64 MB stack. Used stack size is considered while calculating a total used memory space. Here is an example of a proper usage of this feature (this program does not require a large stack, and the code just shows how to use threads):
import java.io.*;
import java.util.*;

public class Main implements Runnable
{
   void solve() throws IOException
   {
      int a = nextInt();
      int b = nextInt();
      out.println(a + b);
   }

   StreamTokenizer in;
   PrintWriter out;

   int nextInt() throws IOException
   {
      in.nextToken();
      return (int)in.nval;
   }

   public void run()
   {
      try
      {
         in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
         out = new PrintWriter(new OutputStreamWriter(System.out));
         solve();
         out.flush();
      }
      catch (IOException e)
      {
         throw new IllegalStateException(e);
      }
   }

   public static void main(String[] args)
   {
      new Thread(new Main()).start();
   }
}

This page was generated by web cache at 2010-08-08 15:56:08
 

Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 0, Server time : 2024-05-31 03:59:22, Gzip enabled