You change what you think when moving between Java and C #

This is a question for those who have the pleasure of working in both Java and C #.

Do you find that you need to make some kind of mental context when you move from one to the other?

I work in both at the moment, and because the syntax and libraries are so similar and subtly different, I find it disappointing when I switch from one to the other.

This is more than I experienced the transition between any other programming languages.

Does anyone have any tips for making your brain work differently for languages ​​that are so similar?

+6
java c #
source share
5 answers

Yes, I need to make a mental context switch - because LINQ is not available in Java :(

In addition, there are few things, like foreach (X x in y) vs for (X x : y) , which often drag me in, but not a huge amount otherwise.

As for the hint, to make your brain work differently: Resist the temptation to use naming conventions in another language. I find that using camel-based label names in Java is a good boost to my brain, for example.

EDIT: Differences in terms of generics are important and can sometimes be a perversion of the brain.

+6
source share

I find that most complex switches are around generics (which C # does better), delegates (which are Java hacks with anonymous inner classes) and events (which Java don't have)

I believe that making sure you use idioms of the language (i.e. using delegates instead of creating an interface in C #) will help you switch to transferring.

+3
source share

Differences in languages, as others have mentioned, are the most obvious and problematic. I also find that the differences in libraries are crazy at times and really waste a lot of time.

The "details" can really help you, for example, understanding how the memory model works, or how to optimize execution. With in-depth knowledge of the runtime memory model, garbage collection methods, streaming model, etc., you can create significant changes in the way you think and develop software.

I cannot intelligently compare Java vs C # data at this level, but I can say that many of the things that I will do in Java are uncomfortable or uncertain if I can do this in C # because I do not understand its low-level details, This affects the code that I write for everything from the interaction of the graphical user interface with memory management.

I found a better way to deal with the differences in Java and C #, just to think of them as completely different languages ​​- to avoid the trap of "C # and Java are basically in the same language with different class names."

+3
source share

The difference between how string comparisons are compared between Java and C # needs to be remembered, especially when moving from working in C # to working in Java.

In C #, you can compare values ​​in two instances of string with the == operator:

 // C# string value comparison example string string1 = GetStringValue1(); string string2 = GetStringValue2(); // Check to see whether the string values are equal if (string1 == string2) { // Do something... } 

In Java, the == operator compares links (not value) in strings, so you need to use the equals() method:

 // Java string value comparison example String string1 = getStringValue1(); String string2 = getStringValue2(); // Check to see whether the string values are equal if (string1.equals(string2)) { // Do something... } 

Bottom line. When comparing strings in Java, remember that the == operator does not compare equality of values, as when comparing strings in C #. (The same concept applies to the operator != .)

+3
source share

They are so similar that the differences touch me. I learned Java in college and started using C # when I got my first programming job.

C # in .Net 1.1 seemed a lot more similar, but now in C # 3.0 I use delegates, generics, and especially API constructs such as dictionaries, much more than ever in Java. I have used the functional aspects of C # more and more when I work my way through training F # (now I write in this, or OCaml really makes me change my mindset).

+2
source share

All Articles