Home
| | Sitemap
||Page number :7
Bitwise Operator
The Bitwise operator when used with any operand works at its individual bits. It does not work with float and double data type.
Logical Operator
Logical operators are generally used with conditional statements and are used to combine two conditions. The three Logical Operators---
| || | Logical Or |
| ! | Logical Not |
| && | Logical And |
Example ---- ( You will see their use in the upcoming chapters) if(a= =1 && b==2) statements… If both the conditions are true then only && block is successfully executed Even if one condition out of the many is true then also the block is successfully executed. If the condition is wrong then only the statements inside the block are executed.
Relational Operators
As the name clear illustrates the meaning, the relational operators show the relations between the two variables. Relational Operators are –
| Greater Than | > |
| Less Than | < |
| Greater Than Equal To | >= |
| Less Than Equal To | <= |
| Equal To | = = |
| Not Equal To | != |
Ternary Operator
Ternary Operator –
Syntax----?:
Conditional Operator is also known as Ternary Operator. It can be used to a substitute of ‘if ’ statement. Observe this program carefully. class cond_opr { public static void main(String args[]) { int n1,n2,ans,ans2; n1=5; n2=10; ans=(n1<n2)?n1:n2; ans2=(n1>n2)?n1:n2;
System.out.println(+ans); System.out.println(+ans2); } }
It difficult to guess the use of ternary operator. So here is an explanation. Look at these statements:
if n1<n2 then Initialize the variable ans with n1 else Initialize the variable ans with n2
This logic can be written as
ans=(n1<n2)?n1:n2; As n1 was less than n2 the variable ans was initialized with n1 in the first case. Similarly the next statement in the program works. Try to reason it out yourself.
Taking Input as Command Line Argument
All the programs we have created till now have variables with there values initialized. (
E.g. int x=4;) Now we will take input from the user using command line arguments. This is a simple calculator.
class sim_calc { public static void main(String args[]) { int n1,n2,add,sub,mul,div; n1=Integer.parseInt(args[0]); n2=Integer.parseInt(args[1]);
add=n1+n2; sub=n1-n2; mul=n1*n2; div=n1/n2;
System.out.println("Sum is "+add); System.out.println("Diff is "+sub); System.out.println("Product is "+mul); System.out.println("Quotient is "+div); }
Writing a program is easy, but understanding the concept behind it is different. Here is a short explanation of what we have done. Things will be clearer once you go through the whole book.
Line :
n1=Integer.parseInt(args[0]); The command line arguments are stored in Strings array which is passed as a parameter in “main”. The Integer.parseInt(parameter) method converts String to Integer. You will learn more about arguments, String and arrays later.
Remember: Index of Array begins with zero. Hence we have started array with args[0]
i.e. zero index.
Exercise
Q1. What is a literal?
Q2. Explain the hierarchy of Constants.
Q3. Why is Java called a Strictly “typed language” ?
Q4. Explain the term variable?
Q5. How can reserve words cause problem?
Q6. What is the main use of all special printing characters?
Q7. Explain Operators with their proper classification.
Q8. Explain the difference between prefix and postfix increment and decrement operator.
Q9. How is ternary operator useful?
Q10.Design a program that takes an input from the user.
page 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29