Few Tricky Programs in Java

Confounding Java questions stem from loops, multithreading, overloading, overriding, and more, making them challenging to navigate.

Occasionally, seemingly simple questions confound us, leading to haphazard code instead of straightforward solutions.

With analytical thinking, we can crack these questions even without prior knowledge. Join us as we explore tricky programs in Java.

Methods Used

  • Comments that work

  • Named loops

Method 1: Comments that work

In the realm of programming, Java comments are textual statements within a program that hold no significance in terms of execution by the compiler or interpreter.

The purpose behind incorporating comments into code is multifold. They serve as invaluable tools for enhancing code readability, allowing developers to provide additional insights into the program's intricacies.

By offering clear explanations and pertinent details, comments contribute to the ease of code maintenance and facilitate the identification of errors. Moreover, comments can furnish valuable information about variables, methods, classes, or any given statement within the codebase.

Additionally, they can be utilized as a means to selectively disable the execution of specific sections of code, particularly when exploring alternative code paths during testing and experimentation.

However, let us delve into an intriguing concept today: “comments that execute.”

Program 1

The code prints a comment by using a Unicode character.


Step 1: Declare the "Tutorialspoint" public class.

Step 2: Define the main method as "public static void main(String[] args)".

Step 3: Begin the main method's execution.

Step 4: Include a single−line comment indicating that the next code line will produce output.

Step 5: As a comment, use the Unicode character u000d, which indicates a carriage return.

Step 6: Using the System.out.println() method, print the string "comment has been executed".


public class Tutorialspoint {
    public static void main(String[] args)
		// the code line below this provides an output
		// \u000d
		System.out.println("comment has been executed");


comment has been executed

Note: An interesting attribute of this code is the comment that uses the Unicode character \u000d, which the Java compiler interprets as a new line. In contrast to what is expected of a comment, the following code line is consequently executed.

Method 2: Named Loops

In Java programming, named loops are not an inherent component of the language's standard repertoire. Java's loop mechanisms commonly revolve around conditions and iteration variables to monitor loop progress. Nevertheless, leveraging flags or labels can circumvent the absence of named loops, presenting viable alternatives.


This Java program demonstrates the usage of named loops. It has two nested loops labeled as "loop1" and prints the values of variables i and j. When i is equal to 4, the named loop is broken and the program exits the loop. The output shows the values of i and j for each iteration until the break condition is met.


Step 1: Declare a class named Tutorialspoint.

Step 2: Define the main method as the entry point of the program.

Step 3: Start the outer loop labeled as loop1 with the initialization of i as 1.

Step 4: Check if the value of i is less than 7.

Step 5: If the condition is true, enter the outer loop.

Step 6: Start the inner loop with the initialization of j as 2.

Step 7: Check if the value of j is less than 8.

Step 8: If the condition is true, enter the inner loop.

Step 9: Check if the value of i is 4.

Step 10: If the condition is true, break the outer loop labeled as loop1.

Step 11: If the condition is false, execute the statements inside the if block.

Step 12: Print the values of i and j using the println statement.

Step 13: Increment the value of j by 1.

Step 14: Return to step 8.

Step 15: If the condition in Step 8 is false, you will have to exit the inner loop.

Step 16: Increment the value of i by 1.

Step 17: Now return to step 5.

Step 18: If the condition in step 5 is false, exit the outer loop.


// A Java program to explain how named loops work.
public class Tutorialspoint
    public static void main(String[] args)
	for (int i = 1; i < 3; i++)
		for (int j = 2; j < 4; j++)
			if (i == 4)
				break loop1;
			System.out.println("i = " + i + " j = " + j);


i = 1 j = 2
i = 1 j = 3
i = 2 j = 2
i = 2 j = 3


Java programming frequently includes dealing with difficult challenges like loops, multithreading, overloading, and overriding. These challenges can sometimes result in extensive and convoluted code rather than simple answers. However, with analytical thinking and problem−solving skills, it is viable to overcome these problems even without previous expertise.

In this blog, we explored two methods: utilizing comments for execution purposes and leveraging named loops to achieve more precise control over loop behavior. Developers can enhance code readability, maintainability, and overall program effectiveness by employing these techniques.

Updated on: 11-Jul-2023


Kickstart Your Career

Get certified by completing the course

Get Started