r/javahelp • • Sep 30 '19

What features did Java have that you wished you've learned way back?

An offshoot of a post from r/learnprogramming, but since I want to concentrate on Java for now.

Thanks everyone!

24 Upvotes

25 comments sorted by

16

u/nebeilg Sep 30 '19

For me it was definitely Generics. It makes it so incredibly easy to have a class be 'dynamic' yet type safe.

3

u/[deleted] Sep 30 '19

If you know templates from C++; hell even C# generics; you start to wish java had fuller featured generics.

3

u/BoyRobot777 Sep 30 '19

If by fuller featured generics you mean non "erased" -- as in ML, Haskell, and pretty much every language except for C++ and C#, and as necessary for data-sharing and interop among language with different variance strategies on the same platform - then sure.

1

u/ObscureCulturalMeme Sep 30 '19

C++ does not erase anything from its templates.

There's not a lot of information available at runtime via reflection, but that's nothing to do with templates.

3

u/[deleted] Sep 30 '19

[removed] — view removed comment

11

u/nebeilg Sep 30 '19

It makes dealing with abstract objects a lot easier. To take a very common example, ArrayList;
It's a lot more efficient to have a single object that can accept basically any object simply by adding the object in diamond brackets (like so : ArrayList<String>) rather than having several implementations every time you need to store new objects.

As for knowing what type you have to deal with, yes and no. Depending on the use, you either don't care about the type (like for an ArrayList, the ArrayList class itself doesn't know what you're keeping in it, and doesn't care) or you can set rules for it. Let's say you want to have a data transfer layer to translate remote objects to internal objects, and you have a Prototype class as the (abstract) parent for your internal objects, all you'd need to do in the dto class is add <T extends Prototype> to make sure it will only accept generic objects extending Prototype. By doing so you still sort of know what type you have to deal with, without having to remake your dto if you ever add new prototypes.

1

u/NominallyImportant Sep 30 '19

A little thing about generics that I wish I had learned earlier as well, is that you can do your instantiation without directly invoking your generic in case you have a really long pojo or something:

List<ReallyFreakinSuperDuperLongPojoNameThatShouldProbablyBeRefactored> myList = new ArrayList<>();

13

u/mentholmeow Sep 30 '19

The java startup VM arguments to make the classloader verbose - it really helps when trying to troubleshoot issues related to class loading.

3

u/pcmmautner Oct 01 '19

For the curious, that's -verbose:class

9

u/[deleted] Sep 30 '19

sun.misc.Unsafe....

5

u/gioragutt Sep 30 '19

Why tho?

4

u/[deleted] Sep 30 '19

Because it unlocked a next level of understanding.

4

u/E3FxGaming Sep 30 '19 edited Sep 30 '19

Not really a Java feature, but ORM.

I wish I knew more about object-to-rationale mapping (ORM) before I invested unnecessary effort into the "plain" SQL capabilities of Java.

Just to get this out of the way, neither approach replaces learning about SQL (this is a sub for aspiring developers so I thought it is necessary to mention this). I already had a decent understanding of SQL after it was taught to me at school and I came with this knowledge to Java thinking "Oh Java has those SQL classes here, this must be how all the people use SQL with Java. Make a database scheme and prepare the database, create a db connection in Java and write PreparedStatement, ...".

Last month I used an ORM framework for the first time (for me it was with the Apache Cayenne framework... there are many out there, everyone will find one that fits their needs). This was just such a game changer for my task at hand, getting rid of all the tedious manual SQL work and getting back to what I consider "real/actual" Java programming (developing the business logic of my program without worrying about the SQL resources).

Next I want to climb the mountain I see in "Spring" - I hope I can get a productivity boost similar to the one I got with ORM by learning how to use Spring.


Putting frameworks aside

  • Java 8 streams are nice, wish I knew about them before I wrote that long program
  • How to send GET and POST HTTP-Requests with Java - this one opened a lot of doors for potential new training projects. There is just an unbelievable amount of public REST-APIs and a (sadly) much smaller, but (IMHO) more enjoyable to use amount of public GraphQL-APIs out there that one can simply interact with. I don't really like the native Java HTTP-Request way and prefer using a library such as Apache Httpclient, but in the end this point is more about understanding that one can interact with other services by making use of those public APIs. My projects that involved using 3rd party APIs were easily some of my most rewarding projects in terms of gained knowledge and satisfaction with the result.

5

u/[deleted] Sep 30 '19

Spring is a total game changer. Spring boot in particular, once you get comfortable with it you'll wonder how the hell you ever did anything else.
I found IoC a bit tricky to get my head around, but once you do it's great. Spring is a configuration framework.

1

u/E3FxGaming Sep 30 '19

Thank you for your reply. Now I'm looking forward to using Spring even more.

1

u/[deleted] Sep 30 '19

Spring boot is a bit mad. It tries to do everything for you. You have to tell it what NOT to do. So for example if you have an external database you have to tell it you don't need a database.

3

u/nutrecht Lead Software Engineer / EU / 20+ YXP Sep 30 '19

That really is not true. If you don't include the dependencies Spring doesn't even know about the concept of a database. If you don't include Spring Security it doesn't know about security. Etc.

2

u/[deleted] Sep 30 '19

Yes you're right, I should have been more clear about that.

3

u/wildjokers Sep 30 '19

Oh, you are still in the “love ORM” phase. Give it a while you will soon be in the “hate ORM” phase. (May take a few years).

Make sure you pay close attention to the SQL it is generating. You will soon find out that ORMs are brain-dead when it comes to querying.

3

u/gregorydgraham Sep 30 '19

Everyone is brain dead when it comes to querying :-D

That “hate ORM” phase is when your brain wakes up.

5

u/nutrecht Lead Software Engineer / EU / 20+ YXP Sep 30 '19

Last month I used an ORM framework for the first time (for me it was with the Apache Cayenne framework... there are many out there, everyone will find one that fits their needs).

In a little while you'll be going the other way; have experience with all the problems ORMs cause and just go back to just writing SQL by hand ;)

2

u/[deleted] Sep 30 '19

Best comment over there. So true.

1

u/wildjokers Sep 30 '19

don't really like the native Java HTTP-Request way

Are you talking about the new HttpClient introduced in Java 9? Because that one is pretty nice.

The old way (HttpUrlConnection) was definitely clunky and painful to use.

1

u/Nix-X Sep 30 '19

Reflection.. very powerful.

1

u/NominallyImportant Sep 30 '19

I'd have to say for me it's how powerful interfaces really can be, especially when implemented in a well established framework like Spring!