Tag: Java
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….
Java – How to decode string from Hexadecimal and Octal to Integer in Java ?
Problem Statement You need to decode a string from hexadecimal to integer in java as well as octal to integer. Solution Use the decode method defined in the Integer class to decode an hexadecimal and octal number to integer as shown below. The output of the above program is 255 46
Java – How to Check if integer is multiple of a number?
Problem Statement You need to check if the integer number is a multiple of a number in Java. Solution Use the modulus operator and check the remainder to find it out. Below is a sample code snippet demonstrating how to find if the number is a multiple of 4.
How to Declare an Octal literal in Java ?
Problem Statement You want to declare an octal literal in Java. Solution Prefix the value with 0. A literal that contains leading zero is an octal number. The output of the above program will be 8 21
Java Program to copy range of elements from an array
The Java’s Arrays class provides a static function called Arrays.copyOfRange that helps the developers to copy a range of elements from one array to another. How to Copy range of elements from an array in Java ? Below is an example that demonstrates the copying of the range of objects from one array to another using Arrays.copyOfRange. Output Source Array ———— Java Abundantcode.com MCA Lab…
For loop in Java
The for loop in java lets the developers to iterate over a list of values . The for loop executes until the exit condition matches . Following the syntax of the for loop in java for (initialization; condition;increment){statement(s)} The initialization expression is used to initialize the loop and is executed as soon as the loop begins. The termination condition is verified every time and the…
Microsoft Band and Android(Java) – Connect to the Band
Below is the sample code snippet demonstrating how to connect to the first microsoft band that is paired using the BandInfo and BandClient object. How to Connect to the Microsoft Band from android application using Java ? How to get the list of paired bands from the android application using java.
While Loop example in Java
The while loop in java lets the developers execute a block of statements continuously until a particular condition is true . The syntax of the while loop in java can be written as while(expression){statement(s)} The while loop evaluates the expression which returns true and executes until the expression returns false. While Loop example in Java Below is a sample code snippet demonstrating the usage of…
Java – How to get the String representation of the Boolean value in Java ?
Problem statement You need to convert the Boolean value to string and display the string representation of the same. Solution Use the toString static method defined in the Boolean class for the conversion. The output of the above program is Abundantcode.com Java Tutorials false
Microsoft Band and Android(Java) – Add Permission Tags in the Manifest file
When working with the Microsoft Band using Java for an android app , it is necessary add the following permissions under the uses-permission tag of the AndroidManifest.xml. android.permission.BLUETOOTH com.microsoft.band.service.access.BIND_BAND_SERVICE How to add Permission Tags in the Manifest file to use Band SDK in Java ? The reason for adding this to the manifest file is that the app will be using the capabilities of the…