Interesting and Cool Tricks in Java - GeeksforGeeks

07 May, 2020
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:public class GFG {    public static void main(String[] args)    {        // \u000d System.out.println("GeeksForGeeks");    }}Output: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:public class GFG {    public static void main(String[] args)    {        int x = 123_34;        System.out.println(x);    }}Output: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:import java.util.HashSet;import java.util.Set;  public class GFG {    public static void main(String[] args)    {        Set<String> GFG = new HashSet<String>() {{            add("DS");            add("ALGORITHMS");            add("BLOCKCHAIN");            add("MACHINE LEARNING");        } };        System.out.println(GFG);    }}Output:[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:import java.util.Arrays;public class GFG {    public static void main(String[] args)    {        int[] arr = new int[] { 1, 3, 4, 5, 6 };          // 2 has to be inserted        int pos = Arrays.binarySearch(arr, 2);        System.out.print("Element has to be inserted at: "                                                  + ~pos);    }}Output: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.import java.util.Arrays;public class GFG {    public static void main(String[] args)    {        int num_1 = 10;          int num_2 = 10;          Integer wrapnum_1 = new Integer(10);          Integer wrapnum_2 = new Integer(10);          System.out.println(num_1 == num_2);          // Compares reference        System.out.println(wrapnum_1 == wrapnum_2);          // Compares value of object        System.out.println(wrapnum_1.equals(wrapnum_2));    }}Output:true
false
true
My Personal Notes
arrow_drop_upSave
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:public class GFG {    public static void main(String[] args)    {        // \u000d System.out.println("GeeksForGeeks");    }}Output: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.
public class GFG {    public static void main(String[] args)    {        // \u000d System.out.println("GeeksForGeeks");    }}
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:public class GFG {    public static void main(String[] args)    {        int x = 123_34;        System.out.println(x);    }}Output:12334

Example:
public class GFG {    public static void main(String[] args)    {        int x = 123_34;        System.out.println(x);    }}
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:import java.util.HashSet;import java.util.Set;  public class GFG {    public static void main(String[] args)    {        Set<String> GFG = new HashSet<String>() {{            add("DS");            add("ALGORITHMS");            add("BLOCKCHAIN");            add("MACHINE LEARNING");        } };        System.out.println(GFG);    }}Output:[MACHINE LEARNING, ALGORITHMS, DS, BLOCKCHAIN]

Example:
import java.util.HashSet;import java.util.Set;  public class GFG {    public static void main(String[] args)    {        Set<String> GFG = new HashSet<String>() {{            add("DS");            add("ALGORITHMS");            add("BLOCKCHAIN");            add("MACHINE LEARNING");        } };        System.out.println(GFG);    }}
[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:import java.util.Arrays;public class GFG {    public static void main(String[] args)    {        int[] arr = new int[] { 1, 3, 4, 5, 6 };          // 2 has to be inserted        int pos = Arrays.binarySearch(arr, 2);        System.out.print("Element has to be inserted at: "                                                  + ~pos);    }}Output:Element has to be inserted at: 1

Example:
import java.util.Arrays;public class GFG {    public static void main(String[] args)    {        int[] arr = new int[] { 1, 3, 4, 5, 6 };          // 2 has to be inserted        int pos = Arrays.binarySearch(arr, 2);        System.out.print("Element has to be inserted at: "                                                  + ~pos);    }}
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.import java.util.Arrays;public class GFG {    public static void main(String[] args)    {        int num_1 = 10;          int num_2 = 10;          Integer wrapnum_1 = new Integer(10);          Integer wrapnum_2 = new Integer(10);          System.out.println(num_1 == num_2);          // Compares reference        System.out.println(wrapnum_1 == wrapnum_2);          // Compares value of object        System.out.println(wrapnum_1.equals(wrapnum_2));    }}Output:true
false
true
My Personal Notes
arrow_drop_upSave
import java.util.Arrays;public class GFG {    public static void main(String[] args)    {        int num_1 = 10;          int num_2 = 10;          Integer wrapnum_1 = new Integer(10);          Integer wrapnum_2 = new Integer(10);          System.out.println(num_1 == num_2);          // Compares reference        System.out.println(wrapnum_1 == wrapnum_2);          // Compares value of object        System.out.println(wrapnum_1.equals(wrapnum_2));    }}
true
false
true

Articles
GBlog
GFacts
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Analysis of Algorithms | Set 1 (Asymptotic Analysis)
Mutex vs Semaphore
Time Complexity and Space Complexity
Understanding "extern" keyword in C
SQL | Views
Roadmap to Become a Web Developer in 2022
Must Do Coding Questions for Companies like Amazon, Microsoft, Adobe, ...
Socket Programming in C/C++
DSA Sheet by Love Babbar
GET and POST requests using Python