Wednesday, October 29, 2014

Is C# more superior than Java as a programming language, objectively speaking?


The question should be "Which language is better suited for modern, typical application development?".
Edit: I addressed some of the comments below. A small remark: consider that when you have a lot of things natively, as idioms, it's a big difference than implementing or downloading and using them yourself every time. Almost everything can be implemented in any of these languages. The question is - what the languages natively provide you with.
So off the top of my head (some arguments apply to both languages)...

C# is better than C++ in that:

  • It has native garbage-collection.
  • It allows you to treat class-methods' signatures as free functions (i.e. ignoring the statically typed this pointer argument), and hence create more dynamic and flexible relationships between classes.edit if you don't know what this means, then try assigning a member method returning void and accepting void to a void (*ptr)() variable. C# delegates carry the this pointer with them, but the user doesn't always have to care about that. They can just assign a void() method of any class to any other void() delegate.
  • It has a huge standard library with so much useful stuff that's well-implemented and easy to use.
  • It allows for both managed and native code blocks.
  • Assembly versioning easily remedy DLL hell problems.
  • You can set classes, methods and fields to be assembly-internal (which means they are accessible from anywhere within the DLL they're declared in, but not from other assemblies).

C# is better than Java in that:

  • Instead of a lot of noise (EJB, private static class implementations, etc) you get elegant and friendly native constructs such as Properties and Events.
  • You have real generics (not the bad casting joke that Java calls generics), and you can perform reflection on them.
  • It supports native resource-management idioms (the using statement). Java 7 is also going to support this, but C# has had it for a way longer time.
  • It doesn't have checked exceptions :) (debatable whether this is good or bad)
  • It's deeply integrated with Windows, if that's what you want.
  • It has Lambdas and LINQ, therefore supporting a small amount of functional programming.
  • It allows for both generic covariance and contravariance explicitly.
  • It has dynamic variables, if you want them.
  • Better enumeration support, with the yield statement.
  • It allows you to define new value (or non-reference) types.

Edit - Addressing comments

  • I didn't say C++ doesn't support native RAII. I said Java doesn't have it (you have to explicitly do a try/finally). C++ has auto pointers which are great for RAII, and (if you know what you're doing) can also substitute garbage-collection.
  • I didn't say anything about emulating free functions. But for example if you need to access a field by a this pointer, and bind the method that does it to a generic function pointer (i.e. not in the same class), then there's simply no native way to do it. In C#, you get the for free. You don't even have to know how it works.
  • By "treating member methods as free functions" I meant that you can't, for example, natively bind a member method to a free function signature, because the member method "secretly" needs the this pointer.
  • The using statement, obviously along with IDisposable wrappers, is a great example of RAII. See this link. Consider that you don't need RAII as much in C# as you do in C++, because you have the GC. For the specific times you do need it, you can explicitly use the using statement. Another little reminder: freeing memory is an expensive procedure. GC have their performance advantage in a lot of cases (especially when you have lots of memory). Memory won't get leaked, and you won't be spending a lot of time on deallocating. What's more, allocation is faster as well, since you don't allocate memory every time, only once in a while. Calling new is simply incrementing a last-object-pointer.
  • "C# is worse in that it has garbage collection". This is indeed subjective, but as I stated at the top, for most modern, typical application development, garbage collection is one hell of an advantage.
    In C++, your choices are either to manually manage your memory using new and delete, which empirically always leads to errors here and there, or (with C++11) you can use auto pointers natively, but keep in mind that they add lots and lots of noise to the code. So GC still has an edge there.
  • "Generics are way weaker than templates" - I just don't know where you got that from. Templates might have their advantages, but in my experience constraints, generic parameter type-checking, contravariance and covariance are much stronger and elegant tools. The strength in templates is that they let you play with the language a bit, which might be cool, but also causes lots of headaches when you want to debug something. So all in all, templates have their nice features, but I find generics more practical and clean.

JAVA PPROGRAMMING

Elements of Programming :

Overview.

 Our goal in this chapter is to convince you that writing a computer program is easier than writing a piece of text such as a paragraph or an essay. Writing prose is difficult: we spend many years in school to learn how to do it. By contrast, just a few building blocks suffice to take us into a world where we can harness the computer to help us solve all sorts of fascinating problems that would be otherwise unapproachable. In this chapter, we take you through these building blocks, get you started on programming in Java, and study a variety of interesting programs.


