Showing posts with label JAVA Basics Part 2. Show all posts
Showing posts with label JAVA Basics Part 2. Show all posts

Tuesday, July 3, 2012

Java Basics 10: Java Conditional Operators


Java has the conditional operator. It's a ternary operator -- that is, it has three operands -- and it comes in two pieces, ? and :, that have to be used together. It takes the form
Boolean-expression ? expression-1 : expression-2
The JVM tests the value of Boolean-expression. If the value is trueit evaluates expression-1; otherwise, it evaluates expression-2
For 
Example

if (a > b) {
     max = a;
}
else {
     max = b;
}


Setting a single variable to one of two states based on a single condition is such a common use of if-else that a shortcut has been devised for it, the conditional operator, ?:. Using the conditional operator you can rewrite the above example in a single line like this:

max = (a > b) ? a : b;

Java Basics 9:Java Boolean Operators

The Boolean logical operators are : | , & , ^ , ! , || , && , == , != . Java supplies a primitive data type called Boolean, instances of which can take the value true or false only, and have the default value false. The major use of Boolean facilities is to implement the expressions which control if decisions and while loops.

These operators act on Boolean operands according to this table

A         B             A|B       A&B      A^B      !A
false     false         false     false    false    true
true      false         true      false    true     false
false     true          true      false    true     true
true      true          true      true     false    false
| the OR operator
& the AND operator
^ the XOR operator
! the NOT operator
|| the short-circuit OR operator
&& the short-circuit AND operator
== the EQUAL TO operator
!= the NOT EQUAL TO operator 

Example
     
class Bool1{ 
   public static void main(String args[]){

// these are boolean variables     
      boolean A = true;
      boolean B = false; 

      System.out.println("A|B = "+(A|B));
      System.out.println("A&B = "+(A&B));
      System.out.println("!A = "+(!A));
      System.out.println("A^B = "+(A^B));
      System.out.println("(A|B)&A = "+((A|B)&A));
    }
}

JAVA Basics 8: Java Relational Operators


A relational operator compares two values and determines the relationship between them. For example, != returns true if its two operands are unequal. Relational operators are used to test whether two values are equal, whether one value is greater than another, and so forth. The relation operators in Java are: ==!=<><=, and >=. The meanings of these operators are:
UseReturns true if
op1 > op2op1 is greater than op2
op1 >= op2 op1 is greater than or equal to op2
op1 op2 op1 is less than to op2
op1 <= op2 op1 is less than or equal to op2
op1 == op2 op1 and op2 are equal
op1 != op2 op1 and op2 are not equal
Variables only exist within the structure in which they are defined. For example, if a variable is created within a method, it cannot be accessed outside the method. In addition, a different method can create a variable of the same name which will not conflict with the other variable. A java variable can be thought of

The main use for the above relational operators are in CONDITIONAL phrases The following java program is an example, RelationalProg, that defines three integer numbers and uses the relational operators to compare them. 
public class RelationalProg {
 public static void main(String[] args) {

 //a few numbers
 int i = 37;
 int j = 42;
 int k = 42;

 //greater than
 System.out.println("Greater than...");
 System.out.println(" i > j = " + (i > j)); //false
 System.out.println(" j > i = " + (j > i)); //true
 System.out.println(" k > j = " + (k > j)); //false
 //(they are equal)

 //greater than or equal to
 System.out.println("Greater than or equal to...");
 System.out.println(" i >= j = " + (i >= j)); //false
 System.out.println(" j >= i = " + (j >= i)); //true
 System.out.println(" k >= j = " + (k >= j)); //true

 //less than
 System.out.println("Less than...");
 System.out.println(" i < j = " + (i < j)); //true
 System.out.println(" j < i = " + (j < i)); //false
 System.out.println(" k < j = " + (k < j)); //false

 //less than or equal to
 System.out.println("Less than or equal to...");
 System.out.println(" i <= j = " + (i <= j)); //true
 System.out.println(" j <= i = " + (j <= i)); //false
 System.out.println(" k <= j = " + (k <= j)); //true

 //equal to
 System.out.println("Equal to...");
 System.out.println(" i == j = " + (i == j)); //false
 System.out.println(" k == j = " + (k == j)); //true

 //not equal to
 System.out.println("Not equal to...");
 System.out.println(" i != j = " + (i != j)); //true
 System.out.println(" k != j = " + (k != j)); //false
 }
}

