Hello guys, welcome to lingarajtechhub.com . Today we are going to discuss subtract two numbers using arithmetic operators in java.
Code:
package com.substraction.java; public class Substract { public static void main(String[] args) { float a=5.2f , b =1.2f; float sub=a-b; System.out.println("Result is:" +sub); } }
Line-1:
- package com.substraction.java
It is a java package , this package is created in a java project and it contains classes inside the package.
Line-3:
- public class Substract
Here ” Substract ” is the class-name , class is a template used to create objects & class-name first letters must be capital . It is public means we can access this data in another project.
Line-5:
- public static void main(String[] args)
It is a main method in which we can execute a program.
public : It is an access modifier. The main() is public because we can access main() outside of the class , if main() can’t be public then JVM(Java Virtual Machine) can’t be called main() & code can’t be executed.
Static : static is a keyword in which the main method has to be static in which JVM can load the class into memory and call the main method without creating an instance.
Void : Void is a data type. It means no return type.
main(): Main is a function.
String [] args : string means collection of characters ,String[] args represents a collection of Strings that are separated by a space and can be typed into the program on the terminal directly.
Line-6:
- float a=5.2f , b =1.2f
Here a,b is the variable and float is adata type of the variable.Here ”a” value assign 5.2f &“b” value assign 1.2f .
Line-7:
- float sub=a-b
“sub” is the variable and float is a data type of the variable.The method subtract two integers using the “ -” operator.
Line-8:
- System.out.println(“Result is:” +sub)
system is a predefined class .Out is a static variable of type printstream present in system class. Println shows the result.