Why java is called platform independent language ?


One of the major features of java includes that why java is called platform independent language.
Before understanding this feature we need to know about -

Javac – compiler that converts source code to byte code. 
JVM- interpreter that converts byte code to machine language code. 
As we know java is both compiler & interpreter based language. Once the java code also known as source code is compiled, it gets converted to native code known as BYTE CODE which is portable & can be easily executed on all operating systems. Byte code generated is basically represented in hexa decimal format. This format is same on every platform be it Solaris work station or Macintosh, windows or Linux. After compilation, the interpreter reads the generated byte code & translates it according to the host machine. . Byte code is interpreted by Java Virtual Machine which is available with all the operating systems we install. so to port Java programs to a new platform all that is required is to port the interpreter and some of the library routines.
Source code -> javac ->Universal byte code


In this section, our plan is to lead you into the world of Java programming by taking you through the three basic steps required to get a simple program running. The Java system is a collection of applications not unlike any of the other applications that you are accustomed to using (such as your word processor, e-mail program, or internet browser). As with any application, you need to be sure that Java is properly installed on your computer. You also need an editor and a terminal application. Here are system specific instructions for three popular home operating systems. [ Mac OS X · Windows · Linux ]

Programming in Java.

 We break the process of programming in Java into three steps:
  1. Create the program by typing it into a text editor and saving it to a file named, say, MyProgram.java.
  2. Compile it by typing "javac MyProgram.java" in the terminal window.
  3. Run (or execute) it by typing "java MyProgram" in the terminal window.
The first step creates the program; the second translates it into a language more suitable for machine execution (and puts the result in a file named MyProgram.class); the third actually runs the program.
  • Creating a Java program. A program is nothing more than a sequence of characters, like a sentence, a paragraph, or a poem. To create one, we need only define that sequence characters using a text editor in the same way as we do for e-mail. HelloWorld.java is an example program. Type these character into your text editor and save it into a file named HelloWorld.java.
    public class HelloWorld { 
       public static void main(String[] args) { 
          System.out.println("Hello, World");
       }
    }
    
  • Compiling a Java program. At first, it might seem to you as though the Java programming language is designed to be best understood by the computer. Actually, to the contrary, the language is designed to be best understood by the programmer (that's you). A compiler is an application that translates programs from the Java language to a language more suitable for executing on the computer. It takes a text file with the .java extension as input (your program) and produces a file with a.class extension (the computer-language version). To compile HelloWorld.java type the boldfaced text below at the terminal. (We use the % symbol to denote the command prompt, but it may appear different depending on your system.)
    % javac HelloWorld.java
    
    If you typed in the program correctly, you should see no error messages. Otherwise, go back and make sure you typed in the program exactly as it appears above.
  • Executing a Java program. Once you compile your program, you can run it. This is the exciting part, where the computer follows your instructions. To run the HelloWorld program, type the following at the terminal:
    % java HelloWorld
    
    If all goes well, you should see the following response
    Hello, World
    
  • Understanding a Java program. The key line with System.out.println() send the text "Hello, World". When we begin to write more complicated programs, we will discuss the meaning of publicclassmainString[]argsSystem.out, and so on.
  • Creating your own Java program. For the time being, all of our programs will be just like HelloWorld.java, except with a different sequence of statements in main(). The easiest way to write such a program is to:



Below is the syntax  HelloWorld.java :

/*************************************************************************
 *  Compilation:  javac HelloWorld.java
 *  Execution:    java HelloWorld
 *
 *  Prints "Hello, World". By tradition, this is everyone's first program.
 *
 *  % java HelloWorld
 *  Hello, World
 *
 *  These 17 lines of text are comments. They are not part of the program;
 *  they serve to remind us about its properties. The first two lines tell
 *  us what to type to compile and test the program. The next line describes
 *  the purpose of the program. The next few lines give a sample execution
 *  of the program and the resulting output. We will always include such 
 *  lines in our programs and encourage you to do the same.
 *
 *************************************************************************/

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello, World");
    }

}