midul's blog

By midul, 14 years ago, In English
i want to start participating in contest.
but i can not make decide which language to pick up c++/java.

please help me to make decision.
thanks.
  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
14 years ago, # |
  Vote: I like it +15 Vote: I do not like it
I think it is bad idea to learn both languages simultaneously. It is better to start with one of them, and personally suggest to start with java.
  • 14 years ago, # ^ |
      Vote: I like it +12 Vote: I do not like it
    I agree, because C++ has a lot of obliviousness things as memory leaks, zero-terminated strings, pointers, lack of array border checking and other.
    Java is better, because you don't need to think about memory management, invalid pointers and arrays size
    • 14 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it
      but you must think about time limits:)
      • 14 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it
        Yes, you do.
      • 14 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it
        You must think about time limits for all languages. If there is a sutiation, when an O(nlogn) solution is considered, and a C++ O(n^2) solution passes, while the same algo in Java fails, then it is the oversight of the authors of the problemset.
    • 14 years ago, # ^ |
      Rev. 2   Vote: I like it +2 Vote: I do not like it
      You can avoid most of bad C things.

      > memory leaks
      Don't use pointers. Use std::vector instead of arrays.

      > zero-terminated strings
      Use std::string.

      > lack of array border checking
      Use vector.at method.
      • 14 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it
        and lose all C++'s benefits
        • 14 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it
          What benefits? I think std::vector and std::string are real benefits of C++. They aren't much slower than C style arrays and strings. At least not slower than Java. And the code is pretty concise. At least not more verbose than Java.
          • 14 years ago, # ^ |
            Rev. 4   Vote: I like it 0 Vote: I do not like it
            No once my code get AC after TL & replacing vectors and strings

            So, It rather slow.

            Sorry, I can't compare with Java, but I think it isn't very faster than Java
      • 14 years ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it
        Do use pointers. They don't cause memory leaks themselves. The only thing you should really never use is a dynamic memory allocation — it's slow and leaks the space (especially because of granularity). Also if we're talking about contest programming, there is no need for "new", "delete" and "malloc" because input is always bound by some constants. Static allocation is enough. For example, you can use the following technique as a substitution for "new" keyword:

        Some_structure pool[MAX];

        Some_structure * next = &pool[0];

        Some_structure * New() {
        return next++;
        }

        This technique can be adapted to support "deletion" too. And this will work much faster than heap memory allocation, because static arrays are implemented by mapping virtual memory and real allocation of memory pages is being done inside kernel's code, so it doesn't affect your process's CPU time.
      • 14 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it
        But Java gives you more information about exception: full stacktrace. C++ does not.
14 years ago, # |
  Vote: I like it +6 Vote: I do not like it
use pascal - be a real pro! kidding-kidding..:)
14 years ago, # |
  Vote: I like it 0 Vote: I do not like it
I noticed that C++ teaches you well how stuff works (including standard data structures). Besides, starting with C++ gives you basic skills in all C-style languages.
Java is good for it's wide functionality. However, it can often be somewhat messy — I think C++ is cleaner.
  • 14 years ago, # ^ |
      Vote: I like it +5 Vote: I do not like it
    > However, it can often be somewhat messy — I think C++ is cleaner.
    It seems exactly opposite for me) C++ is too low-level to have clean code
    • 14 years ago, # ^ |
      Rev. 2   Vote: I like it +3 Vote: I do not like it
      I tried to use Java instead of C++ on TopCoder but I didn't feel myself comfortable with it because Java is too verbose. Does verbosity of Java affect you in any way? For example, it's a pain for me to type "System.out.println" instead of "printf" or "Map<Integer, Integer> m = new HashMap<Integer, Integer>()" instead of "map<int,int> m". These differences may seem subtle but in a big enough program they add up together and make expressing my thoughts in Java much harder than in C++. Or maybe I just didn't get used to programming in Java?

      Another aspect of Java that I don't like is that Java collections can't contain primitive values. They need to be wrapped in objects. In C++ I often use vector<int>-s, but in Java I'd have to use ArrayList<Integer>. Sequential processing of all elements of ArrayList may be much slower because of cache misses.
      • 14 years ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it
        Why do you think using objects instead of primitive types is a drawback of Java? Just because you need to type Integer instead of int?
        If you feel that Java is too verbose, Ctrl + space is the help.
        HashMap, HashSet are the things which C++ doesn't possess, and in some cases they really can help you. Existence of StringBuilder, Comparator, java.awt.geom.*, BigInteger, BigDecimal cover the fact that java is verbose.
        Epic fail :)
        • 14 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it
          Here is an example where Java's ArrayList is slower than C++'s vector. The algorithm is simple. Fill a vector with numbers from 1 to n, then random shuffle the elements of the vector, then calculate the sum of elements. 


          For n = 1000000 the Java version is 10 times slower than C++.

          The verbosity is not just problem of typing. It's also harder to read such program and find errors in algorithm or implementation.
          • 14 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it
            Now, if we replace vector<int> with array of ints in C++ program (http://ideone.com/1QHHy) then the execution time almost doesn't change. 

            But if we replace ArrayList<Integer> with int[] array in Java version (http://ideone.com/jf7F6) the execution time becomes much lower, about 1.5 times of C++ program.

            So what I want to say is that using vector<int> instead of int[] in C++ doesn't add any overhead, but using ArrayList<Integer> instead of int[] in Java makes program much slower. And that's primarily because integers in ArrayList are stored in wrapper objects.
            • 14 years ago, # ^ |
              Rev. 2   Vote: I like it 0 Vote: I do not like it
              Integer is 32 bit, as well as int. Try to test C# List: it stores primitive types, but the speed is not much better than in Java.
              I remember the times when replacing vector with an array in C++ solved the problem with TLE. 
              ArrayList really sucks. And in most cases it can be replaced by the functionality with faster arrays or HashSet. Remember that arrays in java are much more sophisticated: jagged int [][] a = new int [n][]; is not possible in c++.
              Anyway, it's the question of tastes. Everyone supports the language he knows best of all.

              • 14 years ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it
                Integer is 32 bit, as well as int. Try to test C# List: it stores primitive types, but the speed is not much better than in Java.

                Are you sure? I believe C#'s list speed must be close to C++. 

                So, do you agree that there's no easy and efficient way to work with arbitrary length lists of primitive types in Java like there is in C++? If so then it's a big minus in my opinion, because I often use such lists in C++.
                • 14 years ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it
                  Yes, C# is fast. About 1.5 times slower than C++.

                  • 14 years ago, # ^ |
                      Vote: I like it 0 Vote: I do not like it
                    Indeed .Net is faster than java.But for some reason mono,which is the compiler that codeforces use,is even slower than java .
                    according to my testing it is between 1.5 and 2 time slower than java.(I tested it in codeforces.)
        • 14 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it
          I never met any C++ compiler that doesn't support stdext namespace which contains stdext::hash_set and stdext::hash_map.

          java.awt.geom.*, BigInteger, BigDecimal — well, you don't have'em in C++, but what can StringBuilder and Comparator do that std::string and STL generic algorithms cannot?
          • 14 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it
            Java has very powerful regexps with .split() method. split() and StringTokenizer are very convenient to parse complex inputs
      • 14 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it
        Despite the verbosity of java, my solutions on TC are offen among the fastest, so I think that's not a big deal