Naming Conventions: Naming
conventions specify the rules to be followed by a Java programmer while writing
the names of packages, classes, methods etc.
· Package
names are written in small letters.
e.g.: java.io, java.lang,
java.awt etc
· Each
word of class name and interface name starts with a capital
e.g.: Sample, AddTwoNumbers
· Method names start with small letters
then each word start with a capital e.g.: sum (), sumTwoNumbers (), minValue () ·
.Variable names also follow the same
above method rule
e.g.:
sum, count, totalCount
· Constants
should be written using all capital letters
e.g.: PI, COUNT
· Keywords
are reserved words and are written in small letters.
e.g.: int, short, float, public, void
Data Types: The
classification of data item is called data type. Java defines eight simple
types of data. byte, short, int, long, char, float, double and boolean. These
can be put in four groups: · Integer Data Types: These data types store integer numbers
Data Type
|
Memory
size
|
Range
|
Byte
|
1 byte
|
-128 to
127
|
Short
|
2 bytes
|
-32768 to
32767
|
Int
|
4 bytes
|
-2147483648 to
2147483647
|
Long
|
8 bytes
|
-9223372036854775808 to
9223372036854775807
|
e.g.: byte rno = 10; long
x = 150L; L means forcing JVM to allot
8 bytes
· Float Data Types: These data types
handle floating point numbers
Data Type
|
Memory size
|
Range
|
Float
|
4 bytes
|
-3.4e38 to
3.4e38
|
Double
|
8 bytes
|
-1.7e308 to
1.7e308
|
e.g.: float pi = 3.142f;
double distance = 1.98e8;
· Character Data Type: This data type
represents a single character. char data type in java uses two bytes of memory
also called Unicode system. Unicode is a specification to include alphabets of
all international languages into the character set of java.
Data Type
|
Memory size
|
Range
|
Char
|
2 bytes
|
0 to 65535
|
e.g.: char
ch = 'x';
· Boolean Data Type: can handle truth values either true or false
e.g.:- boolean response = true;
Operators: An
operator is a symbol that performs an operation. An operator acts on variables
called operands.
· Arithmetic operators: These operators are used to perform
fundamental operations like addition, subtraction, multiplication etc.
Operator
|
Meaning
|
Example
|
Result
|
+
|
Addition
|
3 + 4
|
7
|
-
|
Subtraction
|
5 - 7
|
-2
|
*
|
Multiplication
|
5 * 5
|
25
|
/
|
Division (gives quotient)
|
14 / 7
|
2
|
%
|
Modulus (gives remainder)
|
20 % 7
|
6
|
· Assignment operator: This operator (=)
is used to store some value into a variable.
Simple Assignment
|
Compound Assignment
|
x = x + y
|
x += y
|
x = x – y
|
x -= y
|
x = x * y
|
x *= y
|
x = x y
|
x /= y
|
· Unary operators: As the name indicates
unary operator’s act only on one operand.
Operator
|
Meaning
|
Example
|
Explanation
|
-
|
Unary minus
|
j = -k;
|
k value is negated and stored into j
|
++
|
Increment Operator
|
b++; ++b;
|
b value will be incremented by 1 (called as
post incrementation) b value will be incremented by 1 (called as pre
incrementation)
|
--
|
Decrement Operator
|
b--; --b;
|
b value will be decremented by 1 (called as
post decrementation) b value will be decremented by 1 (called as pre
decrementation)
|
· Relational operators: These operators
are used for comparison purpose.
Operator
|
Meaning
|
Example
|
==
|
Equal
|
x == 3
|
!=
|
Not equal
|
x != 3
|
<
|
Less than
|
x < 3
|
>
|
Greater than
|
x > 3
|
<=
|
Less than or equal to
|
x <= 3
|
· Logical operators: Logical operators
are used to construct compound conditions. A compound condition is a
combination of several simple conditions.
Operator
|
Meaning
|
Example
|
Explanation
|
&&
|
and operator
|
if(a>b &&
a>c)
System.out.print(“yes”);
|
If a value is greater than
b and c then only yes is displayed
|
||
|
or operator
|
if(a==1 || b==1)
System.out.print(“yes”);
|
If either a value is 1 or b
value is 1 then yes is displayed
|
!
|
not operator
|
if( !(a==0) )
System.out.print(“yes”);
|
If a value is not equal to
zero then only yes is displayed
|
· Bitwise operators: These operators act on
individual bits (0 and 1) of the operands. They act only on integer data types,
i.e. byte, short, long and int.
Operator
|
Meaning
|
Explanation
|
&
|
Bitwise AND
|
Multiplies the individual
bits of operands
|
|
|
Bitwise OR
|
Adds the individual bits of
operands
|
^
|
Bitwise XOR
|
Performs Exclusive OR
operation
|
<<
|
Left shift
|
Shifts the bits of the
number towards left a specified number of positions
|
>>
|
Right shift
|
Shifts the bits of the
number towards right a specified number of positions and also preserves the
sign bit.
|
>>>
|
Zero fill right shift
|
Shifts the bits of the
number towards right a specified number of positions and it stores 0 (Zero)
in the sign bit.
|
~
|
Bitwise complement
|
Gives the complement form
of a given number by changing 0’s as 1’s and vice versa.
|
· Ternary Operator or Conditional Operator (?
:): This operator is called ternary because it acts on 3 variables. The
syntax for this operator is:
Variable = Expression1?
Expression2: Expression3;
First Expression1 is evaluated. If it is true, then Expression2
value is stored into variable otherwise Expression3 value is stored into the
variable.
e.g.: max = (a>b) ? a: b;
Program 1: Write
a program to perform arithmetic operations
//Addition of two numbers
class AddTwoNumbers
{ public
static void mian(String args[])
{
int i=10, j=20;
System.out.println("Addition
of two numbers is : " + (i+j));
System.out.println("Subtraction
of two numbers is : " + (i-j));
System.out.println("Multiplication
of two numbers is : " + (i*j));
System.out.println("Quotient
after division is : " + (i/j) );
System.out.println("Remainder
after division is : " +(i%j) );
}
}
//Bitwise Operations
class Bits
{ public
static void main(String args[])
{ byte
x,y;
x=10; y=11;
System.out.println
("~x="+(~x));
System.out.println
("x & y="+(x&y));
System.out.println
("x | y="+(x|y));
System.out.println
("x ^ y="+(x^y));
System.out.println
("x<<2="+(x<<2));
System.out.println
("x>>2="+(x>>2));
System.out.println
("x>>>2="+(x>>>2));
}
}