views:

520

answers:

12

I'll maybe have to switch to Java for new project. I have very few experiences about Java, because I've mainly studied and used C#, and I'm afraid of the differences between these two language/platform should likely to cause me many problems.

Which are pitfalls/gotchas I should care about?

A: 

I think your question is subjective. All can't be explained here. I suggest you to read Java Puzzlers, By Joshua Bloch and Neal Gafter. You can learn more and be safe from pitfalls.

venJava
not all pitfalls, but pitfalls which are C# programmer are likely to make, in Java :)
Vimvq1987
@Vimvq1987 - there's no reason to suppose that you won't run into the "Java Puzzlers" pitfalls after switching to Java.
Stephen C
+10  A: 

The one that got me was Java substring args are beginIndex, endIndex while C# Subtring args are startIndex, length. Thats enough of a difference to make it annoying and a good probability of getting index out of bounds either way you switch.

krock
+1 More confusing is the fact, that it's beginIndex INCLUSIVE and endIndex EXCLUSIVE... and that there are some API's found in the JDK which use the startIndex, length approach...
Helper Method
+16  A: 

Here are some important Java gotchas when coming from C#:

See also

Related questions

On some topics listed above:

On general Java gotchas:

polygenelubricants
+1 for the related questions
finnw
+2  A: 

depends on what kind of program you are working on. Wikipedia has this article and it's quite extensive. (also check out the "external links" section at the end) http://en.wikipedia.org/wiki/Comparison_of_Java_and_C_Sharp.

Also, I read this article when I switched from C# to Java http://www.25hoursaday.com/CsharpVsJava.html and it was very useful.

Louis Rhys
+10  A: 

One obvious pitfall is comparing strings with C# style string1 == string2 (Java compares only references) instead of Java style string1.equals(string2).

Another one is that private is the default access modifier in C#, package in Java.

Also ToString() methods are not automatically localized by current culture in Java.

Cloudanger
This is an extension of the fact there are no operator overloads.
Graphain
Wrong. Package-private is Java's default access modifier.
Helper Method
@Helper Method: Oh, sorry. I meant C# had private as default, but not Java. It's now edited.
Cloudanger
A: 
  • You get no LINQ
  • You get no good - looking UI (no WPF)
  • No properties
  • You get dancing Egyptians
  • You get APIs without examples and good documentation

Hm.

Turing Complete
Never found the Java API bad (in fact easier to navigate) but there are definitely less examples. What's this about Egyptians?
Graphain
I'm upvoting this...... even if is a sarcastic low blow.
Mark
@Graphain see http://stackoverflow.com/questions/2349378/new-programming-jargon-you-coined/2801919#2801919
Cloudanger
@Mark: I have to agree with you, including the upvote. I don't know what the issue with apidocs is though, I mean I understand that lack of examples makes things annoying at times but that's mostly because the API writer was lazy.
Esko
Are these true pitfalls? I understand pitfalls as possible causes of bugs. These are just things you have to live with as you can't do any other way.
Cloudanger
@cloudanger: agree with you. Pitfalls should be somethings that "work" wrongly, not somethings that does not even work.
Vimvq1987
True, but I think that a language without functional data access methodology (LINQ), a good UI framework (WPF) and properties is simply not state of the art anymore, it's like having only one leg and one arm.I mean, come on... while in C# one can declaratively convert XML elements into objects (Select - method of LINQ, even things like "ToDictionary", etc...!), the unbelievable amount of work one has to put into this in Java (iterating, doing every little step myself) makes baby FSM cry.And don't get me started about what Java developers seem to think is an acceptable GUI...
Turing Complete
wpf doesnt make a nice looking gui by itself, nor does swing. But wpf has serious problems and there is so much awful wpf stuff out there. if you stick to UX conventions and let graphics guis do the work, wpf can be nice. if programmers do the work, it usually sucks (but at least something flashes and flies around). Its not about the api, its about what you can do with it.
atamanroman
Yes, it's about the API and what you can do with it. There is nothing even remotely comparable to it in Java. And someone who doesn't strictly adhere to MVVM when doing WPF is not a real developer anyway. I tried a lot of this Java - stuff and I didn't like ANYTHING about it, there was not a single thing about it that I thought was made well. Quite the opposite - trifle tasks in C# are complicated and a pain to do in Java, everything feels so open sourceish, so clumsy. And the language, as I stated before, isn't state of the art anymore with the lack of delegates, LINQ and all that...
Turing Complete
+2  A: 

