Comparison of C#.net with Java

Now let us see the main differences and similarities in Java and c#.net languages. While most of the concepts are similar, there are few major differences in terms of keywords and syntax
ConceptC#.netJava
Built-In LibrariesBase Class Library(BCL) contains namespaces like System, System.IO, System.Collections, System.Collections.Generic, System.Diagnostics, System.Glogalization, System.Security, System.Text, System.Threading. Apart from BCL, C#.net has System.Xml, System.Globalization, System.Net, System.Reflection namespaces Java class libraries (JCL) contains packages like java.lang, java.io, java.nio, java.util, java.net, java.math, java.awt, javax.swing, java.text, java.security, java.sql, java.applet
Third party librariesYou can install third party libraries using Nuget package manager. Some popular 3rd party libraries are Speckflow, Selenium WebdriverYou can download and install 3rd party libraries (dependencies) using maven build management tool. Some of the popular 3rd party libraries are selenium, restassured, cucumber, Jackson, Gson, Appium, Apache commons
Access modifierspublic, private, internal, protected, internal protected
  • internal - same assembly
  • protected - derived class
  • internal proteted - Same assembly and derived class
public, private, package, protected
  • package - same package
  • protected - same package and/or derived class
Reading and writing to console System.Console.ReadLine and System.Console.WriteLine can be used to read from and write to console System.in and System.out streams can be used to read from and write to console in Java. e.g. Scanner input = new Scanner(System.in); System.out.println("Enter number of items"); int no1 = Integer.parseInt(input.nextLine());
Numbers and currencies - parsing and formatting To convert string to number, you can use Convert class or TryParse method. e.g. System.Convert.ToInt32(dNumber) or int.TryParse("14", out numb)) To format numbers, you can use "22.443".ToString ("0.##"); String.Format("{0:.##}", output) String.Format("{0:C}", output) Each wrapper class provides the method to parse the numbers. E.g. Integre.parseInt. To format numbers, you can use java.text.DecimalFormat class. e.g. DecimalFormat f = new DecimalFormat("##.00"); System.out.println("Total " + String.valueOf(f.format(disvalue1 + disvalue2 + disvalue3)) );
String , StringBuilder and StringBufferStringBuffer is thread safe class and both classes are in System.Text namespace. You can convert any object to String using toString method. e.g. object.toString(""#,##0") StringBuffer is thread safe class and both classes are in java.lang package. You can convert any object to String using String.valueOf(object) method
Regular expressionsUsing regular expressions, we can check if the specified pattern is found in the given source string. If pattern is found multiple times, we can find all occurences or groups using matched collections. We can also replace the matched text with other text. System.Text.RegularExpressions namespace contains Regex, MatchCollection, Match and GroupCollection classes to work with regular expressions java.util.regex.Matcher and java.util.regex.Pattern classes can be used to work with regular expressions
Date, time parsing and formattingDateTime is a struct in C#.net Calendar (java.util), Date (java.util) and SimpleDateFormat (java.text) classes can be used to work with date and time in Java
MathsSystem.Math class can be used to do maths operations in C#.netjava.lang.Math class can be used to do mathematical operations
Collections and GenericsSystem.Collections and System.Collections.Generics namespaces provide support for managing Hashtable (implements System.Collections.IDictionary) , SortedList, ArrayList, BitArray, Stack, Queue type of collections. Using generics, we have 2 benefits - type safety and performanceJava.util package provides support for various types of collections like Treeset, LinkedList, ArrayList, Vector, Stack, priorityQueue. Map, TreeMap and HashMap can be used for dictionary type of data and map is not a part of collections library in Java. Map does not implement Collection interface.
File handlingSystem.IO namespace provides support for working with files and directories in C#.netjava.io and java.nio packages provide support for working with files and directories in Java
Object Quering apiIn C#.net, we can use LINQ for quering objects using sql like syntaxIn Java 8, we have stream api (java.util.stream package) which can be used to query the objects. Other way is to use http://www.jinq.org/
Passing params By default, all parameters are passed by value in C#. ref, out keywords can be used to pass value by reference in JavaJava doesn't have ref and out keywords. so you can not pass value by reference in Java. You can only pass by value in Java.
Boxing and casting In boxing, valye type is changed to reference type and variable is stored from stack to heap area of memory. In unboxing, reverse process happens. In casting, one reference type is changed to other reference type. Only heap memory is used in castingSame as C#.net
Name conflict managementNamespaces are used to avoid name conflicts. Deployment is done using assembly files also called as .dll filesPackages are used to avoid name conflicts. Deployment is done using jar or war files
Build management toolsMSbuild and MStest are used to build the solution and run the tests. Dotnet CLI tools are also used for build management in dot net coreMaven or gradle is used as build management tool. JUnit or testNG is used as a unit testing framework
Restrict inheritanceusing sealed classes, we can restrict class from getting inheritedUsing final classes, we can restrict class from getting inherited in Java
IDEVisual StudioIntelliJ, Eclipse and Netbeans
Complex problems, Simple Solutions