Deprecated: Creation of dynamic property Kirki_Toolkit::$modules is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/kirki.php on line 67

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Textarea::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Checkbox::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Toggle::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Radio::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Checkbox::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Custom::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Custom::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Radio_Image::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Radio::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Multicheck::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Number::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Checkbox::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Text::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Radio_Image::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Multicheck::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Checkbox::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Checkbox::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Radio_Image::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Custom::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Custom::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Warning: Cannot modify header information - headers already sent by (output started at /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php:291) in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-includes/feed-rss2.php on line 8
Java Tutorial Archives - OctopusCodes https://octopuscodes.com/category/java/java-tutorial/ Wed, 01 Feb 2023 08:49:50 +0000 en-US hourly 1 https://wordpress.org/?v=6.9 https://octopuscodes.com/wp-content/uploads/2022/12/cropped-logo-80x80-1-32x32.png Java Tutorial Archives - OctopusCodes https://octopuscodes.com/category/java/java-tutorial/ 32 32 Write a Java program to print the area and perimeter of a square https://octopuscodes.com/java/java-tutorial/basic-java/write-a-java-program-to-print-the-area-and-perimeter-of-a-square/ Wed, 01 Feb 2023 08:47:58 +0000 https://octopuscodes.com/?p=1215 This example will provide how to print the area and perimeter of a square in Java. Example: package octopuscodes.com.demo; import java.util.Scanner; public class Demo { public static void main(String[] args) …

The post Write a Java program to print the area and perimeter of a square appeared first on OctopusCodes.

]]>
This example will provide how to print the area and perimeter of a square in Java.

package octopuscodes.com.demo;

import java.util.Scanner;

public class Demo {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		System.out.print("a: ");
		double a = scanner.nextDouble();

		double area = a * a;
		double perimeter = a * 4;

