Java Programs
Java Programs
KARNATAKA
CORE
JAVA
PROGRA
MMING CORE JAVAPROGRAMMING
Operators are pre defined symbols which are used to perform some specific task.
1. Unary operator
2. Binary operator
3. Ternary operator
1. Unary operator
Ex – logical NOT operator, increment and decrement operator, type cast operator, new operator
2. Binary operator
Ex -Arithmetic operator, relational operator, logical operator, logical AND, assignment and compound
assignment operator
3. 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
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”
Note : By drfault compiler will take int for whole numbers and double for decimal numbers
Ex – 1. 5/2=2
2. 5%2=1
1. m%n
m/n = m%n
1. m>n
m- numerator
n- denominator
m>n= 0…….n-1(result)
2. m==n
m==n = 0(result)
3. m<n
m<n = m (result)
Ex – 1. ‘a’+’A’
Max(int, char,char)
int
2. 5.0/2
Int
3. 5/2
Int
4. 65 +‘B’
Int
Note : (we cannot store decimal or double values inside int container)
1. Primitive typecasting
2. Non-primitive typecasting
1. Primitive typecasting
i. Widening
ii. Narrowing
Widening/Auto widening
No data loss
Implicitly
Narrowing
Data loss
Achieve explicitly
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);
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;
long b=a;//CTE