This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests and have Scott Selikoff and Jeanne Boyarsky on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP forum!

Stephan van Hulst

Saloon Keeper
+ Follow
since Sep 20, 2010
Cologne, Germany
Cows and Likes
Cows
Total received
277
In last 30 days
4
Total given
228
Likes
Total received
2941
Received in last 30 days
18
Total given
499
Given in last 30 days
8
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Stephan van Hulst

I don't see any reason not to go with 11. Java is (mostly) backwards compatible, and so is the knowledge you get for studying for a certification.

Don't worry about the industry. I know a scary amount of companies that still use Java 6. This is not something you should emulate personally.
Why are you not using a MVVM framework? Manipulating the DOM with JavaScript manually is pretty old-school.

Anyway, you can do this pretty easily using Element.closest() and Element.querySelector():
The most common (and by this I mean the least rare) use case I personally have for using stacks is when I'm processing a dynamic tree-like model (such as an XML DOM) and I want to access information about the processed ancestor nodes in the method call that processes the current node.

Here's an example (in Java):
11 hours ago
MVC
You're not giving us a lot to work with.

Are you using ASP.NET MVC 5, or are you using ASP.NET Core MVC? What data do you want to transfer from where to where? Are you building a web page or a web API?
17 hours ago
Yes, the book is right, you should definitely strive to make most classes immutable.

The reason you don't often see this in real life is because people are lazy. They don't mind fixing bugs later if it means doing less work now.

When you write a class that only acts as a container of data for Hibernate to fill, you can and should also add another immutable class that you convert your data transfer objects to. Don't perform logic directly on your 'Hibernate classes'. Most people won't do this though, because they don't see the point and they are lazy, but it really helps preventing bugs.
19 hours ago
Pablo, I assume you're going to use matrix and active in the query that you're sending to the repository. If my assumption is correct, can you please give us an example that matches your actual situation a bit more closely?
2 days ago
Don't confuse Java the language with java the command line tool. If you use jshell instead of java, you don't need a main method.

Heck, I recall writing a shell script in Java once, just to prove a point.
2 days ago
I'm with Carey. Why not build your return value in pieces, instead of using string constants?
2 days ago
You can use javac and java from any directory. The trick is feeding the right arguments to the -sourcepath, -cp, -d, -p and -m switches.

To compile, use the -sourcepath to specify the folder that acts as the root package of the classes you want to compile. Use the -cp switch to specify JARs and root packages containing pre-compiled classes that your source depends on. You usually do this when you're writing an application that depends on an existing library.

Use the -d switch to specify the output directory of your root package.

As a final argument to the javac command, you specify the path of the entry points that you want to compile. These paths DON'T take the -sourcepath switch into account! ALL paths that you specify are relative to the current working directory!



Imagine that you have an application that consists of the class com.example.Application, whose sources are located in the MyApplication/src/com/example/ directory, where MyApplication/ is the root folder of your project. It relies on a library dependency.jar, which is located inside MyApplication/lib/. You want to compile it all so that your compiled classes appear in the MyApplication/bin/ folder. Assuming you opened a command prompt inside the MyApplication/ directory, here's what you would do to compile:

Note how even though you used -sourcepath to specify that the src folder contains your sources, you still had to specify your entry point as src/com/example/Application.java. As I said before, all paths you specify are relative to the current working directory. Java uses the -sourcepath switch to look for other uncompiled source code that is used by your entry point recursively.



To run the application, you do the same thing, but since your sources are now compiled, you no longer need the -sourcepath and -d switches, and you add your compiled classes to the classpath:

On Mac, I believe you have to separate the classpath entries using a colon instead:



On Java 9+, if your application is modular you use the -p and -m switches instead:

On Mac:
2 days ago
The algorithms you mention are pretty basic. I doubt there will be many puzzles out there that will require you to write a merge sort specifically. Most puzzles these days rely on you knowing how to use your programming language of choice to sort a list or to look up an object in a hash table. The skills they test are more abstract.

If that's not a problem for you, I recommend the following sites:

  • CodinGame
  • Advent of Code
  • HackerRank

  • If you're mathematically inclined, you might also want to check out Project Euler. The first bunch of problems are solvable through brute force or dynamic programming, but after a while you will need to use some hardcode maths, so I don't recommend it for programming puzzles as much as the other sites.
    2 days ago
    Here's an example that violates statelessness:

    Each time StringBuilder.append() is called, it modifies the state of the StringBuilder instance. This instance is also used when other elements of the stream are iterated over, potentially leading to issues. To be stateless, the operations you pass to the reduce() method must not call any mutators, and should always return the reduced result as a new immutable value.

    The example above is associative though: (("a" + "b") + "c") yields the same result as ("a" + ("b" + "c")).

    Here's an example that violates associativity:

    It is stateless, but it's not associative because ((100 - 20) - 15) yields a different result than (100 - (20 - 15)).
    You get an Optional because the stream might not contain any elements for you to find one from. This happens if pickOrders is empty, if none of the pickOrders have any pickOrderLines, or if none of the pickOrderLines has a quantity that is high enough. Rob Spoor already warned about this.

    Again, what do you want to do when there is no PickOrderLine that matches your criteria?
    4 days ago
    As Campbell pointed out, there's a lot of wonkiness here.

    1) You can't give different equality definitions for sub-classes. Override equals() once and make it final. The implementation should be based on the current class, not sub-classes. Use instanceof, not getClass().
    2) equals() should account for ALL normalized properties of an object, not just a subset. If you want to compare a subset of an object's properties, don't use equals().
    3) bytes are not Privileges. You're comparing apples to oranges.
    4) It looks like you're using Project Lombok. Stop it. Lombok is a terrible substitute for writing proper type-safe code.
    5) Most importantly, don't base your business logic on mutable DTOs. A DTO is only there to facilitate transferring data to and from the database. Convert it to an immutable instance of your business model, which will also contain the equals() implementation.

    Now, even though Privilege has a correct equals() implementation, DON'T use it to compare privileges if you're only interested in the IDs of the privileges. Do this instead:
    5 days ago
    To me it looks like BM_mod8.properties refers to an object of a custom type that extends the Properties class.

    Don't you have access to the source code of the BM_mod8 class?
    6 days ago
    I think what Mike means is, if he has a PEM-encoded certificate file, then why should he go through the trouble of converting it into a JKS/PKCS12 certificate file?

    Honestly, it doesn't really matter what you use. If you already have a PEM-encoded certificate, good for you. Use it and be done with it.
    1 week ago