Few Tricky Programs in Java - GeeksforGeeks

25 Sep, 2017
Comments that execute :Till now, we were always taught “Comments do not Execute”. Let us see today “The comments that execute”Following is the code snippet:public class Testing {    public static void main(String[] args)     {         // the line below this gives an output         // \u000d System.out.println("comment executed");     }}Output:comment executedThe reason for this is that the Java compiler parses the unicode character \u000d as a new line and gets transformed into:public class Testing {    public static void main(String[] args)    {        // the line below this gives an output        // \u000d        System.out.println("comment executed");    }}Named loops :// A Java program to demonstrate working of named loops.public class Testing {    public static void main(String[] args)    {    loop1:    for (int i = 0; i < 5; i++)     {        for (int j = 0; j < 5; j++)         {            if (i == 3)                break loop1;            System.out.println("i = " + i + " j = " + j);        }    }   }}Output:i = 0 j = 0
i = 0 j = 1
i = 0 j = 2
i = 0 j = 3
i = 0 j = 4
i = 1 j = 0
i = 1 j = 1
i = 1 j = 2
i = 1 j = 3
i = 1 j = 4
i = 2 j = 0
i = 2 j = 1
i = 2 j = 2
i = 2 j = 3
i = 2 j = 4You can also use continue to jump to start of the named loop.We can also use break (or continue) in a nested if-else with for loops in order to break several loops with if-else, so one can avoid setting lot of flags and testing them in the if-else in order to continue or not in this nested level.
Comments that execute :Till now, we were always taught “Comments do not Execute”. Let us see today “The comments that execute”Following is the code snippet:public class Testing {    public static void main(String[] args)     {         // the line below this gives an output         // \u000d System.out.println("comment executed");     }}Output:comment executedThe reason for this is that the Java compiler parses the unicode character \u000d as a new line and gets transformed into:public class Testing {    public static void main(String[] args)    {        // the line below this gives an output        // \u000d        System.out.println("comment executed");    }}
Till now, we were always taught “Comments do not Execute”. Let us see today “The comments that execute”
Following is the code snippet:
public class Testing {    public static void main(String[] args)     {         // the line below this gives an output         // \u000d System.out.println("comment executed");     }}
Output:
comment executed
The reason for this is that the Java compiler parses the unicode character \u000d as a new line and gets transformed into:
public class Testing {    public static void main(String[] args)    {        // the line below this gives an output        // \u000d        System.out.println("comment executed");    }}
Named loops :// A Java program to demonstrate working of named loops.public class Testing {    public static void main(String[] args)    {    loop1:    for (int i = 0; i < 5; i++)     {        for (int j = 0; j < 5; j++)         {            if (i == 3)                break loop1;            System.out.println("i = " + i + " j = " + j);        }    }   }}Output:i = 0 j = 0
i = 0 j = 1
i = 0 j = 2
i = 0 j = 3
i = 0 j = 4
i = 1 j = 0
i = 1 j = 1
i = 1 j = 2
i = 1 j = 3
i = 1 j = 4
i = 2 j = 0
i = 2 j = 1
i = 2 j = 2
i = 2 j = 3
i = 2 j = 4You can also use continue to jump to start of the named loop.We can also use break (or continue) in a nested if-else with for loops in order to break several loops with if-else, so one can avoid setting lot of flags and testing them in the if-else in order to continue or not in this nested level.
// A Java program to demonstrate working of named loops.public class Testing {    public static void main(String[] args)    {    loop1:    for (int i = 0; i < 5; i++)     {        for (int j = 0; j < 5; j++)         {            if (i == 3)                break loop1;            System.out.println("i = " + i + " j = " + j);        }    }   }}
Output:
i = 0 j = 0
i = 0 j = 1
i = 0 j = 2
i = 0 j = 3
i = 0 j = 4
i = 1 j = 0
i = 1 j = 1
i = 1 j = 2
i = 1 j = 3
i = 1 j = 4
i = 2 j = 0
i = 2 j = 1
i = 2 j = 2
i = 2 j = 3
i = 2 j = 4
You can also use continue to jump to start of the named loop.
We can also use break (or continue) in a nested if-else with for loops in order to break several loops with if-else, so one can avoid setting lot of flags and testing them in the if-else in order to continue or not in this nested level.
This article is contributed by Abhineet Nigam. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
java-puzzle
Java
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Different ways of Reading a text file in Java
Constructors in Java
Stream In Java
Generics in Java
Exceptions in Java
Comparator Interface in Java with Examples
HashMap get() Method in Java
Functional Interfaces in Java
StringBuilder Class in Java with Examples
Strings in Java