r/learnprogramming •

Confused by intermediate Java

[deleted] — view deleted post

0 Upvotes

6 comments sorted by

•

u/tin2426 29m ago

For you situation, I can recommend the book Head First Java. It will not cover the language end to end. But, it will give you the intuition that you can use to answer the why's that you are talking about.

Altough, it is not a reference, so it might not be the best material if you want to optimize for performance in your class. But if you want to understand Java, it's a good one.

Another one that you can use is Core Java 1. It's more encyclopedic, and I feel like it might confuse a beginner. But there is that too. Typically, I use it when I need to learn something specific.

Also, the mandatory part, whatever resources you end up using, make sure to apply the knowledge by writing your own programs.

•

u/dkopgerpgdolfg 34m ago

Wtf how does a lambda work?

Do I use get set for everything?

Do i private everything? Do i protect everything? Do i static everything?

These topics exist for Rust too, which you caim to know. Maybe you need to improve more.

And usually there's no single hard answer. Opinion/style play a role too.

When do i abstract instead of using interfaces?

The difference is a very simply google answer away

Why reflection?

Because it's possible. Why not.

•

u/xarop_pa_toss 23m ago

Ah yes, you have reached the "OOP is bullshit" level. Especially when you know C or Rust like you mentioned, all that added nonsense feels really unnecessary; and to a certain degree it is.

Most people will probably recommend Clean Code by uncle Bob. It became the industry standard book but nowadays a lot of devs think it was a step in the wrong direction.

I use C# instead of Java but it's basically the same thing and the way I got accustomed to it was to actually ignore a lot of features.

  • I almost never use Inheritance. Composition as become the standard I'd say.
  • Interfaces are your friends.
  • Generics are very useful imo. You can write a single parent function that takes any datatype and then you can activate other things according to what was sent in for example.
  • I don't like Lambdas so I don't use them. I understand what they are but I'm confused by them.

What helped make some things click was learning some of the more commonly used Design Patterns. Façade, Factory, Strategy, and others are things you might do naturally in your own code but when you practice them a little bit you'll see that "oh, making this class with different overrides allows me to do X here" for example.

I don't really have a book recommendation on this, except the refactoring.guru website

•

u/Realistic_Speaker_12 23m ago edited 19m ago

You might claim to know C and rust but you probably know only the basics. You should get deeper into whatever language you like. Most concepts transfer really well between languages. If you understand C++ to some degree, your intuition for rust and why the language does things in the way it does it is really solid.

To awnser at least one of your questions: a lambda is nothing different than an anonymous class with a function call operator.

It works the same in probably every programming language. In rust you have closures, in c++ you have lambdas, they are lazy evaluated etc..

Same thing for classes. It’s the same concept for polymorphism everywhere. You have some dynamic dispatch with a virtual table lookup and that’s it….

•

u/MathiasBartl 36m ago

Lambdas are functional, not object oriented.

•

u/Poddster 5m ago

Confused by intermediate Java

Just to set your expectations: this is beginner level java. It's beginner level C and rust too :)

You can claim that intermediate title when you've made a project of 10k LOC.

Im decent with programming languages that are low level, I can use C and Rust,

Perhaps you don't know them as well as you think? :) java is a very C-like language written by old C heads. Take a look at the source for the standard library at some point, especially java.util.concurrency.

A java class is a C struct, except instead of having to write myStruct_doWhatever(myStruct* this, int blah) you can just write doWhatever(into blah) inside of the class.

It's a class on Java OOP + stuff like Lambdas and Reflection, and every time i try to learn something all I can think is 'why?' and I can't produce an answer.

Do I use get set for everything? When do i abstract instead of using interfaces? Wtf how does a lambda work? Why reflection? Do i private everything? Do i protect everything? Do i static everything? And many others...

Frankly, this isn't "Java", this is more "software engineering". It mostly comes down to experience and taste.

Do I use get set for everything? 

I don't, unless I'm explicitly designing a class to be extended, or it's part of library class and I don't control the code if the consumes. But for all code internal to my projects I don't bother with getting and setting, it's pointless noise.

Do i private everything?

Yes. Private everything, until you need to make it something else. Private should have been the default tbh.  protected is the equivalent to C default. Private is marking C module levels variables statric. And public is makeing C module level variables extern. Except they're inside a struct, not module level.

Do i static everything?

I personally static everything that can be staticed. This means it's not dependent on the per -instance state and makes it clear what state changes, and what state persist. Same for functions, I make as many of them static as possible to help see the ones that are actually mutating the state of the live object Vs just crunching numbers etc.