Beware the differences in default access modifiers. Also note that all non-static methods in Java are virtual (unless you mark them as final).

Although it's somewhat out of date, I've found this to be a great reference.

Comparison of C# and Java, by Dare Obasanjo

Winston Smith
`Also note that all non-static methods in Java are virtual.` How I wish C# was like this too
Graphain
+2  A: 

I think this blog covers a lot of things that you are looking for.. http://www.ericsink.com/entries/java_eclipse_2.html

Raze2dust
+1  A: 

The biggest meta-pitfall is to assume that the Java language and libraries behave the same as similar-looking stuff in C#. Do the tutorials, read the javadocs, don't assume ...

Another meta-pitfall is to assume that the fact that you can do something in Java equally as easily/nicely as you can/could in C#. It is not true. Java is a much older language, and mistakes were made ...

And the last meta-pitfall is to think that complaining about stuff being missing / different in Java on SO will get you universally sympathetic / supportive responses!

Stephen C
+1  A: 
  • Java enums are way more powerful/complicated, they are in fact real classes instead of named integers.
  • inner classes in java are more powerful (and they behave different)
  • no delegates, only functional objects
  • the constructor chaining thingy has a completely different syntax in both languages, i tend to fail every time i have to do that in c#
  • Java has extends for subclassing and implements for interfaces, which is quite nice. C# instead relays on a naming convention which says that interfaces begin with an uppercase I in their name. I dont like that convention, since i can never be sure if someone else fails.
  • java autoboxing can bite you in the a**
  • java type erasure does really make things more complicated
atamanroman
You are joking, right?-1, however. You can't be serious.
Turing Complete
you should at least make clear which point you dont like, otherwise i have to assume that you are just trolling.
atamanroman
* taking absence of linq easy * seeing wpf as just another gui framework * api-doc * the ide question * considering egghead - books serious * comparing javadoc to the far superior sandcastle * collections and math are inferior to .net (by far!) * platform independance: what for? for the 0.1% 1337 linux users? ignore the nolifers, seriously... * lowercase methods (about as worse as the "walk like an egyptian thing)
Turing Complete
now your not only joking but you are just ignorant: linux IS important in the backend area, javadoc is much cleaner than these stupid ms help files which wont work if you view them from network shares. sandcastle is nearly not documented and completely unusable without a proper gui. most ppl will agree that there a really nice collections in the java framework and joshua bloch did a nice job there. And "egghead" books are just books you dont understand. Eclipse is an awesome IDE which VS cant stand without external plugins. Btw: I like C# and miss linq in java.Get out of your ms-office-world.
atamanroman
@Turing Complete - this answer is **just as bad as yours**. Both answers are just lists of whinges / counter-whinges about the Java and C# ecosystems. These are not pitfalls in the sense that OP asked the question.
Stephen C
Only one of these items is a gotcha
finnw
-1 from me too, you don't know what a good ide is (and thus don't know much of anything programming related). Edit: Pffft Java more mature, Java doesn't even have real generics..
Blindy
now this answer should fit to the question :P @blindy if you look up what the word mature means, you will see that this statement is correct.Its not necessarily better, but well-prooven and has evolved over time. Thats why some of the features added later are not as good as they could be, but its always easier to learn from others mistakes and start cherrypicking. But i clearly missed the topic here, so i apologize.
atamanroman
A: 

In Java language the objective equivalents of primitive types such as int, char, are not "value types" (e.g. Integer is a reference type). In C# the System.Int32 is a structure.

MarcAndreson