Differences Between C# vs. Java | .NET vs Spring Flashcards
- Learn the syntax differences between C# and Java. - Learn the conceptual difference between C# and Java - Learn key difference between .NET and Spring, two of the world's most popular frameworks
Explain the differences between the ‘virtual’, ‘sealed’ and ‘final’ keywords in C# and Java.
How are these keywords treated differently in each language? Explain from an OOP conceptual viewpoint.
C#: - All class methods are 'final' in C# by default. This means that child classes cannot override their methods. This is the default when no specifier is included. - Thus, there is no 'final' keyword in C#, but this terminology is still sometimes used to describe a class method which is not marked with the keyword 'virtual'. Another terminology is 'non-virtual'. - We use the 'virtual' keyword to mark a class method that we would possibly like to be overridden in a child class. - If we strictly do not want a class to be inheritable from, we use the keyword 'sealed'. (Warning: The 'sealed' keyword does not exist in Java!)
Java:
- In Java, every method is assumed to be virtual by default and can be made non-virtual using the ‘final’
keyword.
- In Java, the ‘final’ keyword can be applied to a variable, method or class.
- If we do not want a class to be inheritable from in Java, use the ‘final’ keyword.
Key Takeaways: - Polymorphism is one of the defining properties of Object-Oriented Languages, and it wouldn’t be possible without virtual methods. A virtual method is one whose function can be overridden by any class that inherits it. In Java, every method is assumed to be virtual by default and can be made non-virtual using the final keyword. Conversely, in C#, all methods are non-virtual by default and so a directly equivalent keyword would have no use.
Explain the differences between check and unchecked exceptions in C# and Java.
Which of the keywords are available in each language?
JAVA:
- Java divides exceptions into Checked and Unchecked. Checked exceptions are conditions that are checked at compile time. Certain methods that are likely to throw an exception must be handled using a try/catch block or otherwise must specify the exception with a throws statement
- Java requires us to provide the logic to handle them at compile time. Without one of these required checking statements, the code will fail to compile.
- In Java exceptions under Error and RuntimeException classes are unchecked exceptions, everything else under throwable is checked.
C#: - C# does NOT have checked exceptions, the architects purposefully didn’t include the feature. - C# provides three keywords try, catch and finally to implement exception handling. - Hint: Use the 'throw;' keyword when you want to throw an unnamed exception. - Here are the common exceptions in C#: System.OutOfMemoryException System.NullReferenceException System.InvalidCastException System.ArrayTypeMismatchException System.IndexOutOfRangeException System.ArithmeticException System.DivideByZeroException System.OverFlowException
Explain the difference between Inner Classes in Java and C#.
(Hint: Non-Static Inner Classes)
BOTH:
- Both Java and C# have static, nested classes:
class Outer { static class Inner { void msg(){System.out.println("inner class"); } }
- A nested classes static methods can be accessed without an instantiation of the outer class:Outer.Inner.msg();
- Nested static class doesn’t need reference of Outer class to be instantiated and used, but non static nested class or Inner class requires Outer class reference.
- Java has Non-Static Inner Classes. See below. (C# does NOT have non-static inner classes).
JAVA:
- Has 3 types of Non-Static Inner Classes:
Method-Local, Anonymous, and regular inner class
- One benefit of these (non-static) inner classes is that they can be made private, so that they can only be accessed by an object from the parent class.
- public, private, and protected keywords can be applied to inner classes in Java.
C#:
- C# has only static inner classes. (They are static by default when not including the static keyword.)
- Yes, C# Does NOT have anonymous inner classes. (Yep, it really doesn’t, this surprised me too if you’re used to Java land).
- In C#, an inner class can be marked as: private, public, protected, internal, protected internal, or private protected
What is the difference between optional parameters in C# and Java?
In C# you can write optional method parameters, as well as give them default values:
(Thus, make the value null or another value by default if you would like it to be optional)
//C# void OptionalPrintStatement(string param1, string param2 = null) { Console.WriteLine(param1 + param2); }
In Java you can achieve the same by method overloading:
(Java will pick the correct one based on how many parameters you passed in)
//Java private void Foo(String something) { System.out.println(something); } private void Foo(String something1, String something2) { System.out.println(something1 + something2); }
BONUS POINTS: In Java 8+, there is also now the Optional class:
//Java: public Integer sum(Optional a, Optional b) { System.out.println("First parameter is present: " + a.isPresent()); System.out.println("Second parameter is present: " + b.isPresent());
Integer value1 = a.orElse(new Integer(0)); //returns argument if not there Integer value2 = b.get(); //gets the value of an Optional return value1 + value2; }
How do you find the length of a List (or ArrayList) in Java vs C#?
//JAVA: Use the .size() method: List aList = new ArrayList(); aList.add("Apple"); aList.add("Mango"); aList.add("Guava"); System.out.println("The size of the ArrayList is: " + aList.size());
//C#: Use the Count method: List dinosaurs = new List(); dinosaurs.Add("Tyrannosaurus"); dinosaurs.Add("Amargasaurus"); dinosaurs.Add("Mamenchisaurus"); Console.WriteLine(dinosaur.Count);
Show the differences in looping over an array using every different type of loop in BOTH C# and Java:
- for loop over ArrayList and List
- for loop over primitive array []
- enhanced for loop
- lambda
//Regular for loop over collection:---------------- //Java: for (int i = 0; i < countries.size(); i++) { System.out.println(countries.get(i)); } //C#: for (int i = 0; i < list.Count; i++) { Console.WriteLine(list[i]); } //primitive array is same but use .Length in C# and .length in Java
//Enhanced for loop:--------------- //Java: class Main { public static void main(String[] args) { char[] vowels = {'a', 'e', 'i', 'o', 'u'}; for (char item : vowels) { System.out.println(item); } } } //C#: var fibNumbers = new List { 0, 1, 1, 2, 3, 5, 8, 13 }; int count = 0; foreach (int element in fibNumbers) { Console.WriteLine($"Element #{count}: {element}"); count++; } Console.WriteLine($"Number of elements: {count}");
//Lambda:--------------------- //Java: Arrays.stream(arr).forEach(e->System.out.print(e + " ")); //C#: var dogs = new Dog[]{ new Dog(){ Size = 1 }, new Dog(){ Size = 3 } }; Array.ForEach(dogs, dog => dog.Bark());