r/javahelp • • Feb 04 '20

Switching from C# to Java

Hi,

can you give me a list of things i should be aware of in the java world? Like really important libraries, concepts etc?

The thing is i know C# really well, but i dont know what i dont know in the java world, so i cant really look it up.

26 Upvotes

12 comments sorted by

33

u/Northeastpaw Feb 04 '20

So I'm a Java developer who went the other way to C# for a time. Here are some notable differences:

  1. No properties. Getters and setters have to be coded yourself or generated by an IDE (or hacked in via some black magic called Project Lombok).

  2. No reference types. Everything in Java is passed by value. However, even though object references are passed by value, the passed in references will still point at the correct object so things work pretty transparently. You can't swap references in a method and see that reflected back in the calling method but that's a very niche use case anyway.

  3. No out parameters. I never understood the use for them anyway.

  4. Huge ecosystem. Coming from Java to C# it felt like there was a dearth of high quality C# libraries and frameworks. The amount of really good stuff in Java can feel overwhelming, but that's complaining about being spoiled.

You will definitely want to bookmark the standard library javadocs. Find out what frameworks and libraries the team you're moving to is using and bookmark the reference documentation for all of them.

Common equivalents for C# frameworks/libraries/tools:

  1. Entity framework -> JPA. Hibernate is the most common JPA implementation but there's also EclipseLink, OpenJPA, DataNucleus. Raw DB access is done through JDBC.

  2. Visual Studio -> Eclipse, IntelliJ, NetBeans. There's a lot of love for IntelliJ. Eclipse is really entrenched and has a lot of plugins for common frameworks. I personally prefer Eclipse but IntelliJ is very cool.

  3. MSBuild/NuGet -> Maven or Gradle. Both handle dependency resolution and pull dependencies from Maven Central. Maven builds are written in XML and are very declarative; if you find yourself struggling with a complex Maven build then you're not thinking declaratively enough. Gradle has declarative elements but allows you to script parts of your build. Gradle has been adopted by Android as its build system so if you're working with Android, learn Gradle.

  4. IIS -> Apache Tomcat, Wildfly, many others. Java application servers package lots of enterprise components like messaging, transactions, and dependency injection with a web container. Note that Apache Tomcat is just a servlet container which means it handles the web part but doesn't provide any of the rest. Tomcat is often paired with another framework called Spring which can provide those enterprise features, albeit in a slightly different way than JavaEE.

1

u/Cell-i-Zenit Feb 08 '20

Thanks for your answer.

Can you maybe point me to general java concepts which are not 1 to 1 comparable to C#?

If someone other would write the question the other way (transitioning from java to c#), i would tell him that he should look into await/async and linq, can you do the same thing for me? :)

As i said i struggle with the things i dont know, since i cannot research them.

1

u/Northeastpaw Feb 10 '20

Unfortunately I don't know enough C# to really give you a great comparison.

I can tell you that Java Streams are kinda-not-really like LINQ. There's no SQL-like comprehension over collections but the Stream API does have filter(), map(), and other methods. LINQ and Streams are more like different approaches at a similar concept.

Java has some concurrency APIs but nothing as simple as async/await. Java has like 64 billion different ways to go about it. CompleteableFuture is the most recent attempt. It allows you to compose multiple tasks into a chain similar to promises in JS land (just with far more boilerplate).

You can use ea-async from EA (yes, the game company) which does some bytecode magic to provide an await pseudo-keyword (really a static method).

5

u/proskillz Some Skillz Feb 04 '20

That's crazy. I've been writing Java since 2005 and have never heard that Java is pass-by-value. I guess technically that's true if you get crazy low level.

For all intents and purposes it is pass-by-reference for objects, especially compared to something like JavaScript.

Also, lombok is amazing black magic that will eventually be included in the Java language, either 13 or 14, not sure which.

4

u/OffbeatDrizzle Feb 04 '20

Pass by reference and pass by value have different meanings depending on who you ask or what languages you've used before. It's like asking someone if a language is strong or weak, dynamic or static...

Java is pass by value in the sense that you can't modify the pointer of the argument in your function - i.e. you can't change what object the caller's object points to. But it's also NOT pass by value in the sense that the object passed in is not copied (if you come from something like VB you'll find that passing an array in byvalue copies the array).

You basically get an unmodifiable pointer to the original object - call that what you want.

1

u/itzNukeey Feb 04 '20

Wait are they actually gonna implement lombok? Its not in 13 I think but I always use lombok so

0

u/[deleted] Feb 04 '20

[deleted]

1

u/proskillz Some Skillz Feb 05 '20

I generally declare all of my variables using the final keyword, so issues like this just never would have come up since I can mutate the object after passing it to the method. Good info though 👍

2

u/mmahowald Feb 04 '20

This is a small thing, but you cant use == on strings in java - it will compare their addresses in memory rather than their content. use StringA.Equals(StringB) instead.

2

u/proskillz Some Skillz Feb 04 '20

Semantically this is correct, but "hello" == "hello" is true in java since they implemented the string pool.

0

u/AutoModerator Feb 04 '20

You seem to try to compare String values with == or !=.

This approach does not work in Java, since String is an object data type and these can only be compared using .equals(). For case insensitive comparison, use .equalsIgnoreCase().

See Help on how to compare String values in our wiki.


Your post is still visible. There is no action you need to take. Still, it would be nice courtesy, even though our rules state to not delete posts, to delete your post if my assumption was correct.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/jayoandrangi Feb 05 '20

The syntax of java becomes very monotonous as you code more and more with it but there are so many little aspects that are important in the entire code itself