Interesting and Cool Tricks in Java
Java is one of the best object-oriented programming language developed by James Gosling from Sun Microsystems in the year 1991 and was publicly available in the year 1995. It is an interpreted programming language with platform independency making it one of the best programming language among all.
In this article, we’ll see some interesting and cool tricks in Java.
- Executing Comments: Most of the developers think comments are never executed in a program and are used for ease in understanding the code. But, they are executed. For example:
publicclassGFG {publicstaticvoidmain(String[] args){// \u000d System.out.println("GeeksForGeeks");}}chevron_rightfilter_noneOutput:
GeeksForGeeks
Explanation:
This comment is executed because of Unicode character “\u000d” and java compiler parses this unicode character as a new line. Java allows using Unicode characters without encoding. - Underscore in Numeric Literals: In Java SE 7 and above, underscores can be used in numeric literals without generating any warning or error in the output.
Example:
publicclassGFG {publicstaticvoidmain(String[] args){intx = 123_34;System.out.println(x);}}chevron_rightfilter_noneOutput:12334
- Double Brace Initialization: In Java, collections such as sets, lists, maps, etc. does not have a simple and easy way to initialize the values during declaration. Developers either push values into the collection or creates a static block for the constant collection. Using double brace initialization, collections can be initialized during declaration with less efforts and time.
Example:
importjava.util.HashSet;importjava.util.Set;publicclassGFG {publicstaticvoidmain(String[] args){Set<String> GFG =newHashSet<String>() {{add("DS");add("ALGORITHMS");add("BLOCKCHAIN");add("MACHINE LEARNING");} };System.out.println(GFG);}}chevron_rightfilter_noneOutput:[MACHINE LEARNING, ALGORITHMS, DS, BLOCKCHAIN]
- Finding a position to insert the numeric element in the array: There is a small cool trick to find the position where the requested element can be inserted in the sorted array.
Example:
importjava.util.Arrays;publicclassGFG {publicstaticvoidmain(String[] args){int[] arr =newint[] {1,3,4,5,6};// 2 has to be insertedintpos = Arrays.binarySearch(arr,2);System.out.print("Element has to be inserted at: "+ ~pos);}}chevron_rightfilter_noneOutput:Element has to be inserted at: 1
- Wrapper class vs datatype: In the below example, the second print statement will not display true because reference of wrapper class objects are getting compared and not their values.
importjava.util.Arrays;publicclassGFG {publicstaticvoidmain(String[] args){intnum_1 =10;intnum_2 =10;Integer wrapnum_1 =newInteger(10);Integer wrapnum_2 =newInteger(10);System.out.println(num_1 == num_2);// Compares referenceSystem.out.println(wrapnum_1 == wrapnum_2);// Compares value of objectSystem.out.println(wrapnum_1.equals(wrapnum_2));}}chevron_rightfilter_noneOutput:true false true
- Java tricks for competitive programming (for Java 8)
- Interesting Facts About Java
- Interesting facts about null in Java
- Interesting facts about Array assignment in Java
- Interesting facts about Increment and Decrement operators in Java
- Tricks in Windows 10 | Set -1
- Logarithm tricks for Competitive Programming
- 5 Tips and Tricks To Crack The Hackathon
- 7 Tips and Tricks to Learn Programming Faster
- Tips and Tricks for Competitive Programmers | Set 2 (Language to be used for Competitive Programming)
- Interesting Facts About PHP
- Interesting Facts About CSS
- Interesting Facts about DBMS
- 10 Interesting Facts About Computers
- Interesting Facts in C Programming | Set 2
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

