Operators are used to performing operations on variables and values.

There are six types of operators, and it will be good for you once you understand how they work in Java because it will be very similar to other languages. 

  • Arithmetic Operators
    • +, -, *, /, % (remaining)
  • Comparison operators (=relational operator)
    • >=, <=, == (same), != (same)
    • ex) if(a == b){} // if a and b are equal
    • if(a!=b){} // if a and b are not equal
  • Condition operators = (conditional expression)
    • ? Value1: Value2;
      If the conditional expression is true, assign the value 1 to the variable
      If the conditional expression is false, assign the value 2 to the variable.
  • Logical operators
    • ||, &&,!
  • Extended substitution operators
    • +=, -=, *=, /=, %=
    • ex) a+=b; // a = a + b;
      a-=b; // a = a - b;
      a*=b; // a = a * b;
      a/=b; // a = a / b;
      a%=b; // a = a % b;
  • Increase/decrease operators
    • ++ Increasing by 1 ++a (pre-processing) // a=a+1;
      a++ (following) // a=a+1;
    • -- Decrease by one --a (pre-processing) // a=a-1;
      a--(following) // a=a-1;

Here are some examples of using the operators in Java. 

 

Arithmetic Operators

public class Oper01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a = 10, b = 3, c;
		
		c = a + b;
		System.out.println("a+b="+c);
		System.out.println("a+b="+(a+b));
		System.out.println("a-b="+(a-b));
		System.out.println("a*b="+(a*b));
		System.out.println("a/b="+(a/b)); 
		System.out.println("a%b="+(a%b)); 
		
        //Character
		String str1 = "Java";
		String str2 = str1 + "Oracle";
		System.out.println("Str1="+str1);
		System.out.println("Str2="+str2);
		
		String str3 = "Python";
		String str4 = "Spring";
		System.out.println(str3 + str4); 
		
		int i = 50; 
		System.out.println(str3 + i); 
		
		String str5 = str3 + 50;
		System.out.println("str5="+str5); 
			
	}

}

Comparison operators (=relational operator)

public class Oper03 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// Comparison Operator: > , < , >= , <= , == , !=
		
		int num1 = 10; 
		int num2 = 10;
		
		boolean result1 = (num1 == num2);
		boolean result2 = (num1 != num2);
		boolean result3 = (num1 <= num2);
		System.out.println("result1="+result1);
		System.out.println("result2="+result2);
		System.out.println("result3="+result3);
		
		System.out.println(num1 > num2);
		
		char c1 = 'A'; //65
		char c2 = 'B'; //66
		boolean result4 = (c1 < c2);
		System.out.println("result4="+result4);

		// Comparison Operator2
		String str1 = "Java";
		String str2 = "Java";
		String str3 = new String("Java");
		
		// Comparing the addresses
		if(str1 == str2) {
			System.out.println("Same Address");
		}else {
			System.out.println("Different Address");
		}
		
		if(str1 == str3) {
			System.out.println("Same Address");
		}else {
			System.out.println("Different Address");
			
			// Comparing the values
			System.out.println(str1.equals(str2));
			System.out.println(str1.equals(str3));
		}
	}

}

Condition operators = (conditional expression)

import java.util.Scanner;

public class Oper6 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// Condition Operator
        // Variable = (Conditional statement) ? Value1 : Value2;
        // True -> Value1 / False -> Value2

		// Maximum and minimum values out of the two integers entered on the keyboard.
		
		int n1, n2, max, min; 
		System.out.println("Enter two integers");
		
		Scanner sc = new Scanner(System.in);
		n1 = sc.nextInt();
		n2 = sc.nextInt();
		
		max = (n1 > n2) ? n1 : n2;
		min = (n1 < n2) ? n1 : n2;
		
		System.out.println("max="+max);
		System.out.println("min="+min);
		
	}

}

Logical Operators

 The results are treated as int-type if you perform arithmetic operations on the int-type and int-type variables. If the double and double types are performed, the result is treated as a double type. The results are treated as double types if you perform arithmetic operations of int and double types. 

import java.util.Scanner;

public class Oper07 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// Write a program that determines whether you pass or fail 5 subjects when you enter them on the keyboard
		// Each subject's class is 40 points, and an average of 60 points or more is required to pass.
		
		int n1, n2, n3, n4, n5, total;
		double avg;
		System.out.println("Enter the scores of 5 subjects.");
		
		Scanner sc = new Scanner(System.in);
		
		n1 = sc.nextInt();
		n2 = sc.nextInt();
		n3 = sc.nextInt();
		n4 = sc.nextInt();
		n5 = sc.nextInt();
		

		total = n1 + n2 + n3 + n4 + n5;
		avg = (double)total / (double)5;
		System.out.println("avg="+avg);
		
		if(n1>=40 && n2>=40 && n3>=40 && n4>=40 && n5>=50 && avg>= 60) {
			System.out.println("Success");
		}else {
			System.out.println("Fail");
		}
		
}
}
public class Oper08 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		boolean b1 = true;
		boolean b2 = false;
		
		System.out.println(!b1);   
		//false !not operator changes to the opposite
		System.out.println(!b2);  
		System.out.println(!true);  
		System.out.println(!false);  
	}

}

Extended substitution operators

public class Oper09 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int a = 10, b = 3;
		System.out.println(a+=b); 
		// a=a+b
		System.out.println(a-=b); 
		System.out.println(a*=b); 
		System.out.println(a/=b); 
		System.out.println(a%=b); 
	}

}

Increase/decrease operators

public class Oper10 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int a=10, b=10, c=10, d=10;
		int a1, b1, c1, d1;
		
		a1 = ++a;  //Prefix Operator
		b1 = b++;  //Postfix Operator
		c1 = --c;  //Prefix Operator
		d1 = d--;  //Postfix Operator
		
		System.out.println("a1="+a1+" a="+a);
		System.out.println("b1="+b1+" b="+b);
		System.out.println("c1="+c1+" c="+c);
		System.out.println("d1="+d1+" d="+d);
		
		int a=10, b=10;
		System.out.println("a="+a++); //Postfix Operator : a=10
		System.out.println("a="+a); //a=11
		
		System.out.println("b="+(++b)); //Prefix Operator :b=11
		System.out.println("b"+b); //b=11
	}

}

 

'Java' 카테고리의 다른 글

Java) Array  (0) 2022.09.04
Java) .length vs length()  (0) 2022.09.02
Java) Conditional statements and loops  (0) 2022.08.30
Java) Data Types  (0) 2022.08.27
Java (Must know)  (0) 2022.08.25

+ Recent posts