Lesson_2: Operations in Java

1. Capda toplama emeliyyatinin ferqi:

package org.example.test;

public class Test {
public static void main(String[] args) throws Exception {
int one = 1;
int two = 2;
System.out.println("result = " + one + two);
System.out.println(one + two + " result");
}

} 

char a = 'a';
char b = 'b';
System.out.println(a + b);
System.out.println('a' + 'b');
System.out.println("" + a + b);
System.out.println("" + 'a' + 'b');


2. float ve double-da bash veren riyazi emaliyyat xeyalari:

package org.example.test;

public class Test {
public static void main(String[] args) throws Exception {
float f = 0.6F;
System.out.println(f + 0.1);
}
}


Operators in Java

Operators in Java can be classified into 5 types:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Relational Operators
  4. Logical Operators
  5. Unary Operators
  6. Bitwise Operators


1. Arithmetic Operators:

Addition +

Subtraction -

Multiplication *

Division /

Modulo %


int i = 5;
double d = 2.3;
double result = i + d;
System.out.println(result);
i += d;
System.out.println(i);



2. Assignment Operators:

=

+=

-=

*=

/=

%=


3. Relational Operators:

Relational Operators always return boolean value.

==

!=

>

<

>=

<=


4. Logical Operators:

Logical Operators always return boolean value.

Logical AND &&

Logical OR ||

Logical NOT !


5. Unary Operators

Unary plus + 

Unary minus -

Increment ++

Decrement --

Logical complement !


int i = -5;
int b = -i;
System.out.println(i);
System.out.println(b);


int a = 10;
++a;
a++;
System.out.println(a);


int a = 10;
System.out.println(a++);
System.out.println(++a);


Weird behaviour:

System.out.println("Normal behaviour");
int i = 0;
i = ++i;
System.out.println(i);
i = ++i + 2;
System.out.println(i);

System.out.println("Weird behaviour");
i = 0;
i = i++;
System.out.println(i);
i = i++ + 2;
System.out.println(i);

System.out.println("Normal behaviour too");
i = 0;
int j = i++;
System.out.println(i);
System.out.println(j);


6. Bitwise Operators

Bitwise complement ~

Bitwise AND &

Bitwise OR |

Bitwise XOR ^

Left shift <<

Right shift >>

Unsigned right shift >>>


package org.example;

public class Main {
public static void main(String[] args) {
if((getOne() == 1) && getTwo() == 2) {
System.out.println("done");
}
}

public static int getOne() {
System.out.println("getOne is called");
return 1;
}

public static int getTwo() {
System.out.println("getTwo is called");
return 2;
}
}


Complex version of Ternary operation:

boolean b;
int x = (int) (Math.random() * 1000);
int y = (int) (Math.random() * 1000);
int z = (int) (Math.random() * 1000);
// x = 100;
// y = 20;
// z = 30;

b = x < y ? x < z ? true : false : false;
System.out.println(b);

b = (x < y ? (x < z ? true : false) : false);
System.out.println(b);

b = (x < y && (x < z ? true : false));
System.out.println(b);


Precedence. Evaluation Order:

int a = 6;
int b = 5;
int c = 10;
float rs = a + ++b * c / a * b;
System.out.println(rs);


Situation1:

int a = 6;
int b = 5;
int c = 10;

int rs = ++b * c / a * b;
System.out.println(rs);

Burada cavab 60 alinir. Cunki ilk once (++b) olur ve b = 6 olur. Sonra ise moterize olmadigi ucun

6 * 10 / 6 * 6 olur.


Situation2:

int a = 6;
int b = 5;
int c = 10;

int rs = b++ * c / a * b;
System.out.println(rs);

Burada ise cavab 48 alinir. Cunki 5 * 10 / 6 * 6 olur.


Situation3:

int a = 6;
int b = 5;
int c = 10;

int rs = a++ + ++b * c / a * b;
System.out.println(rs);

Burada cavab 54 alinir. Cunki 6 + 6 * 10 / 7 *  6 olur.






Комментарии

Популярные сообщения из этого блога

Lesson1: JDK, JVM, JRE

SE_21_Lesson_11: Inheritance, Polymorphism

SE_21_Lesson_9: Initialization Blocks, Wrapper types, String class