Jesper de Jong

Java Cowboy
Sheriff
+ Follow
since Aug 16, 2005
Jesper likes ...
Android IntelliJ IDE Java Scala Spring
Forum Moderator
Jesper de Jong currently moderates these forums:
The Netherlands
Cows and Likes
Cows
Total received
88
In last 30 days
0
Total given
31
Likes
Total received
1895
Received in last 30 days
4
Total given
1149
Given in last 30 days
5
Forums and Threads
Scavenger Hunt
(keep public parts private until JForum day)
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt
Moderation Tools

Recent posts by Jesper de Jong

403 means Forbidden. Maybe the website requires you to log in before you are allowed to view the webpage. It's also possible that the website detects that you are not a regular browser (maybe by looking at the User-Agent header) and block you, because it doesn't want automated bots to be able to read the page.

If you need to log in, you'll have to do more in your program than just try to read from the page. It's most likely easier to program that using a library such as Apache HttpClient - Java's standard classes for reading webpages are quite underpowered and don't (easily) allow you to do things like logging in on a website.
1 day ago
If you want all the benefits of reactive, async / non-blocking, you'll need to make the whole stack async / non-blocking. JDBC is indeed inherently a blocking API, so you can't build a fully reactive / non-blocking app if you need to access the database through JDBC.

Oracle seems to be working on a new, async / non-blocking database API, which is (provisionally) called ADBA, but there's not a lot of information on it available yet. It will probably take a few years before it's available. Besides the API itself, database vendors will also need to implement non-blocking drivers.
4 days ago
It's a bit hard to say without seeing the whole project, but it's probably not the findBy() call that causes the record to be created in the database, but something else in your project.

Spring Boot automatically configures and does a lot of things automatically, which is on one hand good because it makes things easy and you don't have to write a lot of code to make things work, but on the other hand it makes it difficult to understand exactly what happens when you run the application. One of the things Spring Boot can do is automatically pick up certain files to populate the database when you start the application. For example, if you're using a regular relational database, you can put a file named data.sql in src/main/resources and it will automatically be executed (see chapter 78 of the Spring Boot manual). Maybe a similar mechanism exists for MongoDB and you have a file somewhere that gets loaded into the database at startup.

If you want to know exactly what's happening at startup, you can start your Spring Boot application with the --debug option on the command line, which will cause Spring Boot to write a detailed auto-configuration report to the console, which is useful for finding out what exactly is happening.
4 days ago
Yes, I think it was a hit on the web a few years ago and I've also played it for a while. Did you succeed in getting to 2048?

If you're tired of playing it yourself, you can try writing a program that plays it for you. Some people have used AI to write a program to play 2048.
4 days ago
You correctly understood what "static" means.

The of() method of class LocalDate is a factory method. It's a static method because you don't call it on an existing LocalDate object. Instead, it creates and returns a new LocalDate object.
5 days ago

Pete Letkeman wrote:I did recently see an article stating that extension methods get compiled into static methods when transformed into bytecode.
As a result the more extension methods you have, the more static methods you have, and people have different views on static methods.


If you write Java code with lots of static methods, then you're probably doing something wrong - you're most likely writing non-object-oriented, procedural code. But that is not really relevant for Kotlin extension methods - the fact that the Kotlin compiler happens to implement extension methods under the covers using static methods on the JVM doesn't make extension methods somehow bad.

Extension methods are a way to add methods to existing classes after the fact - or, that's what they seem to be at first sight.

In Java, people often create "utils" classes. Maybe you've written a StringUtils class yourself or used methods from such a class. Several well-known libraries have their own sets of "utils" classes, for example Apache Commons Lang and Spring both have their own StringUtils classes with utility methods for strings.

With Kotlin, instead of creating utility methods in a StringUtils class, you can create extension methods for class String instead. You can call these extension methods on String objects, and calling such a method looks exactly the same as calling any other method from class String:

Extension methods are not really dynamically added to a class - they are instead like any other external method, so they can for example not access the private variables of the class they are designed for.
5 days ago
I've been using Kotlin at work, so far only for a few little tools that I use myself.  I haven't yet used it extensively with Spring 5 yet.