JAVA Basics 7:Java Increment and Decrement Operators


There are 2 Increment or decrement operators ->  ++ and --. These two operators are unique in that they can be written both before the operand they are applied to, called prefix increment/decrement, or after, called postfix increment/decrement.  The meaning is different in each case.
Example
x = 1;
y = ++x;
System.out.println(y);

prints 2, but

x = 1;
y = x++;
System.out.println(y);

prints 1
Source Code
//Count to ten

class UptoTen  {

  public static void main (String args[]) {
    int i;
    for (i=1; i <=10; i++) {  
      System.out.println(i);
    } 
   }

}
When we write i++ we're using shorthand for i = i + 1. When we say i-- we're using shorthand for i = i - 1. Adding and subtracting one from a number are such common operations that these special increment and decrement operators have been added to the language. T
There's another short hand for the general add and assign operation, +=. We would normally write this as i += 15. Thus if we wanted to count from 0 to 20 by two's we'd write:
Source Code
class CountToTwenty  {

  public static void main (String args[]) {
    int i;
    for (i=0; i <=20; i += 2) {  //Note Increment Operator by 2
      System.out.println(i);
    } 
   
 } //main ends here

}
As you might guess there is a corresponding -= operator. If we wanted to count down from twenty to zero by twos we could write: -=
class CountToZero  {

  public static void main (String args[]) {
    int i;
    for (i=20; i >= 0; i -= 2) {  //Note Decrement Operator by 2
      System.out.println(i);
    } 
   }

}

JAVA Basics 6: Java Assignment Operators


It's very common to see statement like the following, where you're adding something to a variable. Java Variables are assigned, or given, values using one of the assignment operators. The variable are always on the left-hand side of the assignment operator and the value to be assigned is always on the right-hand side of the assignment operator. The assignment operator is evaluated from right to left, so a = b = c = 0; would assign 0 to c, then c to b then b to a.

i = i + 2;
Here we say that we are assigning i's value to the new value which is i+2.

A shortcut way to write assignments like this is to use the += operator. It's one operator symbol so don't put blanks between the + and =.
i += 2; // Same as "i = i + 2"
The shortcut assignment operator can be used for all Arithmetic Operators i.e. You can use this style with all arithmetic operators (+, -, *, /, and even %).

Here are some examples of assignments:
//assign 1 to 
//variable a
int a = 1;

//assign the result 
//of 2 + 2 to b
int b = 2 + 2;
//assign the literal 
//"Hello" to str
String str = new String("Hello");

//assign b to a, then assign a 
//to d; results in d, a, and b being equal
int d = a = b;

JAVA Basics 5: Java Arithmetic Operators

The Java programming language has includes five simple arithmetic operators like are + (addition), - (subtraction), * (multiplication), / (division), and % (modulo). The following table summarizes the binary arithmetic operators in the Java programming language.


The relation operators in Java are: ==!=<><=, and >=. The meanings of these operators are:
UseReturns true if
op1 + op2op1 added to op2
op1 - op2 op2 subtracted from op1
op1 op2 op1 multiplied with op2
op1 / op2 op1 divided by op2
op1 % op2 Computes the remainder of dividing op1 by op2
The following java program, ArithmeticProg , defines two integers and two double-precision floating-point numbers and uses the five arithmetic operators to perform different arithmetic operations. This program also uses + to concatenate strings. The arithmetic operations are shown in boldface.
public class ArithmeticProg {
 public static void main(String[] args) {

 //a few numbers
 int i = 10;
 int j = 20;
 double x = 10.5;
 double y = 20.5;

 //adding numbers
 System.out.println("Adding");
 System.out.println(" i + j = " + (i + j));
 System.out.println(" x + y = " + (x + y));

 //subtracting numbers
 System.out.println("Subtracting");
 System.out.println(" i - j = " + (i - j));
 System.out.println(" x - y = " + (x - y));

 //multiplying numbers
 System.out.println("Multiplying");
 System.out.println(" i * j = " + (i * j));
 System.out.println(" x * y = " + (x * y));

 //dividing numbers
 System.out.println("Dividing");
 System.out.println(" i / j = " + (i / j));
 System.out.println(" x / y = " + (x / y));

 //computing the remainder resulting
 //from dividing numbers
 System.out.println("Modulus");
 System.out.println(" i % j = " + (i % j));
 System.out.println(" x % y = " + (x % y));

    }
}