Friday, July 16, 2010

Programming with Java Operators(Java Operatorlerii)

Fundamental Operators 
  1.   Assignment operators (Atama Operatorler)
  2.  Arithmetic operators (Aritmatik Operatorler)
  3. Relational operators (Iliskisel Operatorler)
  4. Logical operators (Mantiksal Operatorler)
1 .  Assignment Operators(Atama Operatorleri)
  • =
  • +=
  • -=
Degiskenelere degere atamak icin kullanilir.
boolean isA = true;
byte b = 20;
HelloWord  hw = new HelloWord();
 - Compound Assignment Operations(Birlesik Atama Operatoru) : 
  •   +=
  •   -=
a = a + 5 
a= a - 5 
  - Other Java Assigment Operators Such As : >>= , <<= , += , -= , *= , /= , %= , ^= , |= , &= , >>>=
 
2 .  Arithmetic operators (Aritmatik Operatorler) : 
- Basic Arithmetic Operators 
  • + addition (sum) operator
  • - subtraction (difference) operator
  • * multiplication (product) operator
  • / division (quotient) operator
  • % modulus (remainder) operator
int a = 50;
int b = 25;
int c;
c = a + b; //addition (sum) operator
c = a - b; //subtraction (difference) operator
c = a * b;// multiplication (product) operator
c = a / b;  /// division (quotient) operator
c = a % b; //   modulus (remainder) operator

 - Prefix Increment, Postfix Increment, Prefix Decrement, and Postfix Decrement Operators 
  • ++x prefix increment operator
  • --x prefix decrement operator
  • x++ postfix increment operator
  • x-- postfix decrement operator
int x = 10;
int y = ++x ;
System.out.println("x=" + x + ", y=" + y); // x= 11, y= 11


int x = 10;
int y = x++ ;
System.out.println("x=" + x + ", y=" + y); // x= 11, y= 10



int x = 10;
int y = --x ;
System.out.println("x=" + x + ", y=" + y); // x= 9, y= 9



int x = 10;
int y = x-- ;
System.out.println("x=" + x + ", y=" + y); // x= 9, y= 10
Relational Operators



3 .  Relational operators (Iliskisel Operator) : Bu operator geriye Boolean tipinde degerler dondurur bu operatorlar 4 kisimdan olusur.
  •  <  “less than” operator
  • <= “less than or equal to” operator
  • > “greater than” operator
  • >= “greater than or equal to” operator
- Equality Opertors : Esitlenebilecek primitif tipler number\ , character , boolean
  • ==
  • !=
==   operatoru sag ve solundaki veri tipleri ayni tipte ve birbirine esitse true diger durumda ise false dondurur.
!= operatoru sag ve so;undaki degerler esit degil ise true diger durumda false dondurur. 

int value = 12;
/* boolean comparison, prints true */
System.out.println(true == true);
/* char comparison, prints false */
System.out.println('a' != 'a');
/* byte comparison, prints true */
System.out.println((byte)value == (byte)value);
/* short comparison, prints false */
System.out.println((short)value != (short)value);
/* integer comparison, prints true */
System.out.println(value == value);
/* float comparison, prints true */
System.out.println(12F == 12f);
/* double comparison, prints false */
System.out.println(12D != 12d);


4 . Logical operators (Mantiksal Operatorler) : Bu operator geriye Boolean tipinde degerler dondurur 
  •  && logical AND (conditional-AND) operator
  • || logical OR (conditional-OR) operator 
&& = Ve 
/* Assigns true */
boolean and1 = true && true;
/* Assigns false */
boolean and2 = true && false;
/* Assigns false, right operand not evaluated */
boolean and3 = false && true;
/* Assigns false, right operand not evaluated */
boolean and4 = false && false; 




 || = Veya
/* Assigns true, right operand not evaluated */
boolean or1 = true || true;
/* Assigns true, right operand not evaluated */
boolean or2 = true || false;
/* Assigns true */
boolean or3 = false || true;
/* Assigns false */
boolean or4 = false || false; 


Bu konunun daha iyi anlasilmasi icin bir uygulama yapiyoz.
public class MainTest {
        public static void main(String[] args) {
               if ((isItSmall(3)) || (isItSmall(7))) {
                     System.out.println("Result is true");
               }
               if ((isItSmall(6)) || (isItSmall(9))) {
                      System.out.println("Result is true");
               }
        }

        public static boolean isItSmall(int i) {
               if (i < 5) {
                      System.out.println("i < 5");
                      return true;
               } else {
                     System.out.println("i >= 5");
                     return false;
               }
        }
}

 

//Console Output
 i < 5
Result is true
i >= 5
i >= 5


Aslinda bu uygulamadaki ciktiya baktigimizda cok acik olarak anlasiliyor || operator ilk ilce birinciyi kontrol eder eyer true ise diyerine hic gitmez bakmaz calistirmaz onun icin if ((isItSmall(3)) || (isItSmall(7)))  sart bakildiginda bu kisim (isItSmall(3)) tru dondugu icin ikincisi hic calistirilmiyor ama  
if ((isItSmall(6)) || (isItSmall(9)))  bu sarta birinci kontrol edilyor bakiyor false ise o zaman ikinci calistiriliyor

-Logical Negation Operator : ! bu operator degil demektir bir boolean degerin degilini olusturur. yani boolean a = true ; burada a true degeridir ama su sekilde olursa !a o zaman true olan degeri false olur. 

System.out.println(!false); // Prints true
System.out.println(!true); // Prints false
System.out.println(!!true); // Prints true
System.out.println(!!!true); // Prints false
System.out.println(!!!!true); // Prints true 


non-short-circuit logical operators : 
          ■ & non-short-circuit AND :

          ■ | non-short-circuit OR :Bu operatorde sartlarin ikiside kontrol ediliyor ama sadece birinin dogru olasi sarti true ediyor

public class MainTest {
       public static void main(String[] args) {
              int z = 5;
              System.out.println("Before z : "+z);
              if (++z > 5 | ++z > 6) {
                     z++;
                     System.out.println("After z  : "+z);
              }
       }
}

//Console Output 
Before z : 5
After z : 8
Gorulduyu gibi | operatorunde her ikiside konturol ediliyor yani birinci dogru oldugu halde ikinci sartta kontrol ediliyor. 

No comments:

Post a Comment