Java

How to select any character across multiple lines in Java

You can do that using the following pattern in the compile static method of the java.util.regex.Pattern class. The pattern is (.|n|r)*? which means: any character (the .) or (the |) n or r. Zero or more times (the *) of the whole stuff. Example: The following method strips the multiline comments (those between /* and */) from a string passed in and returns the resulting string: import java.util.regex; // ... other code // Strip multiline comments public void deleteMultilineComments(String subject) { Pattern pattern = Pattern.

Java passes EVERYTHING by value!

It’s a common misconception thinking (or worse teaching!) that, in Java, primitives are passed by value and objects by reference. Actually, everything in Java is passed by value as well as object references. When a parameter is passed by value, an actual copy of it is really passed so that any change made will have only a local effect. For example: public static void main(String[] args) { int a = 0; increment(a); System.

What happens if you write something like a = a++ in Java?

I often happened to see discussions about this topic. Basically, here is the question. If you have such a code: int a = 0; a = a++; System.out.println(a); What does it print? More than 50% of the programmers will answer 1, some of the remaining will say “I don’t know” and the others will say 0. Well “the others” are right! Provided that such a code MUST NEVER BE WRITTEN, let’s try to understand, for academic purposes, why it prints 0.