Java optional parameters

There are several ways to simulate optional parameters in Java: Method overloading.void foo(String a, Integer b) { //… }void foo(String a) { foo(a, 0); // here, 0 is a default value for b }foo(“a”, 2); foo(“a”); One of the limitations of this approach is that it doesn’t work if you have two optional parameters of … Read more

How to round up to the next integer?

No, Math.ceil() won’t work on its own because the problem occurs earlier. a and b are both integers, so dividing them evaluates to an integer which is the floor of the actual result of the division. For a = 3 and b = 2, the result is 1. The ceiling of one is also one – hence you won’t get the desired result. You must … Read more

What is a serialVersionUID and why should I use it?

The docs for java.io.Serializable are probably about as good an explanation as you’ll get: The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If … Read more

C# Java HashMap equivalent

Dictionary is probably the closest. System.Collections.Generic.Dictionary implements the System.Collections.Generic.IDictionary interface (which is similar to Java’s Map interface). Some notable differences that you should be aware of: Adding/Getting items Java’s HashMap has the put and get methods for setting/getting items myMap.put(key, value) MyObject value = myMap.get(key) C#’s Dictionary uses [] indexing for setting/getting items myDictionary[key] = value MyObject value = myDictionary[key] null keys Java’s HashMap allows null keys .NET’s Dictionary throws an ArgumentNullException if you try … Read more

How to remove single character from a String

You can also use the StringBuilder class which is mutable. It has the method deleteCharAt(), along with many other mutator methods. Just delete the characters that you need to delete and then get the result as follows: This avoids creation of unnecessary string objects.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)