		System.out.println("Area : " + area);
		System.out.println("Perimeter : " + perimeter);

	}

}
a: 5
Area : 25.0
Perimeter : 20.0

    The post Write a Java program to print the area and perimeter of a square appeared first on OctopusCodes.

    ]]>
    Write a Java program to print the area and perimeter of a rectangle https://octopuscodes.com/java/java-tutorial/basic-java/write-a-java-program-to-print-the-area-and-perimeter-of-a-rectangle/ Wed, 01 Feb 2023 08:43:10 +0000 https://octopuscodes.com/?p=1207 This example will provide how to print the area and perimeter of a rectangle in Java. Example: package octopuscodes.com.demo; import java.util.Scanner; public class Demo { public static void main(String[] args) …

    The post Write a Java program to print the area and perimeter of a rectangle appeared first on OctopusCodes.

    ]]>
    This example will provide how to print the area and perimeter of a rectangle in Java.

    package octopuscodes.com.demo;
    
    import java.util.Scanner;
    
    public class Demo {
    
    	public static void main(String[] args) {
    
    		Scanner scanner = new Scanner(System.in);
    		System.out.print("a: ");
    		double a = scanner.nextDouble();
    		System.out.print("b: ");
    		double b = scanner.nextDouble();
    
    		double area = a * b;
    		double perimeter = (a + b) * 2;
    
    		System.out.println("Area : " + area);
    		System.out.println("Perimeter : " + perimeter);
    
    	}
    
    }
    a: 3
    b: 10
    Area : 30.0
    Perimeter : 26.0
    

      The post Write a Java program to print the area and perimeter of a rectangle appeared first on OctopusCodes.

      ]]>
      Write a Java program to print the area and perimeter of a circle https://octopuscodes.com/java/java-tutorial/basic-java/write-a-java-program-to-print-the-area-and-perimeter-of-a-circle/ Wed, 01 Feb 2023 08:37:57 +0000 https://octopuscodes.com/?p=1200 This example will provide how to print the area and perimeter of a circle in Java. Example: package octopuscodes.com.demo; import java.util.Scanner; public class Demo { public static void main(String[] args) …

      The post Write a Java program to print the area and perimeter of a circle appeared first on OctopusCodes.

      ]]>
      This example will provide how to print the area and perimeter of a circle in Java.

      package octopuscodes.com.demo;
      
      import java.util.Scanner;
      
      public class Demo {
      
      	public static void main(String[] args) {
      
      		Scanner scanner = new Scanner(System.in);
      		System.out.print("Radius: ");
      		double radius = scanner.nextDouble();
      		double area = Math.PI * radius * radius;
      		double perimeter = 2 * Math.PI * radius;
      		System.out.println("Area : " + area);
      		System.out.println("Perimeter : " + perimeter);
      
      	}
      
      }
      Radius: 4
      Area : 50.26548245743669
      Perimeter : 25.132741228718345
      

        The post Write a Java program to print the area and perimeter of a circle appeared first on OctopusCodes.

        ]]>
        Write a Java program to convert Celsius into Fahrenheit https://octopuscodes.com/java/java-tutorial/basic-java/write-a-java-program-to-convert-celsius-into-fahrenheit/ Wed, 01 Feb 2023 08:33:21 +0000 https://octopuscodes.com/?p=1193 This example will provide how to convert Celsius into Fahrenheit in Java. Example: package octopuscodes.com.demo; import java.util.Scanner; public class Demo { public static void main(String[] args) { Scanner scanner = …

        The post Write a Java program to convert Celsius into Fahrenheit appeared first on OctopusCodes.

        ]]>
        This example will provide how to convert Celsius into Fahrenheit in Java.

        package octopuscodes.com.demo;
        
        import java.util.Scanner;
        
        public class Demo {
        
        	public static void main(String[] args) {
        
        		Scanner scanner = new Scanner(System.in);
        		System.out.print("Celsius: ");
        		double celsius = scanner.nextDouble();
        		double fahrenheit = celsius * 1.8 + 32;
        		System.out.println("Fahrenheit: " + fahrenheit);
        
        	}
        
        }
        Celsius: 100
        Fahrenheit: 212.0
        

          The post Write a Java program to convert Celsius into Fahrenheit appeared first on OctopusCodes.

          ]]>
          Write a Java program to convert Fahrenheit into Celsius https://octopuscodes.com/java/java-tutorial/basic-java/write-a-java-program-to-convert-fahrenheit-into-celsius/ Wed, 01 Feb 2023 08:27:07 +0000 https://octopuscodes.com/?p=1185 This example will provide how to convert Fahrenheit into Celsius in Java. Example: package octopuscodes.com.demo; import java.util.Scanner; public class Demo { public static void main(String[] args) { Scanner scanner = …

          The post Write a Java program to convert Fahrenheit into Celsius appeared first on OctopusCodes.

          ]]>
          This example will provide how to convert Fahrenheit into Celsius in Java.

          package octopuscodes.com.demo;
          
          import java.util.Scanner;
          
          public class Demo {
          
          	public static void main(String[] args) {
          
          		Scanner scanner = new Scanner(System.in);
          		System.out.print("Fahrenheit: ");
          		double fahrenheit = scanner.nextDouble();
          		double celsius = (fahrenheit - 32) / 1.8;
          		System.out.println("Celsius: " + celsius);
          
          	}
          
          }
          Fahrenheit: 212
          Celsius: 100.0
          

            The post Write a Java program to convert Fahrenheit into Celsius appeared first on OctopusCodes.

            ]]>
            Java Operators with Real Life Examples https://octopuscodes.com/java/java-tutorial/basic-java/java-operators-with-real-life-examples/ Wed, 01 Feb 2023 08:18:37 +0000 https://octopuscodes.com/?p=1178 This post will provide real life examples how to use operators in Java.

            The post Java Operators with Real Life Examples appeared first on OctopusCodes.

            ]]>
            This post will provide real life examples how to use operators in Java.

            The post Java Operators with Real Life Examples appeared first on OctopusCodes.

            ]]>
            Assignment Operators in Java https://octopuscodes.com/java/java-tutorial/basic-java/assignment-operators-in-java/ Mon, 30 Jan 2023 10:34:35 +0000 https://octopuscodes.com/?p=1143 In Java, assignment operators are used in Java to assign values to variables. Operator Example = c = d; += c += d; -= c -= d; *= c *= …

            The post Assignment Operators in Java appeared first on OctopusCodes.

            ]]>
            In Java, assignment operators are used in Java to assign values to variables.

            OperatorExample
            =c = d;
            +=c += d;
            -=c -= d;
            *=c *= d;
            /=c /= d;
            %=c %= d;

            1. (=) Operator

            This operator is used to assign the value on the right to the variable on the left.

            package octopuscodes.com.demo;
            
            public class Demo {
            
            	public static void main(String[] args) {
            		int a = 10;
            		System.out.println("a: " + a);
            		
            		double b, c, d, e;
            		b = c = d = e = 4.5;		
            		System.out.println("b: " + b);
            		System.out.println("c: " + c);
            		System.out.println("d: " + d);
            		System.out.println("e: " + e);
            		
            		double f = b + c;
            		System.out.println("f: " + f);
            	}
            
            }
            a: 10
            b: 4.5
            c: 4.5
            d: 4.5
            e: 4.5
            f: 9.0
            
             

            2. (+=) Operator

            This operator is a compound of “+” and “=” operators. It operates by adding the current value of the variable on the left to the value on the right and then assigning the result to the operand on the left.

            package octopuscodes.com.demo;
            
            public class Demo {
            
            	public static void main(String[] args) {
            		int a = 10;
            		a += 5;
            		System.out.println("a: " + a);
            	}
            
            }
            a: 15
            
             

            Best Keyboards for Programming

            3. (-=) Operator

            This operator is a compound of “-” and “=” operators. It operates by subtracting the variable’s value on the right from the current value of the variable on the left and then assigning the result to the operand on the left.

            package octopuscodes.com.demo;
            
            public class Demo {
            
            	public static void main(String[] args) {
            		int a = 10;
            		a -= 5;
            		System.out.println("a: " + a);
            	}
            
            }
            a: 5
            
             

            4. (*=) Operator

            This operator is a compound of “*” and “=” operators. It operates by multiplying the current value of the variable on the left to the value on the right and then assigning the result to the operand on the left.

            package octopuscodes.com.demo;
            
            public class Demo {
            
            	public static void main(String[] args) {
            		int a = 10;
            		a *= 5;
            		System.out.println("a: " + a);
            	}
            
            }
            a: 50
            
             

            5. (/=) Operator

            This operator is a compound of “/” and “=” operators. It operates by dividing the current value of the variable on the left by the value on the right and then assigning the quotient to the operand on the left.

            package octopuscodes.com.demo;
            
            public class Demo {
            
            	public static void main(String[] args) {
            		int a = 10;
            		a /= 5;
            		System.out.println("a: " + a);
            	}
            
            }
            a: 2
            
             

            6. (%=) Operator

            This operator is a compound of “%” and “=” operators. It operates by dividing the current value of the variable on the left by the value on the right and then assigning the remainder to the operand on the left.

            package octopuscodes.com.demo;
            
            public class Demo {
            
            	public static void main(String[] args) {
            		int a = 10;
            		a %= 5;
            		System.out.println("a: " + a);
            	}
            
            }
            a: 0
            

            The post Assignment Operators in Java appeared first on OctopusCodes.

            ]]>
            Logical Operators in Java https://octopuscodes.com/java/java-tutorial/basic-java/logical-operators-in-java/ Wed, 25 Jan 2023 08:27:25 +0000 https://octopuscodes.com/?p=1122 In Java, logical operators are used to determine the logic between variables or values. Logical operators are used to check whether an expression is true or false. Operator Description && …

            The post Logical Operators in Java appeared first on OctopusCodes.

            ]]>
            In Java, logical operators are used to determine the logic between variables or values. Logical operators are used to check whether an expression is true or false.

            OperatorDescription
            && (Logical AND)Returns true if both statements are true.
            || (Logical OR)Returns true if one of the statements is true.
            ! (Logical NOT)Reverse the result, returns false if the result is true and vice versa.

            1. Logical “AND” Operator (&&)

            This operator returns true when both the conditions under consideration are satisfied or are true. If even one of the two yields false, the operator results false.

            package octopuscodes.com.demo;
            
            public class Demo {
            
            	public static void main(String[] args) {
            		int a = 10, b = 5;
            		
            		boolean result1 = a > 3 && b > 2;
            		System.out.println("result 1: " + result1);
            		
            		boolean result2 = a > 30 && b > 2;
            		System.out.println("result 2: " + result2);
            	}
            
            }
            result 1: true
            result 2: false
            
             

            2. Logical “OR” Operator (||)

            This operator returns true when one of the two conditions under consideration is satisfied or is true. If even one of the two yields true, the operator results true. To make the result false, both the constraints need to return false.

            package octopuscodes.com.demo;
            
            public class Demo {
            
            	public static void main(String[] args) {
            		int a = 10, b = 5;
            		
            		boolean result1 = a > 3 || b > 2;
            		System.out.println("result 1: " + result1);
            		
            		boolean result2 = a > 30 || b < 2;
            		System.out.println("result 2: " + result2);
            	}
            
            }
            result 1: true
            result 2: false
            
             

            3. Logical “NOT” Operator (!)

            This operator is a unary operator and returns true when the condition under consideration is not satisfied or is a false condition.

              package octopuscodes.com.demo;
              
              public class Demo {
              
              	public static void main(String[] args) {
              		int a = 10, b = 5;
              		
              		boolean result1 = !(a > 3 || b > 2);
              		System.out.println("result 1: " + result1);
              		
              		boolean result2 = !(a > 30 || b < 2);
              		System.out.println("result 2: " + result2);
              	}
              
              }
              result 1: false
              result 2: true
              

                The post Logical Operators in Java appeared first on OctopusCodes.

                ]]>
                Relational Operators in Java https://octopuscodes.com/java/java-tutorial/basic-java/relational-operators-in-java/ Tue, 24 Jan 2023 02:11:23 +0000 https://octopuscodes.com/?p=1093 In Java, relational operators are used to check the relationship between two operands. Operator Description == Is Equal To != Not Equal To > Greater Than >= Greater Than or …

                The post Relational Operators in Java appeared first on OctopusCodes.

                ]]>
                In Java, relational operators are used to check the relationship between two operands.

                OperatorDescription
                ==Is Equal To
                !=Not Equal To
                >Greater Than
                >=Greater Than or Equal To
                <Less Than
                <=Less Than or Equal To

                1. “Equal to” Operator(==)

                This operator is used to check whether the two given operands are equal or not. The operator returns true if the operand at the left-hand side is equal to the right-hand side, else false.

                package octopuscodes.com.demo;
                
                public class Demo {
                
                	public static void main(String[] args) {
                		int a = 10, b = 5;
                		boolean result1 = a == b;
                		System.out.println("result 1: " + result1);
                
                		int c = 7, d = 7;
                		boolean result2 = c == d;
                		System.out.println("result 2: " + result2);
                	}
                
                }
                result 1: false
                result 2: true
                
                 

                2. “Not Equal to” Operator(!=)

                This operator is used to check whether the two given operands are equal or not. It returns true if the operand at the left-hand side is not equal to the right-hand side, else false.

                package octopuscodes.com.demo;
                
                public class Demo {
                
                	public static void main(String[] args) {
                		int a = 10, b = 5;
                		boolean result1 = a != b;
                		System.out.println("result 1: " + result1);
                
                		int c = 7, d = 7;
                		boolean result2 = c != d;
                		System.out.println("result 2: " + result2);
                	}
                
                }
                result 1: true
                result 2: false
                
                 

                3. “Greater than” Operator(>)

                This checks whether the first operand is greater than the second operand or not. The operator returns true when the operand at the left-hand side is greater than the right-hand side.

                  package octopuscodes.com.demo;
                  
                  public class Demo {
                  
                  	public static void main(String[] args) {
                  		int a = 10, b = 5;
                  		boolean result1 = a > b;
                  		System.out.println("result 1: " + result1);
                  
                  		int c = 7, d = 17;
                  		boolean result2 = c > d;
                  		System.out.println("result 2: " + result2);
                  	}
                  
                  }
                  result 1: true
                  result 2: false
                  
                   

                  4. “Greater than or equal to” Operator(>=)

                  This checks whether the first operand is greater than or equal to the second operand or not. The operator returns true when the operand at the left-hand side is greater than or equal to the right-hand side.

                    package octopuscodes.com.demo;
                    
                    public class Demo {
                    
                    	public static void main(String[] args) {
                    		int a = 10, b = 5;
                    		boolean result1 = a >= b;
                    		System.out.println("result 1: " + result1);
                    
                    		int c = 7, d = 7;
                    		boolean result2 = c >= d;
                    		System.out.println("result 2: " + result2);
                    		
                    		int e = 7, f = 14;
                    		boolean result3 = e >= f;
                    		System.out.println("result 3: " + result3);
                    	}
                    
                    }
                    result 1: true
                    result 2: true
                    result 3: false
                    
                     

                    5. “Less than” Operator(<)

                    This checks whether the first operand is less than the second operand or not. The operator returns true when the operand at the left-hand side is less than the right-hand side.

                    package octopuscodes.com.demo;
                    
                    public class Demo {
                    
                    	public static void main(String[] args) {
                    		int a = 10, b = 5;
                    		boolean result1 = a < b;
                    		System.out.println("result 1: " + result1);
                    
                    		int c = 7, d = 17;
                    		boolean result2 = c < d;
                    		System.out.println("result 2: " + result2);
                    	}
                    
                    }
                    result 1: false
                    result 2: true
                    
                     

                    6. "Less than or equal to" Operator(<=)

                    This checks whether the first operand is greater than or equal to the second operand or not. The operator returns true when the operand at the left-hand side is greater than or equal to the right-hand side.

                    package octopuscodes.com.demo;
                    
                    public class Demo {
                    
                    	public static void main(String[] args) {
                    		int a = 10, b = 5;
                    		boolean result1 = a <= b;
                    		System.out.println("result 1: " + result1);
                    
                    		int c = 7, d = 7;
                    		boolean result2 = c <= d;
                    		System.out.println("result 2: " + result2);
                    
                    		int e = 2, f = 7;
                    		boolean result3 = e <= f;
                    		System.out.println("result 3: " + result3);
                    	}
                    
                    }
                    result 1: false
                    result 2: true
                    result 3: true
                    

                    The post Relational Operators in Java appeared first on OctopusCodes.

                    ]]>
                    Unary Operators in Java https://octopuscodes.com/java/java-tutorial/basic-java/unary-operators-in-java/ Mon, 23 Jan 2023 04:35:30 +0000 https://octopuscodes.com/?p=1059 In Java, unary arithmetic operators are used to increasing or decreasing the value of an operand. Operator Description + Unary plus operator; indicates positive value – Unary minus operator; negates …

                    The post Unary Operators in Java appeared first on OctopusCodes.

                    ]]>
                    In Java, unary arithmetic operators are used to increasing or decreasing the value of an operand.

                    OperatorDescription
                    +Unary plus operator; indicates positive value
                    Unary minus operator; negates an expression
                    ++Increment operator; increments a value by 1
                    – –Decrement operator; decrements a value by 1
                    !Logical complement operator; inverts the value of a boolean

                    1. Plus Operator(+)

                    It is used to represent positive values. Usually, we do not write the operator before the operand. Hence, it is optional.

                    package octopuscodes.com.demo;
                    
                    public class Demo {
                    
                    	public static void main(String[] args) {
                    		int number1 = 4;
                    		System.out.println("Number 1: " + number1);
                    
                    		int result1 = +number1;
                    		System.out.println("After unary plus operation result 1 is " + result1);
                    
                    		int number2 = -9;
                    		System.out.println("Number 2: " + number2);
                    
                    		int result2 = +number2;
                    		System.out.println("After unary plus operation result 2 is " + result2);
                    	}
                    
                    }
                    Number 1: 4
                    After unary plus operation result 1 is 4
                    Number 2: -9
                    After unary plus operation result 2 is -9
                    
                     

                    2. Minus Operator(-)

                    It is used to convert a positive value into a negative one.

                    package octopuscodes.com.demo;
                    
                    public class Demo {
                    
                    	public static void main(String[] args) {
                    		int number1 = 4;
                    		System.out.println("Number 1: " + number1);
                    
                    		int result1 = -number1;
                    		System.out.println("After unary minus operation result 1 is " + result1);
                    
                    		int number2 = -9;
                    		System.out.println("Number 2: " + number2);
                    
                    		int result2 = -number2;
                    		System.out.println("After unary minus operation result 2 is " + result2);
                    	}
                    
                    }
                    Number 1: 4
                    After unary minus operation result 1 is -4
                    Number 2: -9
                    After unary minus operation result 2 is 9
                    
                     

                    3. Increment Operator(++)

                    It is used to increment the value of an operand by one. The operator can be applied before or after an operand.

                    • Pre-increment Operator: If an increment operator is written before (prefix) the operand is known as pre-increment.
                    • Post-increment Operator: If an increment operator is written after (postfix) the operand is known as post-increment.
                    package octopuscodes.com.demo;
                    
                    public class Demo {
                    
                    	public static void main(String[] args) {
                    		int number = 4;
                    		System.out.println("Number: " + number);
                    
                    		++number;
                    		System.out.println("After Pre-increment operation number is " + number);
                    
                    		number++;
                    		System.out.println("After Post-increment operation number is " + number);
                    	}
                    
                    }
                    Number: 4
                    After Pre-increment operation number is 5
                    After Post-increment operation number is 6
                    
                     

                    4. Decrement operator(– –)

                    It is used to decrement the value of an operand by 1. The operator can be applied before or after an operand.

                    • Pre-decrement Operator: If a decrement operator is written before (prefix) the operand is known as pre-decrement.
                    • Post-decrement Operator: If a decrement operator is written after (postfix) the operand is known as post-decrement.
                    package octopuscodes.com.demo;
                    
                    public class Demo {
                    
                    	public static void main(String[] args) {
                    		int number = 4;
                    		System.out.println("Number: " + number);
                    
                    		--number;
                    		System.out.println("After Pre-decrement operation number is " + number);
                    
                    		number--;
                    		System.out.println("After Post-decrement operation number is " + number);
                    	}
                    
                    }
                    Number: 4
                    After Pre-decrement operation number is 3
                    After Post-decrement operation number is 2
                    
                     

                    5. Logical Complement Operator(!)

                    A logical Complement Operator is used to reverse the value of a Boolean value. It means that if the value of an operand is true, then the complement of the operator will be false and vice-versa.

                    package octopuscodes.com.demo;
                    
                    public class Demo {
                    
                    	public static void main(String[] args) {
                    		boolean status1 = true;
                    		System.out.println("Status1: " + status1);
                    
                    		status1 = !status1;
                    		System.out.println("After logical complement operation status 1 is " + status1);
                    
                    		boolean status2 = false;
                    		System.out.println("Status2: " + status2);
                    
                    		status2 = !status2;
                    		System.out.println("After logical complement operation status 2 is " + status2);
                    	}
                    
                    }
                    Status1: true
                    After logical complement operation status 1 is false
                    Status2: false
                    After logical complement operation status 2 is true
                    

                    The post Unary Operators in Java appeared first on OctopusCodes.

                    ]]>