Few Tricky Programs in JavaJava is a versatile programming language with a rich set of features that cater to a wide range of programming needs. From simple applications to complex systems, Java provides numerous tools and techniques to handle various programming challenges. Among these are some tricky programs that demonstrate intriguing aspects of Java's capabilities and sometimes highlight less obvious features of the language. These programs not only offer practical insights but also present opportunities to deepen one's understanding of core Java concepts. 1. Swapping Without a Temporary VariableSwapping values is a fundamental operation in programming, and Java provides multiple ways to accomplish it. A particularly interesting approach is to swap two variables without using a temporary variable. It can be achieved through arithmetic operations or bitwise XOR operations. Using Arithmetic OperationsArithmetic operations provide a simple way to swap values without requiring additional memory allocation for a temporary variable. However, this method can be problematic if the values are large enough to cause overflow. File Name: SwapWithoutTemp.java Output: Before Swap: a = 5, b = 10 After Swap: a = 10, b = 5 ExplanationAddition: a is updated to the sum of a and b. Subtraction: b is updated to the original value of a by subtracting b from the new a. Subtraction Again: a is updated to the original value of b by subtracting the new b from the new a. Using Bitwise XORBitwise XOR operations offer a neat alternative that avoids overflow issues associated with arithmetic operations. Output: Before Swap: a = 5, b = 10 After Swap: a = 10, b = 5 ExplanationXOR Operation: a is updated to the XOR of a and b. Update b: b is updated to the XOR of the new a and b, which results in the original a. Update a: a is updated to the XOR of the new a and the updated b, which results in the original b. 2. Checking for Palindrome Without Extra SpaceA palindrome is a string that reads the same backward as forward. Checking if a string is a palindrome can be done efficiently without extra space, making use of two pointers to compare characters from both ends of the string. File Name: PalindromeCheck.java Output: Is 'racecar' a palindrome? true 3. Recursive Fibonacci with MemoizationThe Fibonacci sequence is a common algorithmic problem that can be optimized using memoization. The technique stores intermediate results to avoid redundant calculations. File Name: Fibonacci.java Output: Fibonacci of 10 is 55 ExplanationMemoization Map: A HashMap is used to store previously computed Fibonacci numbers. Recursive Function: The fib method computes Fibonacci numbers recursively and checks the map for cached results. Optimization: The method avoids redundant calculations by storing intermediate results, improving performance. 4. Handling Floating-Point Precision IssuesFloating-point precision can lead to unexpected results due to the way numbers are represented in memory. This program demonstrates how precision issues can manifest and how to handle them. File Name: PrecisionExample.java Output: a == b: false Math.abs(a - b) < EPSILON: true ExplanationPrecision Issue: Direct comparison of floating-point numbers (a == b) may fail due to precision errors. Tolerance Comparison: By checking if the difference between a and b is within a small tolerance (EPSILON), we can reliably compare floating-point numbers. 5. Comment Execution ProgramStill we learn that comment does not execute in Java. Now we will learn how to execute comment in Java. File Name: CommentExecution.java Output: javatpoint In the above Java program, we see that that the Java compiler parses the unicode character \u000d as a new line and gets transformed into the following. 6. Named or Labled Loop ProgramA name or label is a valid variable name that denotes the name of the loop to where the control of execution should jump. To label a loop, place the label before the loop with a colon at the end. Therefore, a loop with the label is called a labeled loop. In layman terms, we can say that label is nothing but to provide a name to a loop. It is a good habit to label a loop when using a nested loop. We can also use labels with continue and break statements. There are three types of loop in Java:
Let's discuss the above three loops with labels. Java Labeled for LoopLabeling a for loop is useful when we want to break or continue a specific for loop according to requirement. If we put a break statement inside an inner for loop, the compiler will jump out from the inner loop and continue with the outer loop again. What if we need to jump out from the outer loop using the break statement given inside the inner loop? The answer is, we should define the label along with the colon(:) sign before the loop. Syntax: Let's see an example of labeled for-loop. File Name: NamedLoop.java Output: i = 0 j = 0 i = 0 j = 1 i = 0 j = 2 i = 1 j = 0 i = 1 j = 1 i = 1 j = 2 i = 2 j = 0 i = 2 j = 1 i = 2 j = 2 Java Labeled while LoopSyntax: File Name: LabledWhileLoop.java Output: outer value of i= 0 inner value of i= 1, j= 0 outer value of i= 1 outer value of i= 2 outer value of i= 3 outer value of i= 4 ConclusionThese tricky Java programs illustrate various advanced and nuanced aspects of the language, demonstrating how to handle common programming challenges creatively and efficiently. Understanding these tricky programs provides valuable insights into Java's features and capabilities, enhancing both practical programming skills and theoretical knowledge. Each example contributes to a broader understanding of how to tackle complex problems and utilize Java's extensive feature set effectively. Next TopicFind-largest-rectangle-in-matrix-java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India