Spring 5 does officially support Kotlin; the Kotlin support in Spring 5 mainly consists of two things: (1) making sure many things are annotated with @Nullable / @NonNull annotations so that it plays nice with Kotlin's null-safe type system, and (2) a number of extension methods to make working with Spring more convenient.

I've found, and I've seen from other projects / people using Kotlin, that extension methods are one of the most useful and powerful features that people pick up once they're going beyond the basics with Kotlin.

I like Kotlin a lot, and I hope I get the chance to use it a lot more in future projects.
5 days ago
It would be a lot easier if you would add an explanation to your code, besides only the title "How can I fix this code?".

What is the code supposed to do? What is the problem - do you get a compiler error, or an error when you try to run it? Or does it run, but produce output that is different from what you expected? If that's the case, then what exactly did you expect, what does it do in reality and how does that differ from what you expected?

Just by looking at it I can already see an error in the very first line: you are trying to import a class named Sec from the package java.util, but that package does not contain a class named Sec. What was your intention with that line? You're not using the class Sec in the code, so it seems redundant.

There are more errors. For example, String Map<String, String> in line 9 and nre HashMap in line 12, and i+=2> in line 16. You'll need to fix those syntax errors first.
6 days ago
The past week there was a lot of fuss about two security problems in Intel and other CPUs. Like with other security bugs that have happened before, these two have gotten their own catchy names and even a logo: Meltdown and Spectre.

Are you worried about these bugs, do you think they will affect you?

Here's a good video explanation of how these bugs exactly work:

1 week ago
It is really not a good idea to prevent Windows from installing updates. Yes, it's sometimes annoying, but getting malware on your computer because you have an unpatched security hole is an order of magnitude more annoying.

See what well-known security researcher Troy Hunt has to say about it here: Don't tell people to turn off Windows Update, just don't
2 weeks ago
You can use Map with the stream API, but not in the same way as for example a List. Here is an example.

There are also special collectors, such as Collectors.toMap(...) and Collectors.groupingBy(...) that will collect the elements of a Stream into a Map.
Not when the type of the variable is Supplier<StringBuilder> because there would be no way to pass the argument to the constructor. A Supplier is a thing that takes no arguments and returns some object (in your example, a StringBuilder).

You could use Function<String, StringBuilder> instead:

A Function is a thing that takes one argument (in this case, a String) and that returns some object (in this case, a StringBuilder) - which matches the constructor of StringBuilder that you want to use.
3 weeks ago
Java is the most popular programming language and isn't going to disappear anytime soon. People will still be using it 20 or 30 years from now.

Our software development industry is changing all the time, and every few months there's a new library, framework, tool or language that's the hype of the moment. This makes many people, especially those who are new in the industry, confused and unsure of what they should choose to learn.
3 weeks ago
In the two examples in your last post, the B means two completely different things in both examples. The fact that they are both named "B" only makes things confusing.

In the first example, B is a concrete class. Ofcourse you can do "new B()" in that case, you're just creating an instance of class B.

In the second example, B is a type parameter of method3. You cannot do "new B()" when B is a type parameter. The reason why you cannot do this has to do with the way that generics are implemented in the Java compiler and JVM; using type erasure. This means that in Java, generics are purely a compile-time feature. The compiler uses generics for more type-safety, but in the actual byte code, the information about the generics is not present anymore. This means that the information about the generic types is not available to the JVM at runtime - the type parameter "B" doesn't exist anymore at runtime. You cannot do "new B()" where B is a type parameter, because at runtime there is no such thing as the type parameter "B". The JVM wouldn't know what type of object needs to be instantiated.
4 weeks ago
The difference is that if you have a type parameter U, you can use that type inside the method; if you use a wildcard, you don't have access to the actual type inside the method (you only know that it is some unknown type that extends Number).

If you need to know the actual type for whatever reason inside the method, then you cannot use the wildcard version.

Take a look at this method, for example. You cannot write this using a wildcard:

4 weeks ago