Convert int to double

Convert int to double using Typecasting In the above example, we have int type variables a and b. Notice the line, Here, the int type variable is automatically converted into double. It is because double is a higher data type (data type with larger size) and int is a lower data type (data type with smaller size). Hence, there will be no loss in data while converting… Continue reading Convert int to double

Swap Two Numbers

Swap two numbers using temporary variable Output: In the above program, two numbers 1.20f and 2.45f which are to be swapped are stored in variables: first and second respectively. The variables are printed before swapping using println() to see the results clearly after swapping is done. First, the value of first is stored in variable temporary (temporary = 1.20f). Then, value of second is stored in first (first = 2.45f). And, finally value… Continue reading Swap Two Numbers

Round a Number

Round a Number using format Output In the above program, we’ve used the format() method to print the given floating-point number num to 4 decimal places.  The 4 decimal places are given by the format .4f. This means, print only up to 4 places after the dot (decimal places), and f means to print the floating-point number.

Compute Quotient and Remainder

In this program, you’ll learn to compute quotient and remainder from the given dividend and divisor in Java. Example: Compute Quotient and Remainder Output: In the above program, we have created two variables dividend and divisor. Here, we are calculating the quotient and remainder by dividing 25 by 4. To find the quotient, we have… Continue reading Compute Quotient and Remainder

Multiply two Floating Point Numbers

In this program, you’ll learn to multiply two floating point numbers in Java, store the result and display it on the screen. Example: Multiply Two Floating-Point Numbers Output In the above program, we have two floating-point numbers 1.5f and 2.0f stored in variables first and second respectively. Notice, we have used f after the numbers. This ensures the numbers are float, otherwise they will be assigned… Continue reading Multiply two Floating Point Numbers

Add Two Integers

In this program, you’ll learn to store and add two integer numbers in Java. After addition, the final sum is displayed on the screen. Program to Add Two Integers Output: In this program, two integers 10 and 20 are stored in integer variables first and second respectively. Then, first and second are added using the + operator, and its result is stored in another variable sum. Finally, sum is printed on the… Continue reading Add Two Integers

Hello World

A “Hello, World!” is a simple program that outputs Hello, World! on the screen. Since it’s a very simple program, it’s often used to introduce a new programming language to a newbie. Let’s explore how Java “Hello, World!” program works. Java “Hello, World!” Program Output How Java “Hello, World!” Program Works? // Your First Program In Java,… Continue reading Hello World

Wrapper Class

In this tutorial, we will learn about the Java Wrapper class with the help of examples. The wrapper classes in Java are used to convert primitive types (int, char, float, etc) into corresponding objects. Each of the 8 primitive types has corresponding wrapper classes. Primitive Type Wrapper Class byte Byte boolean Boolean char Character double Double float… Continue reading Wrapper Class

 File Class

In this tutorial, we will learn about Java File and its various operations with the help of examples. The File class of the java.io package is used to perform various operations on files and directories. There is another package named java.nio that can be used to work with files. However, in this tutorial, we will focus on the java.io package. File and Directory… Continue reading  File Class

Java Generics

In this tutorial, we will learn about Java Generics, how to create generics class and methods and its advantages with the help of examples. The Java Generics allows us to create a single class, interface, and method that can be used with different types of data (objects). This helps us to reuse our code. Note: Generics does… Continue reading Java Generics