Data Types in Java and Opearator and Separators
Data Types in Java and Opearator and Separators
Data types specify the different sizes and values that can
be stored in the variable. There are two types of data
types in Java:
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
Floating point
double 0.0d 8 byte
Problem
This caused two problems:
1. A particular code value corresponds to
different letters in the various language
standards.
2. The encodings for languages with large
character sets have variable length.
Some common characters are encoded
as single bytes, other require two or
more byte.
Solution
To solve these problems, a new language
standard was developed i.e. Unicode
System.
lowest value:\u0000
highest value:\uFFFF
JAVA TOKENS
1. Reserved ,keywords.
2. Identifiers
3. Literals
4. Operators
5. Separators
N>B: In java most of keywords and identifiers are similar to that
C/C++.However, java has maintained its uniqueness by defining
some new keywords.
1. Reserved keywords: Java language has reserved words (50) like
Boolean, break, final and so on.
int num1=0b0110101
Ternary operator(? :)
1. Arithmetic Operators
Truth table
A B A&&B A||B !A
TRUE TRUE TRUE TRUE FALSE
class OperatorExample
{
public static void main(String args[])
{
int a=10;
int b=5;
int c=20;
System.out.println(a<b && a<c);//false && true = false
System.out.println(a< b&& b<c);//false && true = false
}
}
Output: false
False
4. Assignment operator:
It is used to assign the value on its right to the operand on its left
Syntax:
v op= expression( short hand assignment operator or compound
assignment operator )
is equal to v=v op expression
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=20;
a+=4;
b-=4;
System.out.println(a); o/p= 14
System.out.println(b); = 16
}}
22
21
Ex;class OperatorExample
{ Output: False
public static void main(String args[]) True
{
boolean c=true;
boolean d=false;
System.out.println(!c);
System.out.println(!d);
}}
6. Condition operator( ternary operator)
System.out.println(x);
7.Bitwise operator
Bitwise operators are used to manipulate the data at values of bit level.
These operators are used to for testing the bits or shifting them to right or
left. (Bitwise operators may not applied to float or double).
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ One’s Complement
<< Shift left
>> shift right
>>> Shift right with zero fill
int x=2,y=3;
A B A && B
0 0 0 System.out.println(x&y)
010
0 1 0 011
O/p=2 010
1 0 0
1 1 1
Bitwise OR(|)
A B A|B
int x=2,y=3;
0 0 0
System.out.println(x|y)
0 1 1
1 0 1 O/p=3
1 1 1
0 0 0 System.out.println(x^y)
0 1 1
1 0 1 O/p=1
1 1 0
One’s Complement (~)
0 0 0 0 0 0 1 0 X
11 1 1 1 1 1 0 1 ~X
1 1’s complement
0 0 0 0 0 1 0
1
0 0 0 0 0 1 1 +1
-3
0 0 0 0 1 1 0 0 12
EX: class Demo
{
public static void main(String args[])
{int x=12; 1 1 1 1 0 0 1 1 ~12
Sytem.out.println(~x);
}} 1 0 0 0 1 1 0 0 1’s complement
1 0 0 0 1 1 0 1 2’s complement
-13
1 1 1 1 01 0 0 2’s complement
11 0 0 0 0 1 0 1 1 ~ (-12)=12
Ex-3
EX2: class Demo Class demo
{ {public static void main(String srgs[])
public static void main(String args[]) Int x=12
{int x=-12; Int y=13 o/p=13
Int y=13 o/p=4 x=x|y;
x=x&y; Sytem.out.println(x);
Sytem.out.println(x);
}}
Ex-4 Ex-3
Class demo Class demo
{public static void main(String srgs[]) {public static void main(String srgs[])
Int x=12 Int x=-12
Int y=13 o/p=1 Int y=13 o/p=-7
x=x^y; x=x ^y;
Sytem.out.println(x); Sytem.out.println(x);
Syntax:
Variable >>no. of bits shift
Ex: X>>2
Bit wise right shift operator shift the bits present in the variable to a
specified no.of bits towards right .When a positive number is right shifted ,
it is filled by ‘0’ and for negative number it is filled by ‘1’.
-5>>2
Ex: 5>>2
0 0 0 0 0 1 0 1
5 0 0 0 0 0 1 0 1
5>>2 1 1 1 1 1 0 1 0
1 0 0 0 0 0 0 0 1
2’s complement 1 1 1 1 1 0 1 1
>>2
After shifting 1 1 1 1 1 1 1 0
1 0 0 0 0 0 0 1
1 0 0 0 0 0 1 0 -2
1 1 1 0 1 1 0 0
1 0 0 1 0 0 1 1 1’s complement
1 0 0 1 0 1 0 0 2’s complement
-20
64 -64
0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0
64>>>4 >>>4
0 0 0 0 0 1 0 0 4 0 0 0 0 1 1 0 0 ?
Special operator
1. Instanceof operator
2. Member selection operator
1. This operator allows us to determine whether the object belong to
particular class or not.
Ex: person instance of student is true if the object person belongs to the
class student ,otherwise it is false.
Ex:class Demo
{
public static void main(String args[])
{
String s =”Hello”;
If ( s instanceof java.lang.String) o/p= it is a string
{
Sytem.out.println(“It is a string”);
}}}
2.dot operator (.)