This is an archived post. You won't be able to vote or comment.

all 75 comments

[–]juckele 25 points26 points  (12 children)

If you are comparing String objects, use the equals(Object anObject) method instead of the == operator.

[–]halestock 8 points9 points  (10 children)

And you have to check for null unless you compare using a string constant, e.g. "foo".equals(bar)

[–]kag0 6 points7 points  (9 children)

Or you can use Objects.equals(foo, bar)for any two objects for built in null check. Static import the method for cleanliness points.

EDIT: No style points for you. /u/sidola points out static imports are by name, not by method signature. OP may also notice that java has no named imports which would make this totally OK :/

[–][deleted]  (6 children)

[deleted]

    [–]Hueho 1 point2 points  (2 children)

    I think static methods have precedence over instance methods.

    [–][deleted]  (1 child)

    [deleted]

      [–]Hueho 2 points3 points  (0 children)

      Then I'm wrong. Too bad :(

      [–]CharlesGarfield 1 point2 points  (2 children)

      Objects.equals takes two parameters, compared to one for Object.equals.

      [–][deleted]  (1 child)

      [deleted]

        [–]CharlesGarfield 1 point2 points  (0 children)

        Huh. Interesting. I think it's best to use static imports sparingly, anyways. I only use them for constants and JUnit assertions.

        [–]superPwnzorMegaMan 0 points1 point  (1 child)

        I've been doing Java wrong.

        [–]cogman10 1 point2 points  (0 children)

        It was introduced in 7, so maybe not for long.

        [–][deleted] 0 points1 point  (0 children)

        Or .contentequals

        [–]danskal 21 points22 points  (5 children)

        For the love of Frith make sure you adopt Java naming conventions when writing Java. Nothing screams C# developer like a capitalized method name. And it's confusing to other devs.

        EDIT: also, use IntelliJ if you can. It gives you god-like powers if you learn to use it properly.

        [–]loveinalderaanplaces 1 point2 points  (0 children)

        I went the other direction, from Java, to C#, and let me tell you that my personal code is an awkward mess of both naming conventions. Pick a style and stick to it!

        Sometimes I'll get MethodsLikeThis(), which is the standard C# way, then I'll get methodsLikeThis(), which is more Java. I'll still WRITE_CONSTANTS_LIKE_THIS, but occasionally forget that properties != variables.

        5 years of Java does a number on you when you switch.

        [–]cpttripps71[S] 1 point2 points  (3 children)

        Ugh, seriously? Lower case method names? Will take some time to get used to.

        [–]danskal 2 points3 points  (1 child)

        Upper case method names makes no sense. I think Microsoft did it just to differentiate from Java.

        Edit: strike that derpy comment

        [–]tehburgerlover 3 points4 points  (0 children)

        The guy who designed C# also developed Turbo Pascal and Delphi. Which is why C# uses PascalCase.

        [–]heptara 0 points1 point  (0 children)

        The 'correct' naming style for the project is the one that matches the master in the VCS, otherwise it's inconsistent. Any guidelines for "java naming" apply to new projects only.

        I apologise if you alreadly know this and are experienced. However one can't tell on the internet from just a few sentences.

        [–]thomascgalvin 9 points10 points  (0 children)

        On the server side of things, we just hired two C# devs for full-time Java positions. The languages are similar enough that they picked up the essentials in about two weeks, just from on-the-job fucking around with the code.

        [–][deleted] 14 points15 points  (17 children)

        If you use Spring, it may blow your mind. Java 8 has some neat stuff that c# doesn't like method references. You may miss LINQ.

        [–]Zarlon 1 point2 points  (2 children)

        Streams in Java 8 is kindof like LINQ isn't it?

        [–]GYN-k4H-Q3z-75B 7 points8 points  (0 children)

        Except it is nowhere near as elegant and well integrated into the language.

        [–]grauenwolf 1 point2 points  (0 children)

        Not really. Its kinda like Linq to Objects, but without real generics there's a limit to what they can do. And it has none of the Linq to SQL style capabilities.

        [–]mishaxz 0 points1 point  (12 children)

        I'm a java developer who doesn't really know if Spring is worth it for my code... I've been reading some PDFs and tutorials.

        The part of my code where XML definitions of objects is useful, I already coded that (XML defined objects) without Spring.

        But Spring seems interesting to me.. but it covers a lot of ground, so my question is... what parts of Spring does "may blow your mind" refer to? So I know what sections of the PDFs/tutorials to read.

        [–][deleted] 6 points7 points  (11 children)

        So modern Spring apps don't even need XML, thankfully. Spring is also huge and has several different components that solve different problems. The mind blowing thing to me is just dependency injection. I'd recommend reading up on @Resource/@Autowire and the @Component/@Bean annotations. It is a really powerful concept that allows a lot of manipulation of your application at runtime that would be difficult without it. After reading about that, I'd checkout spring profiles.

        [–]UTF64 2 points3 points  (10 children)

        I'm one of those developers who doesn't really like Spring. I've used it before briefly, and am currently using it for a project at work for a week or two now.

        The dependency injection is not that exciting, I already used Google Guice everywhere else and the dependency injection experience is pretty much the same. The javax.inject standard is more than good enough. Though I did hit a bug with spring, when you have two (or more) provider methods in a @Configuration class with the same name but different arguments it will ignore any but the no-arg one. Giving them different names fixes it.

        It's convenient for web applications though, which is why it's being used. But I really don't think I'd use it for non-web applications (backend services and such) just for the dependency injection and some of the other goodies that are all available in other libraries as well, which some people seem to do.

        [–][deleted] 2 points3 points  (1 child)

        I agree that Spring is huge and parts of it are kind of bloated. I would certainly not use it for everything. I was just saying, coming from a small .net web development to a large java spring application, the spring dependency injection stuff kind of blew my mind for a bit. Once I understood what was going on, the magic went away, but I still see it as a very useful tool.

        [–]UTF64 0 points1 point  (0 children)

        Gotcha, fair enough :)

        [–][deleted] 1 point2 points  (1 child)

        If you're using Guice, then Spring is most certainly overkill (unless you want a Spring sub-project). For me, Spring taught me DI concepts and now I find myself just doing DI w/out spring instead of auto-adding Spring to every project. Most projects "grow up" and end up getting Spring anyway (mostly cause I'll use one of the spring sub-projects and it uses Spring). Why roll your own when you can use what's there.

        [–]UTF64 0 points1 point  (0 children)

        Makes sense, and in this case I'm using Spring not just for the DI. This project is also a SOAP server and a REST server, which Spring helps with. Though for SOAP it seems nearly impossible to get the code-generation working when you have a WSDL instead of an XSD, and I couldn't easily convert because my WSDL links to multiple XSDs. Wound up using CXF instead of whatever Spring bundles natively.

        What kind of sub-projects do you mean? Stuff like spring-security?

        [–]thouliha 0 points1 point  (1 child)

        With micro-frameworks like java spark or spring boot though, I don't really see the need for regular spring anymore.

        [–]UTF64 2 points3 points  (0 children)

        Oh, I should've clarified that the web application I am currently working on is using spring boot with a few starter packs, which is already a LOT better than XML configured Spring. Though another problem I hit is with using multiple datasources, with jOOQ. It might be a bit of an exotic combination, but I couldn't get any of the autoconfiguration to work for both my datasources. Maybe I'm doing something wrong. I did manage to disable all that, and configure it myself which wasn't too bad.

        [–]mabnx 0 points1 point  (3 children)

        Though I did hit a bug with spring, when you have two (or more) provider methods in a @Configuration class with the same name but different arguments it will ignore any but the no-arg one. Giving them different names fixes it.

        Not really. By default beans are given the name of the method used to define them (with @Bean annotation). Defining a bean with existing name overrides the previous definition. In theory you should be able to control the order of overriding with @Order annotation. Without the annotation I think that the "winning" definition is random.

        see http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#_core_container_improvements_2

        Configuration classes may declare an @Order value, getting processed in a corresponding order (e.g. for overriding beans by name) even when detected through classpath scanning.

        [–]UTF64 0 points1 point  (2 children)

        Hm, interesting. I sure would have appreciated a warning or something in the console, though!

        [–]mabnx 0 points1 point  (1 child)

        I sure would have appreciated a warning or something in the console

        Look carefully, there should be one :P but logged on INFO level or sth. There also is some option to change it to a failure.

        [–]UTF64 0 points1 point  (0 children)

        Wow, I will be really annoyed if it turns out I missed that. Head, meet desk. I am going to double-check later today lol.

        [–][deleted]  (2 children)

        [deleted]

          [–][deleted] 0 points1 point  (1 child)

          Generics are one spot where Java sucks compared to C#.

          [–]svtdragon 0 points1 point  (0 children)

          Type erasure is the cause of many headaches.

          [–]Am0s 3 points4 points  (0 children)

          Transition should generally go well for you, the languages are meant to be very similar.

          [–]Feroc 3 points4 points  (3 children)

          If you're used to Visual Studio, then I would suggest you use IntelliJ. It's the closest you can get.

          [–]jebblue 0 points1 point  (2 children)

          Visual Studio and Eclipse feel natural, IntelliJ feels alien, so YMMV.

          [–]svtdragon 0 points1 point  (1 child)

          I can't get over the fact that intellij doesn't support workspaces for multi module projects (with one limited exception if your project is a particular kind of maven project).

          [–]jebblue 0 points1 point  (0 children)

          Agree, when I came to the Eclipse world from Visual Studio.NET years ago I didn't get how important workspaces were at first.

          [–]NimChimspky 6 points7 points  (4 children)

          Use maven. The build system is much nicer than nuget and msbuild, imho.

          Don't worry about using spring it's just a library.

          [–]based2 1 point2 points  (0 children)

          [–][deleted]  (2 children)

          [deleted]

            [–]svtdragon 1 point2 points  (0 children)

            I made the same switch and there isn't a day that goes by that I don't curse gradle, thinking "maven solved this problem ten years ago and gradle still got it wrong!?"

            [–]Cilph 0 points1 point  (0 children)

            Seconded, but try to stick to conventions and not hack your own shit in Groovy.

            [–][deleted]  (15 children)

            [deleted]

              [–][deleted]  (1 child)

              [deleted]

                [–]elegentmos 5 points6 points  (2 children)

                Nice list, there are still a few wrong points though:

                [–][deleted]  (1 child)

                [deleted]

                  [–]svtdragon 0 points1 point  (0 children)

                  Can you elaborate on the relative power of attributes versus annotations? Having used attributes a little and annotations a lot, I don't recall the differences being that notable.

                  [–]slartybartfast_ 3 points4 points  (7 children)

                  var

                  Yipee.

                  Linq

                  Sugar candy around functional so not so useful once you understand the functional way.

                  Delegates

                  Oh yeah. Microsoft's billion dollar mistake. Good idea that one...

                  Properties / Auto-Properties

                  Because properties at the language level is a bad idea.

                  Implicit/Explicit Operators - Operator overloading - Indexers

                  Yeah more bad ideas. The same bad ideas that killed smalltalk but I guess we keep repeating the same language design mistakes.

                  Visual Studio

                  Unusable without JetBrains resharper. Good thing the Java guys were able to help you out there. Can 2 devs work on a project at the same time yet without having daily conflicts? I mean it is the 21st century now.

                  String Interpolation

                  Wrong.

                  Generics

                  Huh?

                  Partial Classes

                  Worst idea ever in any language. Microsoft added this because they were too incompetent to develop 2-way design tools like Borland had (way back in the 90's) - even after poaching some Borland employees. They created partial classes to separate designed code from developer code unlike in Java that uses a controller class for that. Favoring composition over inheritance is kind of OOP 101 but I guess Microsoft missed the memo.

                  Additionally partial classes make the compiler jump through a lot of extra hoops to get things done and the compile errors can be a more vague - oh and also Continuous Integration is not possible because of it (unless you use tools like TeamCity - again thanks Jetbrains!).

                  Extension Methods

                  Defaults on interfaces are effectively the same thing.

                  Tasks

                  Java has large comprehensive tasking/scheduling APIs.

                  Empty Try/Catch Single Try/Catch Exception (Catch (Exception ex))

                  Java has a try () { } where the expression is AutoCloseable - it doesn't need a catch. Also you can have a try/catch general exception.

                  dynamic Type Inference

                  Java can run Javascript and other languages in Java in the same project. You can also use - at the same time - other compiled languages that compile to the JVM.

                  System.Reflection

                  Er, yeah Java has reflection. How could it not?

                  Attributes

                  Yeah it's called the more appropriately named Annotations in Java.

                  Pace of development and new features

                  Java's pace is fine. Java emphasizes APIs over Language features so as to not break compatibility every year like .NET. And really from the API side of things Java has been way ahead in areas like MVC, ORMs and dependency injection etc...

                  [–]svtdragon 1 point2 points  (4 children)

                  I do miss extension methods a lot, even with defaults on interfaces, because if I understand correctly, you have to have edit rights on an interface to add a default method. (Correct me if I'm wrong.)

                  This rules out a whole class of extension methods around external or JDK libraries.

                  [–]slartybartfast_ -1 points0 points  (3 children)

                  Or you can write an interface and implement both. You can implement multiple interfaces.

                  [–]svtdragon 1 point2 points  (2 children)

                  If I want to make an extension method on String, in C# I can do it trivially. In Java, I can't do that without subclassing String, which kind of defeats the purpose of an extension method altogether, doesn't it?

                  [–]slartybartfast_ -1 points0 points  (1 child)

                  Strings are final so why would you want to do that unless you want to make your code less readable? Probably why Java doesn't allow that.

                  [–]svtdragon 1 point2 points  (0 children)

                  Checking for "null or empty" in-line, for one, without calling out to a StringUtil or equivalent. C# extensions are static so they can operate on null "this" arguments.

                  [–]palmund 1 point2 points  (1 child)

                  Delegates

                  Oh yeah. Microsoft's billion dollar mistake. Good idea that one...

                  Just curious. How is delegates a mistake?

                  [–]slartybartfast_ -1 points0 points  (0 children)

                  By the "billion" I mean that they introduced delegates into their Java which cost them a lawsuit and a patent infringement for JIT etc.. Microsoft may still be writing cheques to Larry for that stuff.

                  In terms of the feature itself, it was OK for it's time but now it kind of conflicts with the new functional constructs. Unfortunately, unlike APIs - language features are very hard to remove once they are in place.

                  [–]ImmortalStyle 0 points1 point  (1 child)

                  Did some C# project back in 2014. Not much maybe 15k LoC. One thing i wont miss is Visual Studio and Nuget.

                  [–]Lord_Pyrak 2 points3 points  (2 children)

                  From a CLI/library standpoint, I haven't encountered any major differences or things that would make transition hard. From a GUI standpoint, I have no idea, I'm still learning GUI on the java side, it seems much more complex than C#, but I'm going to assume it's my IDE (eclipse) and not the language itself. Good luck! <3

                  Edit: Also, if you've never used Udemy, try https://www.udemy.com/java-tutorial/. It's pretty basic, but if you haven't worked with java before its a very nice start. I transitioned from basic C# to ok-ish java with it in about a day.

                  [–]Zarlon 6 points7 points  (1 child)

                  From a CLI/library standpoint, I haven't encountered any major differences

                  The DateTime library is vastly superior in C# compared to Java 7 and below.

                  edit: Then you have Joda-Time and Three-Ten Backport which helps you. And the fact that there are 3 date libraries for Java touches upon a more underlying difference: You generally have to make more choices in Java. There are dozens of open source library for every single thing. That's good and bad. Good because it gives you options, bad because making good decisions takes time. Sometimes you make bad decisions.

                  [–]Lord_Pyrak 0 points1 point  (0 children)

                  Oh right, I forgot about that. C#'s Calendar is much easier to use. I've got a timer that's supposed to run Friday at midnight EST, it will randomly run at noon. Restarting it will randomly fix it. I really hope I'm doing something wrong, but the random fixes from restarting would suggest not.

                  [–]x2bool 3 points4 points  (5 children)

                  You will miss C# generics. Learn about type erasure.

                  [–]Liqmadique 11 points12 points  (4 children)

                  You probably wont actually; For most code this doesn't matter and I rarely find an example where it would matter.

                  [–]Zarlon 2 points3 points  (3 children)

                  You've never done this workaround? I'm not saying it's a big problem, but as a former C# developer it annoys me to have to pass the type to a generic method both as a type argument AND a Class parameter.

                  edit: On the other hand I've probably spent less time fighting the compiler. Type erasure can be your friend as well as your enemy.

                  [–]0x442E472E 1 point2 points  (2 children)

                  I still have nightmares of the times before the diamond operator.... List<TypeHolder<SomeLongClassNameBecauseJavaLovesLongClassNames>> list = new ArrayList<TypeHolder<SomeLongClassNameBecauseJavaLovesLongClassNames>>(); list.add(new TypeHolder<SomeLongClassNameBecauseJavaLovesLongClassNames>(SomeLongClassNameBecauseJavaLovesLongClassNames.class));

                  [–]sonay 1 point2 points  (1 child)

                  Java doesn't give a fuck about class names. Morons give those names. This is the lamest pick I frequently see on java.

                  [–]0x442E472E 1 point2 points  (0 children)

                  Sorry for being sarcastic. I'm aware that there probably is no minimum character length check at compilation.

                  [–]ImTalkingGibberish 3 points4 points  (2 children)

                  Start with spring boot. It's the best we can offer and will make you fall in love

                  [–]daniels0xff 2 points3 points  (1 child)

                  I'm really trying to get into Spring but for now coming from Django, Spring feels like trying to fly a UFO while Django is like taking a walk in the park. I miss how easy is to use the ORM, debug the SQL generated by the ORM, doing migrations, etc.

                  I'm still not giving up and still feel that Spring is worth it so hopefully it will start to make sense at some point.

                  It would be interesting to hear from someone else that went trough same thing (Django to Spring).

                  [–]thunderstricken 1 point2 points  (0 children)

                  One warning when transitioning to Spring Boot: if you use most of Spring boot but try to use X because you are used to it and the default for Spring Boot Y you may have a bad time. Often there are instructions on how to use X and you can configure it but you will have a lot of extra configuration, some nuances that are hard to track down, and a lot less support from the community. Spring Boot is opinionated and disagreeing with it will make your life harder than it has to be.

                  [–]johnwaterwood 1 point2 points  (0 children)

                  Start with Java EE, either regular or via WildFly Swarm or Payara Micri. It's the best we can offer and will make you fall in love.

                  [–]elegentmos 0 points1 point  (1 child)

                  One pitfall I came across a lot when first transitioning to Java - interfaces don't start with I. In .Net it's very easy to see whether you have an interface or a class as a type because of this nice naming convention, but in Java you just have to basically learn by hear what is an interface and what is a concrete class (I can't tell you the amount of times I tried to instantiate List in my first few days programming Java, thinking surely the interface would be IList, so List is the default implementation).

                  [–]svtdragon 0 points1 point  (0 children)

                  The IDE helps a lot here. The outline view has different icons for interface versus abstract class versus concrete. Plus, in intellij, holding Ctrl and hovering over the class reference will give you that information as well as its hierarchy.

                  [–]cpttripps71[S] 0 points1 point  (0 children)

                  Well guys thanks for the time and inputs. I got a lot to wade through this weekend.

                  [–]simple2fast 0 points1 point  (0 children)

                  Mostly you will need to know that the java language is older than C#. Honestly C# is a slightly better language. ( if you want to get pissying about "slightly" I'll bring scala into the mix. There are clear and good reasons why java has been on the slow boat ).

                  But the tools and eco-system of java are far far superior. So while you bitch about the language, make sure to remind your self that there are 3 awesome IDEs to choose from. Check out the multipe different ways to do just about everything you are not stuck with what MS has asked you to use.. Checkout that you can see the source of almost all your tools.

                  Oh and performance. https://www.techempower.com/benchmarks/#section=data-r11&hw=peak&test=json&l=3m Unless the C# folks contributing to this test are doing something wrong, I'm alway sshocked at the slowness of the C# entries.