Category: Java
Java – How to accept input from Keyboard ?
Problem Statement You need to accpet user input from the keyboard from your java program and display it in the command line. Solution Use the BufferedReader and InputStreamReader defined in the java.io package to read the input data from keyboard and store it in a variable. The output of the above program is Enter data This is a test message from abundantcode.com This is a…
Java – How to setup a Development Environment ?
Problem Statement You need to install java and start developing your first program. Additionally , you also need to download , install and use an IDE (Integrated Development Environment) for development. Solution You need to install Java Development Kit (JDK) 8 which provides the necessary compiler to compile and run your Java programs. Additionally , you can install any one of the following IDE’s by…
How to fill an array with default values in Java ?
If you want to fill an array with default values in Java , you can use the Arrays.fill method to do it . The Arrays.fill method lets the developers fill the empty array with the default values. How to fill an array with default values in Java ? Below is an example of the usage of the Arrays.fill method in java .
Continue statement in Java
The continue statement in java lets the developers to skip the current iteration within the loop statements like for , while or do-while . As soon as the java compiler figures out the continue keyword in the code , it immediately skips the statements that follow the continue keyword and then proceeds with the next iteration. The break statement jumps out of the loop completely…
Java Program #2
What is the Output of the following Java program? Output 10 200 400 Since Second class doesn’t have its own “i” variable, it inherits it from the super class. Also, the constructor of Second is called when we create an object of Parent.
Microsoft Band and Android(Java) -Get the Version Information
Below is a sample code snippet demonstrating how to get the version information from Microsoft Band using Java from android app. How to Get Microsoft Band Version Information using Java in Android Application ?
Java – How to convert a bool or string value to Boolean in Java ?
Problem Statement You need to convert a boolean value of string value to Boolean type in your java program. Solution Use the Boolean class’s constructor and pass the corresponsing parameter to change it. Below is the sample code snippet demonstrating how to do it. The output of the above program is Abundantcode.com Java Tutorials false false
Java – How to Divide BigDecimal value from another in Java ?
Problem Statement You want to divide one BigDecimal value from another in your Java program. Solution Use the divide() instance method defined in the BigDecimal class as shown below.
Java How to – Find if the Class exists or not
Problem Statement You need to test for the presence of a class from your java program. Solution Use Class.forName method to find if a particular class exists. Below is a sample code snippet demonstrating the usage of Class.forName to find out if the class “test” exists. Note that this method throws an exception incase the class cannot be loaded. Note: You need to pass the…
Keywords in Java
Below are some of the well known keywords in Java Keywords in Java abstractassertbooleanbreakbytecasecatchcharclassconstcontinuedefaultdodoubleelseenumextendsfinalfinallyfloatforgotoifimplementsimportinstanceofintinterfacelongnativenewpackageprivateprotectedpublicreturnshortstaticstrictfpsuperswitchsynchronizedthisthrowthrowstransienttryvoidvolatilewhile
Mocking frameworks for Java Developers
There are plenty of mocking framework for java that are available for developers. Below are some of the links or collections of mocking frameworks for Java Developers Mocking frameworks for Java Developers PowerMock JtestR EasyMock Mockito jmockit
Java Program #3
What is the output of the following Java program? Output false, true, false The iterator() method returns an iterator over the elements in the list in sequential order. It doesn’t return a List or a ListIterator object. You can get a ListIterator object by invoking the listIterator method.
Tools to Convert Java to C# Source Code
There are times when a developer would have done a project using Java and would want to reuse some of the data classes or business logic to C# for developing application on different platform. Tools to Convert Java to C# Source Code Below are some of the tools that can be used to convert the code from Java to C# . Sharpen This is a…
Java – How to Check if a string is a valid number?
Problem statement You need to check if a input string contain a valid number or not. Solution Use the appropriate wrapper class for converting the string to numeric formats. For example Double to convert string to double as shown below. When the input is a invalid number , the NumberFormatException is caused which needs to be handled by the developers.
Java – How to parse a string to Boolean object in Java ?
Problem Statement You need to parse the string value to the Boolean object in your Java program. Solution Use the valueOf static method defined in the Boolean class to parse the string value and convert it to the Boolean object in Java.
Java – How to create a random number which is less than 25 ?
Problem Statement You need to create a random number which is less than 25 in Java. Solution Use the Random class’s nextInt method and pass the value within which the random number needs to be generated.
How to sort an array using Comparator in Java ?
The Arrays.sort method in java lets the developers to sort an array of objects. The sort method also accepts the comparator object which can contain the sorting logic. How to sort an array using Comparator in Java ? Below is a sample code snippet demonstrating the usage of the Arrays.sort method with the comparator.
How to copy array and increase size dynamically in Java ?
Below is a sample code snippet in Java demonstrating the steps to copy the array elements to a new array and increase the size dynamically. The program using the Arrays.copyOf method which lets the developers to create new array with a new size as well as copy the content of the old array to it at the same time. How to copy array and increase…
Java – How to check if a double value is Infinite ?
Problem Statement You need to find out if the given number of type double is Infinite or not. Solution Use the isInfinite method defined in the Double class to find of the given number is a infinite as shown below. The output of the above program is Abundantcode.com Java Tutorials true
Java – How to Convert Long to String in Java ?
Problem Statement You need to convert long to a string value in your java program. Solution Use the toString method to convert long to string in Java. The output for the above program is Abundantcode.com Java Tutorials 75