Java is one of the most powerful object-oriented programming languages, created by James Gosling at Sun Microsystems in 1991 and publicly released in 1995. Known for its platform independence and interpretability, Java remains a top choice for developers worldwide.
In this article, we’ll explore some cool and lesser-known Java tricks that can make your code smarter and more efficient.
1. Executing Code Inside Comments
Most developers believe comments are never executed — they exist only to explain code. But Java’s compiler interprets Unicode before parsing, which can lead to unexpected behavior if certain Unicode characters are used inside comments.
Example:
public class GFG {
public static void main(String[] args) {
// \u000d System.out.println("GeeksForGeeks");
}
}
Output:
GeeksForGeeks
Explanation:
The Unicode character \u000d
is interpreted as a newline character by the compiler, effectively turning the comment into executable code.
2. Underscores in Numeric Literals
Starting from Java SE 7, you can use underscores to separate digits in numeric literals, making large numbers easier to read.
Example:
public class GFG {
public static void main(String[] args) {
int x = 123_34;
System.out.println(x);
}
}
Output:
12334
Note: The underscores have no impact on the actual value — they’re purely for readability.
3. Double Brace Initialization
Java doesn’t provide a simple native syntax for initializing collections inline. However, using double brace initialization, you can declare and populate collections in a concise way.
Example:
import java.util.HashSet;
import java.util.Set;
public class GFG {
public static void main(String[] args) {
Set<String> topics = new HashSet<String>() {{
add("DS");
add("ALGORITHMS");
add("BLOCKCHAIN");
add("MACHINE LEARNING");
}};
System.out.println(topics);
}
}
Output:
[MACHINE LEARNING, ALGORITHMS, DS, BLOCKCHAIN]
Caution: While convenient, double brace initialization creates an anonymous inner class and can lead to memory leaks if used inappropriately.
4. Finding the Insertion Point in a Sorted Array
If you want to insert an element into a sorted array and need to find its correct position, Arrays.binarySearch()
can help. When the element is not found, it returns a negative value that can be transformed to find the correct insertion index.
Example:
import java.util.Arrays;
public class GFG {
public static void main(String[] args) {
int[] arr = { 1, 3, 4, 5, 6 };
int pos = Arrays.binarySearch(arr, 2);
System.out.println("Element should be inserted at: " + ~pos);
}
}
Output:
Element should be inserted at: 1
Explanation:
The tilde (~
) operator flips the bits, helping to calculate the insertion point for elements not found in the array.
5. Wrapper Class vs. Primitive Data Types
Java treats primitive types and their wrapper classes differently. Using ==
compares memory references for wrapper classes, not values.
Example:
public class GFG {
public static void main(String[] args) {
int num1 = 10;
int num2 = 10;
Integer wrap1 = new Integer(10);
Integer wrap2 = new Integer(10);
System.out.println(num1 == num2); // true
System.out.println(wrap1 == wrap2); // false
System.out.println(wrap1.equals(wrap2)); // true
}
}
Output:
true
false
true
Explanation:
- The first comparison uses primitives, so it checks value equality.
- The second compares object references.
- The third uses
.equals()
, which checks value equality between wrapper objects.
Final Thoughts
These tricks highlight some of Java’s hidden behaviors and advanced features. Whether you’re a beginner or an experienced developer, understanding these nuances can help you write cleaner, more effective code. Keep exploring and experimenting — Java has much more under the hood than meets the eye.
Let me know if you’d like this article turned into a tutorial, blog post, or slide presentation!
Comments are closed