Unit 1 - 2
Unit 1 - 2
Data type specifies the size and type of values that can be stored.
Primitive Non-primitive
Numeric Non-numeric
classes Arrays
integer
char Interface
char 2 A-Z,a-z,0-9,etc.
byte – Zero : 0
short – Zero : 0
int – Zero : 0
long – Zero : 0L
float – 0.0f
double – 0.0d
char – null
boolean – false
Variables
Variables are labels that express a particular position in
memory and connect it with a data type.
The first way to declare a variable:
– It assigns zero to primitive types and null to objects.
dataType variableName;
Numeric Constants
Integer constant
• Decimal
• Octal
• Hexadecimal
Real constant
Character Constants
Character constant
String constant
Backslash character constant
Constant Importance
„\b„ Back space
„\t„ Tab
„\n„ New line
„\\„ Backslash
„\‟‟ Single quote
„\”„ Double quote
Symbolic constants
These constants may appear repeatedly in a
number of places in the program.
One example of such a constant is 3.142,
representing the value of the mathematical constant
“pi” .
We face two problems in the subsequent use of
such programs. They are:
– 1. Problem in modification of the program
Examples:
int m = 50;
Byte n = (byte) m;
long count= (long) m;
}}
Logical operators
• When we want to form compound conditions by combining two
or more relations, then we can use logical operators.
class OperatorExample{
public static void main(String[] args){
int a=10;
a+=3; 13
System.out.println(a);
a-=4;
System.out.println(a);
9
a*=2;
System.out.println(a);
18
a/=2;
System.out.println(a); }} 9
Increment and Decrement Operators
The increment operator ++ adds 1 to a variable.
Usually the variable is an integer type, but it can be a floating point type.
If Expression1 is false then Expression3 is evaluated and its value becomes the
conditional expression.
A=3; B=4;
C=(A<B)?A:B; Answer C=3
C=(3<4)?3:4;
Conditional Operators
class OperatorExample{
public static void main(String args[]){
int a=2;
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}}
Answer: 2
Bit Wise Operators
These operators are used for testing the bits, or shifting
them to right or left.
Following table shows bit wise operator
Operator Importance/ significance
| Bitwise OR Example:
& Bitwise AND int a= 4;
&= Bitwise AND assignment int b = a<<3;
|= Bitwise OR assignment
^ Bitwise Exclusive OR Output:
<< Left shift
>> Right shift
b =32
~ One„s complement
Bit Wise Operators
public class Test {
c = a << 2; /* 240 = 1111 0000 */
public static void main(String args[]) {
System.out.println("a << 2 = " + c );
int a = 60; /* 60 = 0011 1100 */
c = a >> 2; /* 15 = 1111 */
int b = 13; /* 13 = 0000 1101 */ System.out.println("a >> 2 = " + c );
int c = 0; c = a >>> 2; /* 15 = 0000 1111 */
c = a & b; /* 12 = 0000 1100 */ System.out.println("a >>> 2 = " + c );
System.out.println("a & b = " + c );
c = a | b; /* 61 = 0011 1101 */
System.out.println("a | b = " + c );
c = a ^ b; /* 49 = 0011 0001 */
System.out.println("a ^ b = " + c );
c = ~a; /*-61 = 1100 0011 */
System.out.println("~a = " + c );
}
}
Special Operators
Java supports some special operators of interest such as
instanceof operator and member selection operator (.) .
Instanceof Operator :
– The instanceof is an object reference operator and returns true if the object
on the left hand is an instance of the class given on right-hand side
Dot Operator :
– The dot operator (.) is used to access the instance variable and methods
of class object.
Person1.age
Person1.salary()
Operator Precedence in Java:
x=a-b/3+c*2-1
when a=9, b=12, and c=3 what will be x
Casting value