Java vs C# for Java newbies



  • My team is made up of C# devs. We've all (except me - I've worked with all sorts of shit) been only C# Enterprise devs.

    Our company in its wisdom has given us a major Java project, covering a couple of RESTful webservices, consuming a RabbitMQ queue, Java services and a whole load more. We'll be using Spring as that is the company standard. And MongoDB as that is the latest buzzword.

    As pretty decent C# devs I do not doubt that we can segue into Java. However, the thing that does worry me is that none of us has ever actually done any Java (aside, again, from me, I've played with Android dev)

    Therefore: please tell me what I should be wary of; what gotchas might bite us on the arse and what other advice you cross-platform guys might have.

    [I would have posted this in the Coding Related Help & Questions category; however the "New Topic" button is disabled there]



  • @scudsucker said:

    [I would have posted this in the Coding Related Help & Questions category; however the "New Topic" button is disabled there]

    That's the old CS category. We have a General Help -> Coding Help subcategory.



  • Fuck. Well, I doubt that the "topic" can be moved, so I guess it will stay here.



  • @scudsucker said:

    what gotchas might bite us on the arse

    I'll go for the simplest ones which always causes Fun:
    Generic type erasure: List<String> isn't a list of strings; It's a list of objects that the compiler will automatically insert casts on.
    The code:

    List<String> strings = getStrings();
    String first = strings.get(0);
    System.out.println(first);
    

    is equivalent to:

    List strings = getStrings();
    String first = (String) strings.get(0);
    System.out.println(first);
    

    This causes issues not just in regular code, but also with Spring versions < 4, as it is unable to see a difference between Product<Foo> and Product<Bar>.
    I've seen Spring give a Product<Bar> instead of a Product<Foo>, for instance, and everything works just fine until much later on when you get a ClassCastException.

    Another simple but annoying one:
    Unless you're dealing with primitive types, == compares that two object references refer to the same instance, similar to comparing pointer addresses in C.
    E.G. "hello" == "HELLO".toLowerCase() is false



  • If you're pulling collections for the keysets,valuesets or entrysets out of a HashMap remember that you can never guarantee the order of those collections ever. If you need to maintain insertion order use a LinkedHashMap or if you need to specify a particular order use a TreeMap and supply a comparator.

    Also remember removing or inserting items into those collections will do unseemly things to the hashmap they're pulled from.

    I've cleaned up variations of this nonsense at least seven times this year and I suspect I'll be doing the same for years to come.



  • OK. So heavy on the unit testing then. Thanks.



  • There are very very very few cases where MongoDB is an appropriate choice. Were you involved in that decision?

    Unless you're making something like Twitter, it's probably not what you want... and if you are making something like Twitter, there's still better options out there.



  • So Mongo was the buzzword about 2 years ago. I'm in the 3rd world (South Africa) and we are always a couple of years behind.

    I've never used Mongo; I don't know the typical use-case, although I'd assume a large-scale, performant system with little in the way of relational data. We are building such a system, although "large scale" perhaps doesn't count. (+/- 5 million users, max - at a wild guess - 20 000 concurrent app users during a large event)

    While I don't want to explain it too much, my parent company has numerous phone apps for subsidiaries. Our task is to set up a generic, company wide system to allow the various marketing people to set up a system to send push notifications. There are a lot of business rules around who can get what notification so we need to keep track of them, and log what got sent to who. The format of the notifications will also differ massively - and as we are sending notifications as JSON why not store them, in a DB, in a similar format.

    To me that sounds like it could either way, either a traditional RDBMS or a document store. We don't need high speed CRUD (we'll be reading off a message queue to actually send)

    Mongo was chosen by our Architect - I can easily argue with him about it if I have sufficient reason. Except I nothing about it, aside from very limited research. Its currently the buzzword at my company, but I do not know what specifically is bad about it.



  • Well a lot of my MongoDB knowledge is out-of-date, so it might be better now.

    MongoDB used to consider a record "securely stored" if it was in a network packet waiting to get sent to a replication server. Which is fine if your MongoDB is just a single server, but if you're not going to do replication you might as well just use something more reliable and proven.

    MongoDB was hot shit a few years ago, then the tables kind of turned in the other direction. It's still fine for creating an application where losing a few records isn't a big deal (example: Twitter), but if you need:

    @scudsucker said:

    There are a lot of business rules around who can get what notification so we need to keep track of them, and log what got sent to who.

    then you need to take a long, hard look at it.

    Also:

    @scudsucker said:

    The format of the notifications will also differ massively - and as we are sending notifications as JSON why not store them, in a DB, in a similar format.

    be sure to take a long, hard look at MongoDB document size limits. Currently it's 16 MB. That's pretty tiny. It might go up in the future, but I wouldn't build a product based on that hope.

    One thing I'll say for MongoDB, not that this helps you, but its C# driver is amazingly complete and bug-free.



  • OK. Quick bit of research later. (a random high-ranked article)

    The best argument for a document store is that each entity is independent and not related to another.

    The best argument for an RDBMS is that entities are shared and have relations.

    I guess we'll be using Postgress or MySQL then,as we care about Users, their Notifications, NotificationType, etc etc. Very much relational.


  • Winner of the 2016 Presidential Election

    @Salamander said:

    This causes issues not just in regular code, but also with Spring versions < 4, as it is unable to see a difference between Product<Foo> and Product<Bar>.I've seen Spring give a Product<Bar> instead of a Product<Foo>, for instance, and everything works just fine until much later on when you get a ClassCastException.

    That's not entirely true - It won't usually cause issues in code (unless getStrings() returns List or List<Object>. Even in that case, you'll get a compiler warning (unchecked cast) when you do that. It still type-checks that lists are lists of the same type or have a superclass of the same type. You'll only run in to issues if you're using something legacy that doesn't do that (written before generics is the usual cause) or if you do stupid crap casting things.

    Spring very well may not play nicely with those though.



  • Further research, from a link in the "is Mongo for Me" thread/topic says a very firm NO to MongoDB



  • It occurs in code which uses reflection, which I've unfortunately seen a fair bit of lately.



  • However long you think it will take you to do something, triple it. Because you won't have the luxury of a sane or usable IDE. It'll crash. It won't have any tools in any place you expect them. Whatever hobbled Intelli-sense they've hacked into it won't work half the time. Chances are it'll fall down hard when you try to start the debugger.

    Then take that time and double it again-- because developing in Java means you'll need the JDE which means you'll end up getting the JRE at some point which will silently install itself into all your browsers which will open a massive attack vector. You'll get malware at least once, and it will wipe out some poor dev's machine. They'll lose their work. Make sure you have regular backups.



  • @Salamander said:

    Unless you're dealing with primitive types, == compares that two object references refer to the same instance, similar to comparing pointer addresses in C.E.G. "hello" == "HELLO".toLowerCase() is false

    Interesting. They don't have string caching, like in C# and pretty much any other language?

    "a" == "a";
    // true
    
    "a" == "A".toLowerCase()
    // false
    

    I... I don't understand.



  • Not exactly, no. They have string.intern() which fetches an equivalent string from a shared pool (or puts it in there and returns itself), however you need to manually call it on any strings you create.
    String literals are always interned by default, which is why you get the weird behaviour in your snippet.


  • Winner of the 2016 Presidential Election

    So... I know this is a 🚎.. But...

    IntelliJ is honestly the best IDE I've used in my time developing. I can't really speak to the other Java IDEs but I've been thoroughly impressed by IntelliJ. The only downside is that (especially on startup) it can get a little sluggish and it can be a little slow on handling huge files.

    The JDK isn't that bad to deal with - just disable java in your browsers, keep the shitty updater turned off, and use apt-get or chocolatey to update it.



  • Gotchas/irritations I've come across as a primarily C# dev.

    1. Classes in Java are virtual by default.
    2. Java doesn't use the IInterface convention so List<String> is IList<String> in C# speak.
    3. Classpath hell
    4. Classpath hell
    5. Classpath hell
    6. Maven vs Ant
    7. Classpath hell


  • @sloosecannon said:

    So... I know this is a 🚎.. But...

    No. It really isn't.

    @sloosecannon said:

    IntelliJ is honestly the best IDE I've used in my time developing. I can't really speak to the other IDEs but I've been thoroughly impressed by IntelliJ.

    That may be the issue. You've never used Visual Studio. It's like saying "Of all the cars I've driven (a beige Camary and a rusty pinto), the Camary is the best". Then someone pointing out that maybe you'd enjoy driving a car with, like, power steering and working brakes.


  • Winner of the 2016 Presidential Election

    I... Have... Used Visual Studio. 2010, 2012, 2013, and 2015. I prefer Intellij.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.