Java - UNIT I
Java - UNIT I
Sc CS Java Programming Siri PSG Arts & Science College for Women
UNIT – I
1
III B.Sc CS Java Programming Siri PSG Arts & Science College for Women
Class declaration:
- Class Classname declares a class, which is an object-oriented construct. Java is a true object-
oriented language and therefore everything must be placed inside a class. Class is a keyword.
Opening brace:
- Every class definition in java begins with an opening brace “{“and ends with matching closing brace
“}” appearing in the last line.
Main line:
- Public static void main (String args[ ]) defines a method named main. main() method is the starting
point for the interpreter to begin the execution of the program. A java application can have any
number of classes but only one of them must include a main method to initiate the execution.
- The main line contains a number of keywords
public An access specifier, methods can be accessed in other classes.
static This method belongs to entire class and not part of any object of the class. Main
must always be declared as static.
Void It states that the main method does not return any value.
- String args[ ] declares a parameter named args, which contains an array of objects of the class type
string.
The output line:
- The only executable statement in the program is:
System.out.println (“Hai1”);
- Where, println method is a member of the out object, which is a static data member of system class.
This line prints the string “Hai!”
- Println appends a newline character to the end of the string. Every java statement must end with a
semicolon.
2
III B.Sc CS Java Programming Siri PSG Arts & Science College for Women
3
III B.Sc CS Java Programming Siri PSG Arts & Science College for Women
2. Identifiers:
Identifiers are programmer-created tokens. They are used for naming classes, methods, variables,
objects, labels, packages and interfaces in a program.
Java identifiers follow the following rules:
1. They can have alphabets, digits, and the underscore and dollar sign characters.
2. They must not start with a digit.
4
III B.Sc CS Java Programming Siri PSG Arts & Science College for Women
5
III B.Sc CS Java Programming Siri PSG Arts & Science College for Women
6
III B.Sc CS Java Programming Siri PSG Arts & Science College for Women
7
III B.Sc CS Java Programming Siri PSG Arts & Science College for Women
Compile and run the program with the command line as follows:
java ComLineTest Simple Object_Oriented Distributed Robust
Secure Portable Multithreaded Dynamic
Upon execution, the command line arguments simple, Object_Oriented, etc. are passed to the
program through the array args as discussed earlier.
That is the element args [0] contains Simple, args [1] contains Object_Oriented, and so on.
These elements are accessed using the loop variable i as an index like
name = args[i];
The index i is incremented using a while loop until all the arguments are accessed.
The number of arguments is obtained by statement
count = args.length;
The output of the program:
Number of arguments = 8
1 : Java is Simple!
2 : Java is Object_Oriented!
3 : lava is Distributed!
4 : Java is Robust!
5 : Java is Secure!
6 : Java is Portable!
7 : Java is Multithreaded!
8 : Java is Dynamic!
Java Constants
8
III B.Sc CS Java Programming Siri PSG Arts & Science College for Women
Integer Constants:
- An integer constant refers to a sequence of digits. 3 types:
Decimal integer It consists of set of digits 0 to 9, preceded by an optional minus sign.
(Ex: 12450)
Octal Integer It consists of any combination of digits from the set 0 to 7 with leading 0.
(Ex: 037)
Hexadecimal integer It consists of sequence of digits proceeded by 0x or 0X. It may also
include alphabets A through F or ’a’ to ‘f’. It represents the number 10 to 15. (Ex: 0X2, 0X9F)
Real Constants:
- Numbers containing fractional part are called real constants.
Ex: 215.03, 0.75, -0.52, 0.0083
- It have a whole number followed by a decimal point and the fractional part. It is possible that the
number may not have digits before the decimal point or digits after the decimal point.
Ex: 215.00, 0.95, -0.71
- A real number may also be expressed in exponential or scientific notation.
Ex: 215.65 2.1565e2
Where e2 means multiply by 102. The general form is:
- The mantissa is either a real number expressed in decimal notation or an integer. The exponent is an
integer with an optional plus or minus sign.
- A floating point constant has 4 parts:
a whole number
a decimal point
a fractional part
an exponent.
Single Character Constants:
- A single character constant contains a single character enclosed within a pair of single double quote
marks. Ex: ’6’, ‘y’, ‘;’ , ‘ ‘
String Constants:
- A string contains is a sequence of characters enclosed between double quotes. The characters may be
alphabets, digits, special characters and blank spaces.
Ex: “hello java” “1979” “welcome”, “r”
9
III B.Sc CS Java Programming Siri PSG Arts & Science College for Women
1.8.2 VARIABLE
- A Variable is an identifier that denotes a storage location used to store a data value. The name can be
chosen by the programmer in a meaning way.
- Variable names consist of alphabets, digits, underscore (-) and dollar characters, subject to following
conditions:
Rules:
They must not begin with a digit.
Upper case and Lower case letters are distinct.
It should not be a keyword.
White space is not allowed.
Variables names can be of any length.
Data types
10
III B.Sc CS Java Programming Siri PSG Arts & Science College for Women
12
III B.Sc CS Java Programming Siri PSG Arts & Science College for Women
1.9 OPERATORS
- Java supports a rich set of operators. An operator is a symbol that is used to perform certain
mathematical or logical manipulations.
- Operators are used in programs to manipulate data and variables.
ARITHMETIC OPERATOR:
- The operators +,-,*, / and % all work the same way as they do in other languages. We can’t use these
operators on Boolean type.
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% modulo division
- These can operate on any built-in numeric data type of java. The unary minus operator, multiplies
its single operand by -1. A number preceded by a minus sign changed its sign.
Arithmetic operators are used as follows:
Example: a+b, a-b, a*b, a/b, a%b where a & b are operands
Integer Arithmetic:
- When both the operands in a single arithmetic expression such as a+b are integers, the expression is
called an integer expression, and the operation is called integer arithmetic.
13
III B.Sc CS Java Programming Siri PSG Arts & Science College for Women
Example: a = 14 b = 4
a - b = 10 a * b = 56 a % b = 2 a + b = 18 a / b = 3
Real Arithmetic:
- An arithmetic operation involving only real operands is called real arithmetic. A real operand may
assume values either in decimal or exponential notation.
- Floating point values are rounded to the number of significant digits permissible; the final value
approximates the correct result.
Example: a= 20.5F b= 6.4F means
a + b = 26.9
a - b = 14.1
a * b = 131.2
a / b = 3.2031
a % b = 1.3
Mixed-mode arithmetic:
- When one of the operands is real and the other is integer, the expression is called mixed-mode
arithmetic expression.
- If either operand is of the real type, then the other operand is converted to real and the real arithmetic
is performed.
Example:
15 / 10.0 produce the result 1.5
15 / 10 produce the result 1
RELATIONAL OPERATORS:
- The comparisons can be done with the help of relational operators.
Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal to
== Is equal to
!= Is not equal to
14
III B.Sc CS Java Programming Siri PSG Arts & Science College for Women
- Where, ae-1 and ae-2 are arithmetic expressions, which may be a simple constant, variable or
combinations of them.
Example: Expression Value
7 < 10 true
2.5 > 1.5 false
20 > 8+5 false
LOGICAL OPERATORS:
- Java language has three logical operators. They are:
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
- These operators are used to combine two or more relations to form compound conditions. It is
termed as logical expression or compound relational expressions. It also yields a value of true or
false.
Example: a >b && x = = 10
- Combines two or more relational expression is termed as a logical expression or a compound
relational expression.
Truth table
Value of expression
Op-1 Op-2
Op-1 && Op-2 Op-1 || Op-2
True True True True
True False False True
False True False True
False False False False
ASSIGNMENT OPERATORS:
- Assignment operators are used to assign the values of an expression to a variable. Assignment
operator is = (equal to).
15
III B.Sc CS Java Programming Siri PSG Arts & Science College for Women
Syntax: V op = expression;
CONDITIONAL OPERATOR:
- The character pair?: is a ternary operator. This operator is used to construct conditional expression
of the form:
exp1? exp2: exp3
16
III B.Sc CS Java Programming Siri PSG Arts & Science College for Women
- Where exp1, exp2 and exp3 are expressions. The exp1 is evaluated first. If it is true, then the
expression exp2 is evaluated and becomes the value of the conditional expression.
- If exp1 is false, exp3 is evaluated and its value becomes the value of the conditional expression.
Example: Equivalent to
a = 50, b = 10 if(a>b)
x = (a>b)? a : b; x = a;
else
x = b;
BITWISE OPERATORS:
- It is used for testing the bits or shifting them to the right to left. It may not be applied to float or
double.
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ One’s complement or bitwise unary NOT
<< Shift left
>> shift right
>>> Shift right with zero fill
&= Bitwise AND assignment
|= Bitwise OR assignment
^= Bitwise exclusive OR assignment
>>= Shift right assignment
<<= Shift left assignment
>>>= Shift right zero fill assignment
Examples
1010 0101 (bitwise NOT)
1010 & 1111 1010 (bitwise AND- produces a 1 bit if both operands are also 1, otherwise 0)
1010 | 1110 1111 (bitwise OR- produces a 1 bit if either of the bits in the operands is a 1, otherwise 0)
1010 ^ 1111 0101 (bitwise XOR – produces a 1 bit if exactly one operand is 1, otherwise 0)
17
III B.Sc CS Java Programming Siri PSG Arts & Science College for Women
SPECIAL OPERATORS:
- Java contains two special operators. They are:
Instance of operator.
Member selection operator (.)
Instance of Operator:
- It is an object reference operator and returns true if the object on the left-hand side is an instance of
the class given on the right-hand side. It allows us to determine whether the object belongs to a
particular class or not.
Example: person instance of student
- It returns ‘true’ if the object person belong to class student, otherwise it is ‘false’.
Dot (.) operator:
- The dot operator is used to access the instance variables and methods of class objects.
Example: person.age; // reference to variable age
person.salary ( ); // reference to method salary ()
1.10 EXPRESSIONS
ARITHMETIC EXPRESSIONS
- An arithmetic expression is a combination of variables, constants, and operators arranged as per the
syntax of the language.
Axb-c a*b-c
(m+n)(x+y) (m+n)*(x+y)
ab/c a*b/c
EVALUATION OF EXPRESSION
- Expressions are evaluated using an assignment statement of the form
Variable = expression
- When the statement is encountered, the expression is evaluated first and the result then replaces the
previous value of the variable on the left-hand side.
Ex:
X = a * b – c;
Z = a – b / c + d;
18
III B.Sc CS Java Programming Siri PSG Arts & Science College for Women
Casting a value
- Java performs type conversions automatically.
- There are instances when we want to force a type conversion in a way that is different from the
automatic conversion.
Ex: Ratio = (float) female_number / male_number;
- The process of such a local conversion is known as casting a value. The general form is
(type-name) expression
Ex:
X = (int) 7.5 // 7.5 is converted to integer by truncation
Z =(int) a+b // a is converted to integer and then added to b;
19