CORE JAVA PROGRAMMING NOTES | QSPIDERS BASAVANAGUDI | BENGALURU |
KARNATAKA
CORE
JAVA
PROGRA
MMING CORE JAVAPROGRAMMING
FROM QSPIDERS BY AKASH SIR
Operators
Operators are pre defined symbols which are used to perform some specific task.
Based on operands, operates are classified into 3 types
1. Unary operator
2. Binary operator
3. Ternary operator
1. Unary operator
Operator which accepts only one operand is known as unary operator
Ex – logical NOT operator, increment and decrement operator, type cast operator, new operator
2. Binary operator
Operator which accepts 2 operands is known as binary operator(i.e LHS+RHS)
Ex -Arithmetic operator, relational operator, logical operator, logical AND, assignment and compound
assignment operator
3. Ternary operator
Operator which accepts 3 operand is known as ternary operator
Ex – conditional operator
Types of operators
1. Arithmetic operator
2. Typecast operator
3. Relational operator
4. Conditional operator
5. Logical operator
6. Assighment operator
7. Compound assignment operator
1. Arithmetic operator
Binary operator (LHS+RHS)
Used to perform a arithmetic operation
‘+’, ‘-’, ‘*’, ‘/’, ‘%’
where
/ division(gives quotient)
% modulus(gives remainder)
Ex- 1. 5/2=2
2. 5%2=1
Arithmetic operator
1. Addition
2. Concatenation
Concatenation
LHS or RHS or both side having string then we can perform concatenation
Ex -1. “A”+’a’=Aa
2. ‘A’+10=65+10=75
3. 10+“10”= “1010”
4. “hello”+10=“hello10”
5. “10”+ “20”+true=“1020true”
6. 10+20+“true”=30true
7. true+10= CTE/error
8. ‘h’+“10”+10=“h1010”
9. “true”+10=“true10”
10. true+10=true10(we cannot add or subtract Boolean value)
11. “true”*10=CTE(concat only +, not -,*,/,%)
Note : By drfault compiler will take int for whole numbers and double for decimal numbers
Ex – int 5 (whole num)
double 5.0(decimal num)
/ and % (division and modulus operator)
Ex – 1. 5/2=2
2. 5%2=1
Cases (% modulus operator)
1. m%n
m/n = m%n
1. m>n
m- numerator
n- denominator
i.e m>n =10>5
m>n= 0…….n-1(result)
2. m==n
i.e m==n = 5==5
m==n = 0(result)
3. m<n
i.e m<n = 5< 10 = 5%10
m<n = m (result)
Resultant type of arithmetic operator is
Max(int, operator1 type, operator2)
Ex – 1. ‘a’+’A’
Max(int, operator1 type, operator2)
Max(int, char,char)
int
2. 5.0/2
Max(int, double, int)
Int
3. 5/2
Max(int, int, int)
Int
4. 65 +‘B’
Max(int, int, char)
Int
Note : (we cannot store decimal or double values inside int container)
int= 4 bytes double= 8 bytes
2. type cast operator(unary operator)
Type casting=convert one type pf data into another type of data
There are 2 types
1. Primitive typecasting
2. Non-primitive typecasting
1. Primitive typecasting
i. Widening
ii. Narrowing
Widening/Auto widening
Smaller range data into larger range of data
No data loss
Implicitly
Narrowing
Larger range into smaller range
Data loss
Achieve explicitly
Using type cast operator
Syntax: (type to be converted ) value/expression/variable;
Ex : 1. int a=10;
byte b= (byte)a;
2. long a= 10;
int b=(int)a;
3. long a=10;
int b=(int)(a+10);
4. char ch= ‘A’;
int a=ch;
5. int a= 10;
long b=a;
6. int a=10;
long b=10;
double c=a;
7. char ch=’A’;
int a=’A’;
8. char ch =’A’;
int a=’A’;
int a=65;
9. float a=10.0;
int b=a; //CTE
10. double a=10.0;
long b=a;//CTE