Category: Java
Java – How to create BigDecimal from long datatype value ?
Problem Statement You need to create BigDecimal values from long type value in your Java program. Solution Use BigDecimal.valueOf method to create BigDecimal from the long value.
Java – How to convert Long to other primitive numeric data types ?
Problem Statement You need convert a long value to other numeric data types in java. Solution Use the Long wrapper class which exposes methods like byteValue , shortValue , intValue etc. Below is a sample code snipper demonstrating the usage of the same. The output for the above program is Abundantcode.com Java Tutorials 76 76 76.0
Java How to – Compile and run Java program in Command-line
Problem Statement You need to compile and run your java program in command-line. Solution You can use the command-line tools available with Java Development Kit (JDK). The “javac” command tool can be used to compile the java program. The “java” command can be used to run the java program. Let’s build out first program and use the command line to run. 1. Download and install…
Java – How to convert a double value to string ?
Problem Statement You need to convert a double value to string in your java program. Solution Use the toString() method defined in the Double class to convert a double value to string in java. The output of the above program is Abundantcode.com Java Tutorials 25.0
Do-While Loop example in Java
The do-while loop in java is similar to the while loop except that the do-while loop is guaranteed to execute at least once . The do while loop evaluates its condition at the bottom of the loop. The syntax of the do-while loop in java can be written as do{statement(s)}while(expression) Do-While Loop example in Java Below is a sample code snippet demonstrating the usage of…
Break Statement in Java
Java provides the break keyword that is used by the developers to exit out of the loop before the actual completion of the loop. Additionally , the break keyword can also be used to jump out of a single case in switch statement. Break Statement in Java Below is a sample code snippet demonstrating the usage of the break statement in java.
Java – How to Negate a BigDecimal in Java ?
Problem Statement You need to negate the value of the BigDecimal datatype in your java program. Solution Use the negate method defined in the BigDecimal class as shown below. The output of the above program is Abundantcode.com Java Tutorials -10102
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.