JAVA TUTORIAL FOR BEGINNERS PART-2 !!!!!!!!!!!!!!!
PART-2
* Java Arithmetic Operators
* Java Assignment Operators
* Java Increment and Decrement Operators
* Java Relational Operators
* Java Boolean Operators
* Java Conditional Operators
"Java Arithmetic Operators"
he relation operators in Java are: ==, !=, <, >, <=, and >=. The meanings of these operators are:
Use Returns true if
op1 + op2 op1 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));
}
}
__________________________________________________________________________________________________-
"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 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 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:
Use Returns true if
op1 > op2 op1 is greater than op2
op1 >= op2 op1 is greater than or equal to op2
op1 < op1 ="="" i =" 37;" j =" 42;" k =" 42;"> 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(" i = " + (j < i)); //false System.out.println(" j = " + (k < j)); //false //less than or equal to System.out.println(" j = " + (i <= j)); //true System.out.println(" i = " + (j <= i)); //false System.out.println(" j = " + (k <= j)); //true //equal to System.out.println(" i ="="" j = " + (i == j)); //false System.out.println(" k ="="" j = " + (k == j)); //true //not equal to System.out.println(" j = " + (i != j)); //true System.out.println(" j = " + (k != j)); //false } } __________________________________________________________________________________________________ " operator ="="" a =" true;" b =" false;" b = "+(A|B)); System.out.println(" b = "+(A&B)); System.out.println(" a = "+(!A)); System.out.println(" b = "+(A^B)); System.out.println(" a = "+((A|B)&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;
PART-2
* Java Arithmetic Operators
* Java Assignment Operators
* Java Increment and Decrement Operators
* Java Relational Operators
* Java Boolean Operators
* Java Conditional Operators
"Java Arithmetic Operators"
he relation operators in Java are: ==, !=, <, >, <=, and >=. The meanings of these operators are:
Use Returns true if
op1 + op2 op1 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));
}
}
__________________________________________________________________________________________________-
"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 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 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:
Use Returns true if
op1 > op2 op1 is greater than op2
op1 >= op2 op1 is greater than or equal to op2
op1 < op1 ="="" i =" 37;" j =" 42;" k =" 42;"> 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(" i = " + (j < i)); //false System.out.println(" j = " + (k < j)); //false //less than or equal to System.out.println(" j = " + (i <= j)); //true System.out.println(" i = " + (j <= i)); //false System.out.println(" j = " + (k <= j)); //true //equal to System.out.println(" i ="="" j = " + (i == j)); //false System.out.println(" k ="="" j = " + (k == j)); //true //not equal to System.out.println(" j = " + (i != j)); //true System.out.println(" j = " + (k != j)); //false } } __________________________________________________________________________________________________ " operator ="="" a =" true;" b =" false;" b = "+(A|B)); System.out.println(" b = "+(A&B)); System.out.println(" a = "+(!A)); System.out.println(" b = "+(A^B)); System.out.println(" a = "+((A|B)&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;
0 Responses to "JAVA TUTORIAL FOR BEGINNERS PART-2"
Post a Comment