Hello guys, welcome to lingarajtechhub.com . Today we are going to discuss whether the number is positive negative or zero in java.
Code:
package com.Else_if.java; public class Else_If { public static void main(String[] args) { int n=-13; if(n>0) System.out.println("The value of " +n + " is positive "); else if (n<0) System.out.println("The value of "+n + " is negative"); else System.out.println("zero"); } }
Line-1:
- package com.Else_if.java
It is a java package , this package is created in a java project and it contains classes inside the package.
Line-2:
- public class Else_If
“Else_If” 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 use this outside of the package.
Line-4:
- 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-5:
- int n=13
Here “n” is the variable and int (Integer) is a data type of the variable. n value is 13 which is Assigned .
Line-6 to Line-11:
- if(n>0)
System.out.println(“The value of ” +n + ” is positive “);
else if (n<0)
System.out.println(“The value of “+n + ” is negative”);
else
System.out.println(“zero”)
Here we used the “Else_If” condition. The logic is to check first if the number is greater than 0 then it prints or shows positive. Then check “else if” block or second
condition , if it is less than zero then it prints negative .Otherwise it executes the “else” block and prints zero.