I’ve been playing around with C# for more than 2 years. Recently got a chance to explore Java.
I didn’t have much idea of Java except that both are quite similar in construct. All I needed to find is the equivalents of C# features in Java.
This post covers the Java equivalent of some of the popular C# features, classes, or methods in brief. I’ll add more to this list in the future. If you feel any important feature is missing, feel free to add a comment.
I assume the reader has some basic to medium programming experience.
C# Linq
Java Equivalent for C# Linq is Streams introduced in Java 8.
Where()
C# Example:
List<User> filteredUsers = users.Where(u => u.Name == "Soundar") .ToList();
Java Example:
List<User> filteredUsers = users.stream() .filter(u -> u.Name == "Soundar") .collect(Collectors.toList());
Skip(), Take()
C# Example
List<User> filteredUsers = users.Skip((page - 1) * pageSize) .Take(pageSize) .ToList();
Java Example
List<User> filteredUsers = users.stream() .skip((page - 1) * pageSize) .limit(pageSize) .collect(Collectors.toList());
More examples on streams are here
Collections
List
C# Example
List<User> user = new List<User>();
Java Example
List<User> user = new ArrayList<User>(); // or new ArrayList<>():
More collection examples are here
String.IsNullOrEmpty()
C# Example
bool hasValue = String.IsNullOrEmpty("");
Java Example: isBlank() as of JDK11
// isBlank() as of JDK11 bool hasValue = (str == null) || str.isBlank(); // for before JDK11 bool hasValue = (str == null) || str.length() == 0;
Null Conditional or Optional Chaining Operator
C# Example
string userCountry = user?.Address?.Country;
Java Example
string user Country = (user != null && user.Address != null) ? user.Address.Country : "";
Null Coalescing
C# Example
int? x = null; //if x is null 0 is assigned else value of x is assigned int y= x ?? 0;
Java Example
int y = (x != null) ? x : 0;
@ – Verbatim Identifier
C# Example
//multiline text string multiline = @"select * from users where id = 1"; //escaping string filePath = @"C:\Program Files\";
Java Example
//multiline String s = "select * from users \n" + "where id = 1"; //escaping string filePath = "C:\\Program Files\\";
$ – String interpolation
C# Example
int value1 = 1; string example = $"We can use variables or values inside string { value1 }";
Java Example
String.format("name=%s email=%s", name, email);
Object Initializer
C# Example
User user = new User() { Name = "Soundar", Email = "example@gmail.com" };
Java Example: In Java, there is no object initializer feature. We need to use the constructor or classic setter methods
User user = new User(); user.setName("Soundar"); user.setEmail("example@gmail.com");
Getter Setter
C# Example
class User { public string Name { get; set; } }
Java Example
class User { private String name; public String getName() { return this.name; } public void setName(String name) { this.name = name; } }
Generics
Java generics is quite similar to C#. For detailed example, check here.
C# Example:
//class public class ExampleClass<T> { } //method public void Method<T>(T param) { }
Java Example:
//class public class ExampleClass<T> { } //method public <T> void method(T param) { }
Type Conversion
C# Example:
//string to int Int16.Parse("100"); //throws exception for invalid input Int64.Parse("2147483649"); //throws exception for invalid input int.Parse("30,000"); //throws exception for invalid input Convert.ToInt16("100") //throws exception for invalid input Convert.ToInt32("100") //throws exception for invalid input Convert.ToInt64("100") //throws exception for invalid input bool isValidNumber = Int32.TryParse("1000", out number); //no exception // int to string int num = 10; string str = Convert.ToString(num); string str1 = num.ToString();
Java Example:
//string to int Integer.parseInt("200"); //throws exception for invalid input Integer.valueOf("200"); //throws exception for invalid input //int to string String.valueOf(200); Integer.toString(200)
For more on type conversions, refer this
I hope the above Java equivalents will be handy and help you get started in Java as a C# Developer.
Happy Coding 🙂