Java Unit 1 (1.9 1.10 1.11 1.12)
Java Unit 1 (1.9 1.10 1.11 1.12)
class add_num
{
public int add(int a, int b)
{
return a+b;
}
}
public class test
{
public static void main(String args[])
{
int a, b;
a=10;
b=3;
add_num ob = new add_num();
System.out.println("The addition: "+ ob.add(a,b));
}
} // Output 13
Constants
Constants refer to fixed values that doesn’t
change during the execution of a program.
Java supports the following constants:
Integer Constants
Real Constants
Single Character Constants
String Constants
Backslash Character
Syntax
static final datatype identifier_name=value;
Variables
An identifier which denotes a memory location
where their values are stored.
Variables can change their values during the
execution of a program.
They can take different values during the
execution of a program.
Variable names may have alphabets, digits,
underscore(_) and dollar ($).
Name must not start with a digit, should not be a
keyword, should not have whitespace and name
can be of any length.
Scope and Lifetime of a variable
A block begins with an opening curly brace and ends
by a closing curly brace.
A block determines scope, that defines which objects
are visible to other parts of your program.
Variables declared within a block localize themselves.
In case of nested block, the outer block encloses the
inner block. The variables declared in the outer block
is visible to the inner block but the reverse is not true.
A variable will not hold it’s value once it has gone out
of it’s scope.
Scope and Lifetime of a variable
Example:
public static void main( String args[])
{
int x =100;
if ( x == 100)
{
int y =2;
System.out.println(“x and y: “+x+ ” “+y);
x= y * 2;
}
y= 100; //Error
System.out.println (“x is “+x);
}
Data Types
A data type is a scheme for representing values.
Values are not just numbers, but any manner of
data that a computer can process.
The data type defines the size and type of data
that is represented by a variable.
As with the keyword class, Java data types are
case sensitive.
Data Types
Data Types
Primitive Non-
primitive
Non-
Numeric numeric Classes
Integer Character
Interface
Floating- Boolean
point Arrays
Primitive Data type
A variable of primitive type contains a single
value of the appropriate size and format for its
type: a number, a character, or a boolean value.
Primitive types are also termed as intrinsic or
built-in types.
Primitive Data type
Data Type
byte For whole
numbers
short
int
long
float
double Real numbers
char Characters
boolean Boolean
Symbolic Constant
Certain unique constant are needed in a program.
These may appear repeatedly at different locations in
the program.
Consider an example where the value of PI is set to
3.14. This is used multiple times in the program.
Better approach is to declare it a constant.
Syntax: final type symbolic_name = value;
Symbolic constants cannot be modified in the
program.
They should be used only as data members of the
class.
Type Casting
The process of converting one data type to
another is called casting.
This is needed when a method returns a type
which is different than the one required.
Syntax:
type variable_name1 = (type) var_name2;
Examples:
int s = 50;
byte t = (byte) s;
long c = (long) s;
Casting to a smaller type will lead to loss of data.
Type Casting
Type casting leading to no loss of information.
From To
byte short, char, int, long, float, double
short int, long, float, double
int long, float, double
long float, double
float double
char int, long, float, double
Type Casting
Automatic type conversion occurs when assigning
a value of one type to a variable of different type
without explicitly casting.
Java automatically does this type conversion.
This happens only when the destination type is
larger than the source type.
Two types are compatible.
Widening: assigning a smaller type to a larger
type.
Narrowing: assigning a larger type to a smaller
one.
public class test
{
public static void main(String args[])
{
byte b;
int i = 257;
double d = 323.142;
b=(byte)i;
System.out.println ("Conversion of int to byte: "+ i +" "+ b);
i = (int)d;
System.out.println("Conversion of double to int: " +d+" " +i);
b=(byte)d;
System.out.println("Conversion of double to byte: " +d+" "+b);
}
}
Operators
Symbol used in an expression to perform an
operation on operands.
There are different types of operators which are
used in an expression.
Arithmetic operators
Logical operators
Relational operators
Assignment operators
Conditional operators
Increment and decrement operators
Bit wise operators
Arithmetic Operators
Class Remainderoptr
{
public static void main (String args[])
{
int a = 6;
int b = 3;
System.out.println("a = " + a);
System.out.println("b =" + b);
c = a % b;
System.out.println("remainder=" + c);
}
}
Operator Type Category Precedence
additive +-
equality == !=
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical OR ||
Ternary ternary ?:
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
System.out.println(a+b);//15
System.out.println(a-b);//5
System.out.println(a*b);//50
System.out.println(a/b);//2
System.out.println(a%b);//0
}}
Output:
Java Logical AND Operator
Example: Logical &&
The logical && operator doesn't check second
condition if first condition is false. It checks
second condition only if first one is true.
Input Output
A B C
T T T
T F F
F T F
F T F
F F F
Example
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 = fal
se
}}
Bitwise operators
Bitwise operators are used to perform manipulation of individual
bits of a number. They can be used with any of the integral types
(char, short, int, etc). They are used when performing update and
query operations of Binary indexed tree.
Bitwise OR (|) –
This operator is binary operator, denoted by ‘|’. It returns bit by bit
OR of input values, i.e, if either of the bits is 1, it gives 1, else it
gives 0.
For example,a = 5 = 0101 (In Binary) b = 7 = 0111
(In Binary) Bitwise OR Operation of 5 and 7
0101
|
0111
________
0111 = 7 (In decimal)
Bitwise AND (&) –
This operator is binary operator, denoted by ‘&’. It
returns bit by bit AND of input values, i.e, if both bits
are 1, it gives 1, else it gives 0.
For example,
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)
Bitwise AND Operation of 5 and 7
0101
& 0111
________
0101 = 5 (In decimal)
Bitwise XOR (^)
This operator is binary operator, denoted by ‘^’. It returns
bit by bit XOR of input values, i.e, if corresponding bits are
different, it gives 1, else it gives 0.
For example,
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)
Bitwise XOR Operation of 5 and 7
0101
^ 0111
________
0010 = 2 (In decimal)
^ -Bit wise XOR operation
5 0 101
7 0 111
0 010 =2
A^B
public class operators
{
public static void main(String[] args)
{
//Initial values
int a = 5;
int b = 7;
// bitwise and
// 0101 & 0111=0101 = 5
System.out.println("a&b = " + (a & b));
// bitwise or
// 0101 | 0111=0111 = 7
System.out.println("a|b = " + (a | b));
// bitwise xor
// 0101 ^ 0111=0010 = 2
System.out.println("a^b = " + (a ^ b));
// bitwise and
// ~0101=1010
// will give 2's complement of 1010 = -6
System.out.println("~a = " + ~a);
}
}
Shift Operators
These operators are used to shift the bits of a
number left or right thereby multiplying or dividing
the number by two respectively. They can be
used when we have to multiply or divide a
number by two. General format:
number shift_op number_of_places_to_shift;
A >> 1
Signed Right shift operator
(>>)
Shifts the bits of the number to the right and fills 0 on voids left as a
result. The leftmost bit depends on the sign of initial number.
Similar effect as of dividing the number with some power of two.
For example,
Example 1:
a = 10
a>>1 = 5
1010>>1
0101=5
A=7
0111>>1
0011=3
.
Left shift operator (<<)
Shifts the bits of the number to the left and fills 0 on
voids left as a result. Similar effect as of multiplying
the number with some power of two.
For example,
a = 5 = 0000 0101
b = -10 = 1111 0110
a << 1 = 0000 1010 = 10
a << 2 = 0001 0100 = 20
b << 1 = 1110 1100 = -20
b << 2 = 1101 1000 = -40
public class operators {
public static void main(String[] args)
{
int a = 5;
int b = -10;
}
}
Shift operators
a= -2. Find (i) a>>2 (ii) a<<2
a = -10. Find (i) a>>2 (ii) a<<2
Operator Precedence & Associativity