Win a copy of Event Streams in Action this week in the Java in General forum!

Paweł Baczyński

+ Follow
since Apr 18, 2013
Paweł likes ...
Firefox Browser IntelliJ IDE Java Linux Spring
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Paweł Baczyński

Piet Souris wrote:then b.m1() is illegal.

If you program to the interface, you won't need an instance method called m1() as such method is not defined in the interface.

Similarly, when you program to interfaces with lists like List<String> list = new ArrayList<>(); you don't expect to be able to call list.ensureCapacity(123);.
2 months ago

Dave Tolls wrote:Indeed, looking at the Arrays source code and it does use its own private class.

And the private class is named ArrayList.

But it isn't java.util.ArrayList. It is java.util.Arrays.ArrayList.

3 months ago
It would help if you shared what you don't get.
5 months ago
It is, however, possible to "leak" the instance into other thread before final variable is assigned.

Be warned. This code is obscure.
It shows why you shouldn't "leak" the this from a constructor and why you shouldn't start threads inside a constructor.

It prints:
[The value is: null]
[The value is: value]

What doubts do you have? You didn't ask any question.

Also, please use code tags.
5 months ago
At the office we discussed if writing an article about client types is a good idea. After some discussion one became clear - good client will laugh at it, lousy client will not read it.

If you have any comments or suggestions, please let me know!

5 months ago

Collie Mason wrote:Thank you! I called the playGame() method in toString(), and it cleared up my issue. However, now "computer" is only being assigned as 1, not somewhere in the range between 1 and the user's input (assigned as "upperBound"). How can I fix this?

I appreciate the suggestion with random.nextInt(), and I may use that in future code.

And as it turns out Math.random() is the cause of your problem. Well, not the method itself, but it is very easy to put parentheses wrong when using it.

(int)Math.random() * ((upperBound) + 1) + 1

Math.random() returns a double greater than or equal to 0.0 and less than 1.0. When you convert such double to an int you always get 0.

0 * ((upperBound) + 1) + 1

Anything multiplied by 0 is 0.

0 + 1

Zero plus one equals one.

6 months ago
This means that DBConnection.getConnection() may return null. If you don't want to see NullPointerException check if conn == null.
6 months ago
Lately, I was discussing with my colleagues where to find useful case studies of how to run a software company.
It turned out to be the beginning of a series of articles on our company blog.

It would be great to take a look and let me know what you think. It is just the intro so that we can adjust it to readers needs.
6 months ago
You can try to change the background color of the Gui instance.
6 months ago
Line 26 in your code.
One should never close a Scanner that is tied to (link)
6 months ago
Wecome to the Ranch!

What security exception are there with == operator?

You shouldn't use == to compare strings.

You coud use a 'reversed' notation to avoid NPE, like: if(!"".equals(message)). I'll let you decide if it looks readable enough.

Writing err.equals("") || err.equals(null) won't guard you against a NPE.
First, you should always check for null first. If err is null then err.equals("") will throw a NPE.
Second, err.equals(null) is an incorrect way to do a null check. It will throw NPE if err is null. Also, a properly designed equals method (String class has one) will never return true for null argument. For a null check you have to use the == operator.
7 months ago
There is no difference. Both lines do the same thing.
7 months ago
You said: "There is already a map class, user-defined for that reason, which includes the method."

You need to create an instance of this class. Look at the code of the class. Does it have public constructors? Static factory methods?
Is it the BST class? Does it implement Map interface?
8 months ago

Does it even compile? You are using a variable that is not initialized.
8 months ago