I have started a module with the Open University where I am to learn OO concepts through learning and using Java. As personal documentation as well as interest for anyone I decided that I would document my journey.
Before we begin, my background for the last 4 years or so has been specifically using c# in my profession, first as an automated tester then as a software engineer so I am familiar with the OO principles and concepts. Though I will admit my theory lacks because I did not go to university or study any CS course previous to the one I'm on now. I have used other languages but Java is not one that I have really touched but being so similar to c# I have heard it talked about many times so I was clued up on at least some of the differences I would face when learning it.
Below are my first discoveries as I embarked on the course.
I want my IDE
The tool of use by the University is BlueJ. And although I can see the appeal for those that are learning OO and Java I'm having a real battle coming from Visual Studio and VS Code. Where is my Intellisense! It feels like a step backwards and an experience that is not relative to the real world.
Collections are a lot harder!
Ok I admit maybe Linq has spoiled me but working with collections seems unnecessarily hard. It seems like there is a lot of code required to just create a new List and iterate through this. Maybe this is just me and I hope that in a few weeks I'll be laughing at this and ranting in the comments about how easy it in fact is.
Where is my entry point
I ran into this when I wanted to create a hello world console app. After a quick search it turns out that itβs just the method
public static void main() {
...
}
and this can be in any class. I guess this can be both a pro and a con.
Always Type
I'm a big user of var in c# and pretty much any keyword that is general and gets the compiler to work out the type. But in Java you must set the Type everytime. Not a big thing and can in some cases be beneficial making sure the variable is definitely the type you expect
String name = "John Doe";
Capital String and Boolean, lowercase int?
From what I seem to be able to gather the types have different casing to each other. In c# it's pretty much all lower case
c#
string Name;
int Age;
boolean IsVerified;
but in Java they are
String name;
int age;
Boolean isVerified
More casing issues
This one I'm used to as I've worked with JS and TS and also just aware of it anyway but method names and instance variable (in c# known as properties) start with lowercase
private String name;
public void setName(String name){
this.name = name;
}
...
Explicit Getters and Setters
I was shocked to be told to have to create and expose getters and setters methods for each instance variable I wanted to expose and manipulate. I much prefer c#'s way of handling this
c#
public string Name { get; set; }
Java
private String name;
public String getName(){
return name;
}
public void setName(String name){
this.name = name
}
To Sum up
It may seem like I'm complaining about Java and perhaps I come across as a c# fanboy but really what do you expect, that's my world and has been for a few years now so it's going to take some adjustment.
I hope to keep documenting my learnings as I delve deeper into the world of Java, after all there is a reason it's still so popular!
Top comments (3)
I can completely empathize with your need for IntelliSense. In fact, when I've had the need to switch to Visual Studio (+ReSharper) and to VS Code (with ample plugins), I've always had the feeling: where is my IntelliSense. That's to say, those of Visual Studio and VS Code at their best, are rather limited compared to what IntelliJ (community and ultimate editon alike) feature out of the box. It's unfortunate that you need to work with a lesser editor for your studies, but do keep in mind that if you were forced to C# in a plain-text editor, that would also not have any bearing on the value of C# as a language or development environment.
Linq is indeed one of the (few) things that the C# environment has done much better that Java (streams in Java are indeed more verbose). With proper intellisense though, writing that code isn't any more effort than writing Linq queries. As with anything: in a professional setting, leveraging your IDE's capabilities is a must; you're not there to write characters, you're there to implement logic, and your IDE tends to be better at low-level logic than you are.
As to explicit type declarations, that's where we can easily get into philosophical discussions. An important part of OOP and its design patterns, is developing against interfaces. Though it saves some characters,
var
makes you not base yourself on interfaces, and (imo, though YMMV) more easily leads to procedural rather than OOP code.With regard to upcase and lowercase types:
With regard to explicit getters and setters, the C# approach does save some characters, but in doing that bypasses the whole point of encapsulations, since there is no real difference with working directly with the fields themselves. For pure DTOs in Java, perhaps have a look at project Lombok which reduces the boilerplate code far beyond C#'s approach (though again, the amount characters are mostly irrelevant when you have a good IDE to lean on).
Good luck with the rest of your course(s) and hope this helps a bit :).
You are the first person I ever encounter to complain about lack of Intellisense in a Java environment, also and the first ever to mention BlueJ (which seems to be an educational environment, outcome of some university project). Please consider giving IntelliJ or Netbeans (or even Eclipse) a try. If you then still see something missing, I'd love to read about it.
Also, Java 10 brings local variable type inference with var into the language. Okay, it's only been some month and certainly won't be in most books, let alone university courses.
With regard to primitive types: you're right to mention it, but I have the impression, that you did not yet come along the fact that for every primitive type (starting with a lower case) there is also an Object wrapper class. Please read up on the concept of autoboxing.
You rightly noticed that Java uses different naming conventions with regard to methods than C#, namely lower instead of upper camel case. That is a convention only, but you should use it, because it is ingrained in the whole ecosystem. It is only a matter of getting used to. I took up C# for a project after 5 years of a lot of Java and I also needed to adjust. A language comes with idioms, this is simply one of many.
I wholeheartedly agree with your issues about getters and setters. The pain goes away with a proper IDE (to fire up code generation in IntelliJ, which for example creates getters and/or setters, hit Alt+Insert). That, or Project Lombok (although, I'm not a fan of too much magic). But C# does it better. Well, it had time to learn from Javas mistakes and some of the best language designers that are active in this industry: Anders Hejlsberg. It also didn't hurt that they had a FP/Haskell expert like Erik Meijers to design LINQ. That is really a well designed API.
With regard to learning idiomatic Java, I'd like to commend the recently released third edition of Joshua Blochs "Effective Java" to you, once you mastered the basics. As a matter of fact, if there is only one Java book you ever read, make it that one.
Thanks for the tips and the comments. I have installed IntelliJ and have had a much better experience with this, my complaint was more the fact that I'm forced to use BlueJ which just mean switching between the two.
On the use of var's, I didn't know that the latest version supports this so I'll have to give that a go, there might also be some extra new functionality that is nice as I think I'm currently working on v8.
I'll be sure to get a copy of that book as well!
p.s. I agree that c# has had a lot of help getting to where it is and if I'm remembering some of the people who worked on java also contributed to starting c#?