Java Puzzles

Recent Tutorial

Java Puzzle: Class Name

Let us directly jump into the puzzle. Following code is a Java puzzle, read and guess the output. Then run, see the result and try to reason it. public class StringClass { public static void main(String[] args) { int a = 10 + 20; System.out.println(a); } } class String { private final String str; public […]
Published to Java Tutorial on 10/01/2016.

Java List size Puzzle

This is a Java puzzle involving the Collection List interface. This is a Java beginner level puzzle and will help understand how the size works in List element. What is the output for the following Java code snippet? package com.javapapers.java.puzzle; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ListPuzzle { public static void main(String args[]) […]
Published to Java Tutorial on 23/09/2014.

Java Puzzle: Floating Point Precision

This is a Java puzzle to pep up our floating point arithmetic skills. Guess the output for the following Java puzzle program, public class Precision { public static void main(String... args){ double d = 0.0d; for (int i = 0; i < 10; i++) { d = d + 0.1d; } System.out.println(d); } } For […]
Published to Java Tutorial on 11/07/2014.

Java Puzzle: Commented Code Compiles

This one is a nice Java puzzle. If you know it already then its very cheap else it will be surprising. These puzzles come out of SCJP preparation mostly. That is where these kind of intriguing code gets cooked up. Long time back, I shared a Java puzzle on unreachable statement and it is similar […]
Published to Core Java Tutorial on 21/11/2013.

Java Puzzle: unreachable statement

This is an interesting puzzle to test our understanding on java compiler. What happens when we compile below code blocks? CODE A: public void javapapers() { System.out.println("java"); return; System.out.println("papers"); } CODE B: public void javapapers() { System.out.println("java"); if(true) { return; } System.out.println("papers"); } CODE C: public void javapapers() { System.out.println("java"); while(true) { return; } System.out.println("papers"); […]
Published to Core Java Tutorial on 09/10/2012.

Java’s toLowerCase() has got a surprise for you!

Have you ever encountered a surprise while using toLowerCase()? This is a widely used method when it comes to strings and case conversion. There is a nice little thing you should be aware of. toLowerCase() respects internationalization (i18n). It performs the case conversion with respect to your Locale. When you call toLowerCase(), internally toLowerCase(Locale.getDefault()) is […]
Published to Core Java Tutorial on 09/05/2010.