AddThis Social Bookmark Button

Print

Conversational C# for Java Programmers

by Raffi Krikorian
05/31/2001

Microsoft introduced C# during June of 2000 as a "modern, object-oriented programming language built from the ground up to exploit the power of XML-based Web services on the .NET platform."

Its chief architect for C#, Anders Hejlsberg, has apparently had the main goal of designing a language that is both object- and component-oriented (software written in C# is meant to be used by other software) and has looked at other languages for guidance on how to do this. Luckily for Java programmers, two of the languages studied were C++ and Java -- so any Java programmer will immediately feel familiar reading and writing C# code as the fundamental concepts and syntaxes are very similar.

The two of them side by side

The best way for a Java programmer to get his crash course in C# is probably to translate a toy Java project into that language. Let's do just that -- starting with Hello.java.

package com.oreilly.hello;
import java.io.*;
/**
 * a simple application that we are going to port
 * to c# as a demonstration
 *
 * @author Raffi Krikorian <r@bitwaste.com>
 */
public class Hello extends Object {
  /**
   * the entry point into this class -- this
   * class will ask the user for his name, then
   * reply to it
   *
   * @param args the command line arguments
   */
  public static void main( String[] args ) {

    // the name we are going to say hello to
    String name = null;

    // if the name is not on the command line,
    // then we ask to read it off standard in
    if( args.length == 0 ) {
      try {
        System.out.print( "Name? " );

        // read from standard in
        BufferedReader reader = new BufferedReader( 
            new InputStreamReader( System.in ) );
        name = reader.readLine();
      } catch( IOException error ) {
        System.err.println( "Problem with input stream" );
        error.printStackTrace();
        System.exit( 1 );
      }
    else {
      // take the name off the command line
      StringBuffer buffer = new StringBuffer();
      for( int count=0;
           count<args.length;
           count++ ) {
        buffer.append( args[count] );
        buffer.append( ' ' );
      }
      buffer.setLength( buffer.length() - 1 );
      name = buffer.toString();
    }

    // echo the name to the screen
    System.out.print( "Hello " + name + "," );
    System.out.println( " welcome to Java" );
  }
}

Comment on this articleIs C# really Java.NET? If so, what are your thoughts regarding the importance of understanding this from the perspective of a .NET developer as well as even a Java developer new to C#?
Post your comments

This is a modified version of the second Java program I wrote when I was first learning Java (the first being the requisite "Hello World"). It's a pretty trivial program, as all it does is say "Hello <name>, welcome to Java," where it gets the name either off the command line of the program or by asking the user for his name when it is first run.

Now let's do the same thing in C# in a file named Hello.cs (this code has been written and compiled with the Microsoft.NET SDK Beta 1).

using System;
using System.IO;
using System.Text;

namespace com.oreilly.hello {
  /// <summary>
  /// a simple application that was ported from 
  /// java to c#
  /// </summary>
  public class Hello : Object {

    /// <summary>
    /// the entry point into this class -- 
    /// this class will ask the
    /// user for his name, then reply to it
    /// </summary>
    /// <param name="args">the command line 
    /// arguments</param>
    /// <returns>
    /// 0 if everything goes well, 1 if 
    /// there is an error
    /// </returns>
    static int Main( string[] args ) {
      // name we are going to say hello to
      string name;

      // if the name is not on the command 
      // line, then we ask to read it off 
      // standard in
      if( args.Length == 0 ) {

        try {

          Console.Write( "Name? " );
          // read from standard in
          name = Console.ReadLine();
  
        } catch( IOException error ) {

          Console.Error.WriteLine( "Problem with input stream" );
          Console.Error.WriteLine( error.StackTrace );
          return 1;
        }
      } else {

        // take the name off the command 
        // line
        StringBuilder buffer = 
            new StringBuilder();
        foreach( string arg in args ) {
            buffer.Append( arg );
            buffer.Append( ' ' );
        }

        buffer.Length = buffer.Length - 1;
        name = buffer.ToString();
      }
  
      // echo the name to the screen
      Console.Write( "Hello " + name + "," );
      Console.WriteLine( " welcome to C#" );
      return 0;
    }
  }
}

Looks remarkably similar, no?

Pages: 1, 2, 3

Next Pagearrow