Introduction To Java
Introduction To Java
Introduction To Java
The wrapping up of data (i.e. characteristics) and functions (i.e. behaviour) into a
import keyword single unit (called class) is known as Encapsulation.
It a predefined reserved keyword to make a package available to a program. The data is hidden inside the class and cannot be accessed outside the class. It can
only be accessed through the methods present inside the class. This hiding of data
DEBUGGING from direct access is known as Data Hiding.
It is the process of identifying and resolving errors in a program.
It is of three types: ABSTRACTION
The act of representing essential features without knowing its internal details is
1. Compile time (Syntax) error known as Anstraction
It occurs if the rules of the programming language are not followed in the
source code. It occurs due to incorrect punctuation, incorrect word etc. Example: A person need not know the internal mechanism of the working of the car.
INHERITANCE
2. Runtime error
It occurs during the execution of the program due to certain unexpected It is the process of deriving one class from another class. It transfers the properties
conditions in the program code. Eg. Division by zero of parent class to sub-class.
IDENTIFIERS
POLYMORPHISM Identifiers are the names given to different parts of the program, like variable,
constants, class object, function, arrays etc.
‘Poly’ means ‘many’ and ‘morph’ means ‘form’. The ability to take more than one
form is known as Polymorphism. It is the process of using a function for more than
Rules for naming an identifier :
one purposes.
1. An identifier can have alphabets, digits, underscore and dollar sign.
MESSAGE PASSING
2. An identifier should not be a keyword.
The calling of the function by its name to perform any task is called message
3. The name should not start with a digit.
passing.
4. The name can be of any length.
JAVA CHARACTER SET 5. Name is case sensitive which means upper case and lower case are
treated in different manner.
Character set is a set of valid characters that a language can understand. A
character represents any letter, digit or a special symbol. Java uses Unicode
character set. Example:
Sample tot_marks sum stud1
TOKENS
cust_code _CHK isDigit SALARY
The smallest individual unit in a program is known as token.
Java provides following tokens:
PUNCTUATORS (Separators)
1. Keyword 4. Operator
2. Identifiers 5. Literal Separators are the characters which are used to separate any two statements, block
3. Punctuators of statements, expressions, variables etc. There are 9 separators used in Java.
KEYWORDS () Brackets
{} Curly Braces
Keywords are reserved words which have a special meaning. They are used for a
[] Square Bracket
specific purpose. The name of the keyword is not used as the name of any identifier.
Following are the keywords used in java: ; Semilcolon
, Comma
. Full Stop
abstract default if private this
LITERALS
boolean do implements protected throw
Literals are data items which are fixed data values. Java provides following literals:
break double import public throws 1. Integer literal
2. Floating Literal
byte else instanceof return transient 3. Boolean literal
case extends int short try 4. Character Literal
5. String literal
catch final interface static void 6. Null literal
char finally long strictfp volatile
• Integer Literal
class float native super while
An integer constant has a digit and does not contain any decimal point. The
const for new switch continue digit can have a – or – sign, which means it can be either positive or negative.
Example: 4 -9 67
goto package synchronized
\’ Single quote
• Floating Literal
\” Double quote
A floating or real literal is a number having a decimal value or fractional part.
They may be written as a fraction or exponent form. DATATYPES
Example: 34.5 -17.9 135E05
• Boolean Literal Data type specifies the type of data type used in the program. There are two types
of datatypes available in Java:
A Boolean literal has two types of value, represented as true or false. The
default value of Boolean data type is false. ▪ Primitive data type
▪ Reference data type OR Non-Primitive data type
• Character Literal
Reference data type
A character literal contains one character, which is enclosed within single
quotation marks. A reference data types give the reference of the values. It refers to the memory
Example : ‘a’ ‘@’ ‘\n’ ‘c’ address of the value using a pointer. The address of the value or a set of values is
stored in the name of the variable. Class, arrays and interface are examples of
• String Literal reference data type.
A string literal is a combination of characters forming a word or a sentence. It Primitive data type
is enclosed within double quotation marks.
Example: “java” “How are you?” Primitive data types are the fundamental or the pure data type offered by Java. The
word ‘primitive’ means a fundamental component that may be used to create other
• Null Literal larger parts. Java supports following four types of primitive data types:
Null means ‘nothing’ or ‘no value’. A null literal has a null value, which means it ▪ Numeric Integral primitive types
is empty without any value. ▪ Fractional primitive types
▪ Character primitive types
ESCAPE SEQUENCE ▪ Boolean primitive types
Java allows nongraphic characters. The nongraphic cannot be typed directly from
the keyboard viz. backspace, carriage return, tab etc. These can be represented by i. Numeric Integral primitive types
escape sequence. An escape sequence is represented by a backslash (\) followed It stores whole numbers, which means numeric values without any decimal
by a character. Following table lists escape sequences: point. The value can be positive or negative. There are four numeric integral
types in Java.
byte int
Escape Sequence NonGraphic Character short long
\a Audible alert ii. Fractional primitive types
\b Backspace It stores numeric values having a fractional part, which means the value
\f Formfeed having a decimal point. The value can be positive or negative. They are known
as floating-point datatypes. The floatinf values are represented by two
\n New line
datatypes:
\r Carriage return float double
\t Horizontal tab
iii. Character primitive types
\v Vertical tab
\\ Backslash
It is used to store characters. A variable defined as char type stores one
character. It can represent all ASCII (American Standard Code for
Information Interchange) as well as Unicode characters. Java supports It is an identifier which holds a value. The value of variable can be changed during
Unicode, which makes the size of char type as 16 bits, i.e., 2 bytes, as the execution of a program.
compared to 1 byte character size of other programming language. Following
Declaration of a variable:
is the data type used to represent character type.
datatype variablename;
char
Example:
iv. Boolean primitive types
int sum;
Boolean type is used to represent a single value which can be either true or float salary;
false.The words true and false are the boolean values. The term boolean is
after a great Mathematician George Boole, who believed that many problems
can be solved with the help of true and false values. The datatype used to CONSTANTS
represent a boolean value is as follows:
boolean It is an identifier which holds a value but the value can not be changed during the
execution of a program. The keyword ‘final’ is added before the declaration which
makes it a constant. Hence, the value can not be changed in the program.
Following table lists the size and range of all datatypes: Declaration of constants:
Single-
32 bits (4 precision -3.4 E + 38 to Precision upto 6
Float
bytes) Floating +3.4 E + 38 digits
point
Double-
64 bits (8 precision -1.7 E +308 to 1.7 Precision upto 15
Double
bytes) Floating E +308 digits
point
Single
16 bits (2
Char character 0 to 65,535 Unicode characters
bytes)
Java
reserves 8 Used for logical test
Boolean bits but Boolean true or false along with if
uses only 1 value statement
bit
VARIABLE
OPERATORS IN JAVA == Equal to
An operator is a symbol used to perform calculation. The objects on which the != Not equal to
operator operates are known as operands. Depending upon the number of
operands, there are two types of operators:
Unary Operator: It operates on only one operand. LOGICAL OPERATOR
These operators are used to check for multiple conditions. When there are more
+ - than one if conditions , then they can be used.
• Unary Plus ( + ) Operator Meaning
&& Logical AND
• Unary Minus ( - )
|| Logical OR
Example: ! Logical NOT
If a = 6, then –a means –6
If a = -10, then +a means –10
Logical AND (&&- Ampersand)
If a = 34, then +a means 34
It checks for two conditions, it evaluates to true if both the conditions are true else
Binary Operator: It operates on two operands. it returns false.
+- * / % Condition 1 Condition 2 Result
Example: True True True
5 + 6, results in 11 True False False
a * 2, results in 8, if a=4 False True False
False False False
In pre increment, first the increment is done and then the expression is solved. The ?: operator is known as conditional operator or ternary operator. It requires
three operands to operate, hence the name Ternary Operator. It is a shortcut and
int a=5, b=2; easier method to use if…else statement. The condition is written before ‘?’ followed
b = b * ( + + a ); by true statement and after ‘:’ follows the false statements.
=2*(++5)
=2*6 Syntax
= 12 var = condition ? True: False
After change : a = 6, b=12 Example
if(a>b)
Post – Increment (Use – then – change ) x=a;
else
Post increment means the expression is solved first and then increment is done. x=b;
Brackets [ ], ( ) are given higher priority which means brackets are solved first. It is 2. x = a + c – b
followed by Multiplication, Division, Modulus then Addition and Subtraction. x=(a+c)–b // Addition & Subtraction have same
Multiply, Division, Modulus are at the same priority level, hence to solve them, the precedence, so Left to Right
rule of Left to Right is followed, which means the operator present at the left side = ( 10 + 7 ) – 3
is solved first. Same is the case with Add and Subtract. Both are at the same level, = 17 – 3
so rule of Left to Right is followed. = 14
3. x = a + ( c – b )
Example: = 10 + ( 7 – 3 ) // First bracket is solved
x=a+b-c = 10 + 4
In this expression, since + & - are at the same level, left to right is followed which = 14
means first a+b is solved and then c is subtracted from the result. 4. x = ( a + b + c ) % a
But if bracket is present in the expression then the expression is solved according = ( 10 + 3 + 7 ) % 10
to the bracket = 20 % 10
x=a+(b–c) =0
Since bracket gets a higher precedence, hence first the expression inside the 5. x = a * b % c
bracket is solved. First (b-c) is solved and then the result is added to a. x=(a*b)%c // Multiplication & Division are at the same level,
so Left to Right
= ( 10 * 3 ) % 7
OPERATOR ASSOCIATIVITY
= 30 % 7
=2
An associativity rule determines the grouping of operands and operators. In the
expression, more than one operator is present having the same precedence. When
the precedence of the operators is same, then the expression in evaluated from left
to right.
Example:
x = a + b - c;
In the above example, + & - have the same precedence. In this case, it is
evaluated from left to right, i.e. first addition is done followed by subtraction. If the
expression is given as
x = a + ( b – c );
In this example, brackets are given high priority, so first subtraction is done and
then addition.
OUTPUT STATEMENT Difference between Interpreter and Compiler
The output statement or the print statement is used to print the result or output on Interpreter is a translator, which converts a source code into machine code, line by
the screen. The print statement in Java is line.
Compiler is a translator, which converts a source code into machine code, all at
System.out.print ( ); once.
Example:
System.out.println (“HELLO JAVA”);
Output : Hello Java
System.out.println (“Sum is”+ sum);
Output : Sum is 30
System.out.println (a+b);
Output : 20 30
System.out.println (“a=”+a);
Output : a=20
System.out.println (“a=”+ a +”b=”+ b);
Output : a=10 b=20
Q3. What will be the output of
PRACTICE WORKSHEET
(i) 42 / 7 =_____ (ii) 42 % 7 =_____ (iii) 15/4 =_____ (iv) 15%4 = _____
Q1. Solve the following expression:
Q4. If a = 57, b=3, then int c= a / b ____________
(i) if x = 2, then evaluate: a) ++x > =5 b) x ++ < =5
(ii) if x = 82, then evaluate: a) y = ++x b) y = x ++ Q5. What will be the output for the following program segment?
(iii) if x=20, evaluate a) y= (++x) % 4 b) y = (x++) % 4 i) int a = 10,b=30,c=40;
(iv) If x = 9, y = 5, determine the value of x after executing x += (++x) % y a= --b + c * a / 2;
(v) If a = 6, b = 4.5, then solve the following expression: System.out.print("a" + a);
a) x = ( ++a) + b + a b) x = ( a++) % b c) x = a * b % a System.out.print("b " +b );
vi) What will be the value of x after evaluating x += x ++ + --x + 4 ; ii) int x=13, y=5 ;
if x = 3 before the evaluation? System.out.println (x++ + x + --y );
vii) What will be the value if x=3 initially, x+=x++---x+x -- iii) int c=900, x, n =2000;
viii) What will be the value if a=5 initially a- - + ++a%4
x = n+ c >2550? 1350 : 1500 ;
(ix) What will be the result stored in x and y after evaluating the following: System.out.println(x);
Assume that initially x = -2 and y = 5. Q6. Write the output of following statements :
(i) x = ++y – x (ii) y + = x class Demo
(x) Find the result of the following expression if the values of x=10, y=5 & z=11 {
(i) x++ * y + z (ii) ++x * y + z public void fmn( )
(xi) If x = 10 , y= 5, calculate the value of z = ++x - y-- + x++ + y – x {
int a=12,b=10;
(xii) If a = 10, then solve the following expression:
System.out.println (“a++=”+a++);
a) x = ( a++) + (a++) + ( ++a)
System.out.println (“a>b=”+a>b);
b) x = a + ( a++) + ( ++a) + (++a) + (a++) % a
System.out.println (“++a=”+++a);
c) a+=a + (++a) + (a++) + a + a++
System.out.println (“--a=”+--a);
d) x= ++a + ++ a + ++a + a++ -a
System.out.println (“a!=(a+b)=”+a!=(a+b));
e) a+=++a /a
}
Q2. Suppose A,B,C are integer variables where A = 3, B = 3, C = 5 and X,Y,Z }
are the floating point variables where X = 8.8, Y = 3.5, Z = -5.2. Determine the
value of the following expressions:
End of Worksheet
i) A%C iv) X / Y
ii) ( A * C ) % B
iii) X / ( X+Y ) v) (int) X % (int) Y