Tag: Java

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 – 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 – 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 

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.

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 Numbers to Objects and vice versa ?

Problem Statement You need convert numbers to objects and vice versa in your Java program. Solution There are times when you need to pass an object for a function where you have a numeric values. To convert numbers to objects and vice versa , you can use the corresponsing wrapper classes. For example , if you want to convert an int to Integer object or…

Java – How to use Packages to organize code ?

Problem Statement You need to use packages in java to organize your code. Solution Your Java program might the following Classes Interfaces Enums Other types There are times when your program might grow larger including th number of java classes used. You might want to organize these source files to that it is easier to maintain and avoid other issues like class name conflicts. Inorder…

Java – How to round floating point number to integer ?

Problem Statement You need to round the floating-point numbers to integers in Java. Solution When you cast a floating value to integer in Java , the value gets truncated. For example , the value 7.8989 when casted to integer will become 7. In order to round he floating-point numbers correctly , you can use the Math.round() method. The Math.round method has a 2 overloaded forms….