Interesting and Cool Tricks in Java


Java is a widely used programming language available nowadays. It serves to develop a variety of software including web and mobile applications. It is also preferable when it comes to developing a backend system. Java has made tremendous progress over the year that has changed the world. This is the reason why the demand for Java developers is still in the market.

Being a Java developer, one might be interested in learning some cool tricks that can make the code more elegant, efficient and fun. In this article, we are going to share some useful tricks that we can use in our daily work. These tricks will demonstrate the power and flexibility of the Java language.

Interesting and Cool Tricks in Java

Here is the list of some interesting and cool tricks in Java −

Importing a Package in Java

In Java, a package is a container that contains a group of related classes, interfaces and annotations. To use these utilities of packages, we need to import them into our program. Suppose, we need to import multiple classes from a single package, instead of importing them one by one we can also import them using '*'.

Instance

If we have to work with ArrayList, TreeSet and TreeMap classes of Java Collection Framework, some of us will import them using the following way −

import java.util.ArrayList; 
import java.util.TreeSet; 
import java.util.TreeMap;

Instead of the above way, we can import them in one line using the following command −

import java.util.*;

Here, * represents we are importing all the utilities available in the 'util' package.

Note that it is better to import the required packages only.

Commenting in Java

We all know that we can use the '//' symbol for one line comment and '/*…*/' formulti line comment. But, Java provides a way to execute the comments also. Surprised! Let's see with an example.

Example

public class Commenting {
   public static void main(String[] args) {
      // \u000d System.out.println("Executing a comment");
   }
}

Output

Executing a comment

Explanation

The comment in the above code is executed because of the Unicode Character '\u000d' as it signifies a new line. When the compiler reaches that comment it parses the '\u000d' as the next line and in the next line it encounters the 'println()' method.

Writing main() method in Java

The Java programs start execution when JVM calls the main() method. From day one, we follow the convention “public static void main(String[] args).” But, there are other variations also of writing main() method.

Variation 1

We can use the square brackets '[ ]' after 'args' also as demonstrated in this example.

Example

public class Example1 {
   public static void main(String args[]) {
      System.out.println("Tutorialspoint");
   }
}

Output

Tutorialspoint

Variation 2

We can use three dots '…' in the place of square brackets '[ ]' as illustrated in this example.

Example

public class Example2 {
   public static void main(String...args) {
      System.out.println("Tutorialspoint");
   }
}

Output

Tutorialspoint

Variation 3

We can also change the place of public with the static keyword as illustrated in this example.

Example

public class Example3 {
   static public void main(String[] args) {
      System.out.println("Tutorialspoint");
   }
}

Output

Tutorialspoint

Use of Ternary Operator in Java

In Java, the ternary operator can be used as an alternative to the if-else condition in some situations. The ternary operator often known as a conditional operator consists of three operands, used to decide which value should be assigned to the specified variable. By using the ternary operator we can convert our lengthy code into a few liner code.

Example

public class Ternary {
   public static void main(String[] args) {
      int zeros = 0;
      boolean isEqual = (zeros == 0) ? true : false;
      System.out.println("Is zeros is equal to 0: " + isEqual);
   }
}

Output

Is zeros is equal to 0: true

Explanation

In the above example, we have used the ternary operator to check whether the integer variable 'zeros' is equal to '0' or not. Since the value is equal to 0, we got the output as true.

Passing arguments through terminal

The main() method is used along with a parameter named String[] args that accepts String type arguments. It allows us to pass arguments through the terminal and it stores these arguments in an array of strings. We can say that String[] args is a command line argument. Let's see an example that will illustrate how we can pass arguments from the terminal to a Java program.

Example

public class Arg {
   public static void main(String []args) {
      // for each loop to print argument taken from terminal
      for(String arg : args) {  
         System.out.println(arg);
      }
   }
}

Output

To compile the code from terminal type command: javac Arg.java

To run the code from terminal type command: java Arg “Your String”

PS D:\Java Programs> java Arg "Hello, You are on Tutorials Point"
Hello, You are on Tutorials Poin

Conclusion

In this article, we have seen various interesting and cool tricks of Java programming language with the help of example programs. The tricks discussed here make our coding job more fun than ever and also knowing these tricks may also makes you a smart developer.

Updated on: 17-Aug-2023

96 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements