Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

PDC –CS –Department JAVA PROGRAMMING SAE4A

Unit 1: Input/Output Files: Concept of Streams-Stream Classes-Byte Stream

Introduction to Java-Features of Java-Basic Concepts of Object Oriented Classes-Character Stream Classes – Using

Programming-Java Tokens-Java Statements-Constants-Variables-Data Streams-Using the File Class-Creation of Files-Random Access Files-
Types- Type Casting-Operators-Expressions-Control Statements: Other Stream Classes.
Branching and Looping Statements.
Unit-5:
Unit-2: Network basics –socket programming – proxy servers – TCP/IP – Net
Classes, Objects and Methods-Constructors-Methods Overloading- Address – URL – Datagrams -Java Utility Classes-Introducing the AWT:
Inheritance-Overriding Methods-Finalizer and Abstract Methods- Working with Windows, Graphics and Text- AWT Classes- Working with
Visibility Control –Arrays, Strings and Vectors-String Buffer Class- Frames-Working with Graphics-Working with Color-Working with
Wrapper Classes. Fonts-Using AWT Controls, Layout Managers and Menus.

Unit 3:
Interfaces-Packages-Creating Packages-Accessing a Package-
Multithreaded Programming-Creating Threads-Stopping and Blocking a
Thread-Life Cycle of a Thread-Using Thread Methods-Thread Priority-
Synchronization-Implementing the Runnable Interface
Unit-4:
Managing Errors and Exceptions-Syntax of Exception Handling Code-
Using Finally Statement-Throwing Our Own Exceptions-Applet
Programming-Applet Life Cycle-Graphics Programming-Managing

Prepared by S.P.CHITRA PREETHA Page 1


PDC –CS –Department JAVA PROGRAMMING SAE4A

HISTORY OF JAVA 6. Familiar, simple and small


James Gosling initiated Java language project in June 1991 for use in one 7. Multithreaded and Interactive
of his many set-top box projects. The language, initially called ‘Oak’ after 8. High performance
an oak tree that stood outside Gosling's office, also went by the name 9. Dynamic and Extensible
‘Green’ and ended up later being renamed as Java, from a list of random 1. Compiled and Interpreted
words. ➢ Basically a computer language is either compiled or interpreted.
Java comes together both these approaches.
Sun released the first public implementation as Java 1.0 in 1995. It
➢ Java compiler translates Java code to Bytecode .
promised Write Once, Run Anywhere (WORA), providing no-cost run-
➢ Java Interpreter generate machine code that can be directly
times on popular platforms.
➢ executable code..
On 13 November, 2006, Sun released much of Java as free and open 2. Platform Independent and portable
source software under the terms of the GNU General Public License ➢ Java programs can be easily moved from one computer system to
(GPL). another and anywhere.
➢ Changes and upgrades in operating systems, processors and
On 8 May, 2007, Sun finished the process, making all of Java's core code
➢ system resources will not force any alteration in Java programs.
free and open
➢ This is reason why Java has become a trendy language for

FEATURES OF JAVA ARE AS FOLLOWS: ➢ programming on Internet which interconnects different kind of

1. Compiled and Interpreted ➢ systems worldwide.

2. Platform Independent and portable ➢ Java certifies portability in two ways.

3. Object- oriented 1. First way is, Java compiler generates the bytecode and that

4. Robust and secure can be executed on any machine.

5. Distributed

Prepared by S.P.CHITRA PREETHA Page 2


PDC –CS –Department JAVA PROGRAMMING SAE4A

2. Second way is, size of primitive datatypes are machine ➢ Java is very small and simple language. Java does not use pointer
independent. and header files, goto statements, etc.
➢ It eliminates operator overloading and multiple inheritance.
3. Object- oriented 7. Multithreaded and Interactive
Java is truly object-oriented language. ➢ Multithreaded means managing multiple tasks simultaneously.
In Java, almost everything is an Object. ➢ Java maintains multithreaded programs. That means we need not
4. Robust and secure wait for the application to complete one task before starting next
➢ Java is a most strong language which provides many securities to task. applications.
make certain reliable code. 8. High performance
➢ It is design as garbage –collected language, which helps the ➢ Java performance is very extraordinary for an interpreted language,
programmers virtually from all memory management problems. majorly due to the use of intermediate bytecode.
➢ Java also includes the concept of exception handling, which detain 9. Dynamic and Extensible
serious errors and reduces all kind of threat of crashing the system. ➢ Java is also dynamic language.
➢ Security is an important feature of Java and this is the strong reason ➢ Java is capable of dynamically linking in new class, libraries,
that programmer use this language for programming on Internet. methods and objects.
➢ The absence of pointers in Java ensures that programs cannot get ➢ Java program is support Methods written in other language
right of entry to memory location without proper approval. such as C and C++, known as native methods.
5. Distributed
➢ Java is called as Distributed language for construct applications on
networks which can contribute both data and programs.
➢ Java applications can open and access remote objects on Internet
easily.
6. Simple and small
Prepared by S.P.CHITRA PREETHA Page 3
PDC –CS –Department JAVA PROGRAMMING SAE4A

OBJECT ORIENTED CONCEPTS


There are some basic concepts of object oriented programming as 3. Data Abstraction
Data abstraction refers to the act of representing important description
follows:
without including the background details or explanations.
1. Object 4. Data Encapsulation
2. Class • Data Encapsulation means wrapping of data and Methods into a
3. Data abstraction single unit.
4. Data encapsulation 5.Inheritance
5. Inheritance
6. Polymorphism
7. Dynamic binding
1. Object

• Objects are important runtime entities in object oriented method.

• Each object holds data and code to operate the data


For example: Accruing the properties of others.Main advantage of inheritance is
reusability.
6. Polymorphism
One in many form.

2. Classes
• A class is a set of objects with similar properties (attributes),
common behaviour (operations).

• A class is a collection of objects of similar type.

Prepared by S.P.CHITRA PREETHA Page 4


PDC –CS –Department JAVA PROGRAMMING SAE4A

TOKENS Literals are java tokens containing set of characters. Literals are used to represent a
constant that has to be stored in a variable.e.g 2,5,4.6,”ram”,’s’ etc.
Operators
Operators are java tokens containing a special symbol and predefined meaning in Java.
Operators can be used with one or more operands to achieve a result.e.g
unary,binary,ternary operators
Variables
Variables are used to hold the value.e.g x = 10
Separators
Separators are used to separate the tokens e.g , ) .

STATEMENTS

Statements are used to carry out some action,


Reserved Keywords
Reserved keywords are java tokens with predefined meaning. Java has 60 TYPES

reserved keywords.e.g int,class,break,continue etc. 1. Expression statement

Identifiers 2. Labeled statement


Identifiers are java tokens designed and decided by the java programmer. Examples for 3. Compound statement
java tokens namely identifiers are: name for the class, name for members of the class,
4. Control statement
and temporary variables in class methods.
5. Iteration statement
Literals
6. Jump statement
Prepared by S.P.CHITRA PREETHA Page 5
PDC –CS –Department JAVA PROGRAMMING SAE4A

7. Synchronization statement

8. Guard Statement Selection statement:

Expression statement: Selects one of the several controls.

An expression followed by a semicolon. Ex:

Ex: If statement

A=3; switch

A=4+3; Iteration statement:

Labled statement: Says how and when the looping will take place

A statement starts with a label. Ex:

Ex: For loop

10 : A=3; While

Compound statement: Jump statement:

Several individual statement enclosed within the Pass the control to the beginning or end of the block.
braces. Ex:
Ex: Break
{ Continue
A=3; synchronize statement:
A=4+3; These are the statement used in multithreading.
}
Prepared by S.P.CHITRA PREETHA Page 6
PDC –CS –Department JAVA PROGRAMMING SAE4A

Ex: Example : int a=100;

synchronize Real Constants

Guard statement: 1. A real constant must have at least one digit

These are the statement used in Exception Handling. 2. It must have a decimal point

Ex: Example : float a=10 .05;

Try,catch Character constants

1. A character constant is a single alphabet, a single digit or a single

CONSTANTS special symbol.


2. Enclosed within single quotes.
➢ Constants are variables which cannot be change during the
Example : char a=’M’;
execution of the program.
String constants
Types of C constant:
1. A group of character is called as string constant.
1. Integer constants
2. Enclosed within double quotes.
2. Real or Floating point constants
Example : char a[4] = “java”;
3. Character constants
Backslash Character Constants:
4. String constants
1. There are some characters which have special meaning in java
5. Backslash character constants
language.
Integer Constants
2. They should be preceded by back slash symbol
1. An integer constant must have at least one digit. Example :

2. It must not have a decimal point.


Prepared by S.P.CHITRA PREETHA Page 7
PDC –CS –Department JAVA PROGRAMMING SAE4A

DATA TYPE
The data type defines the kind of data that is represented by a variable. Integer data type:
There are two types of data types Integer datatype can hold the numbers (the number can be
➢ primitive data type positive number or negative number). In Java, there are four
➢ non-pimitive data type types of integer as follows:
In primitive data types, there are two categories 1. byte
➢ numeric means Integer, Floating points 2. short
➢ Non-numeric means Character and Boolean 3. int
In non-pimitive types, there are three categories 4. long
➢ classes We can make ineger long by adding ‘l’ or ‘L’ at the end of the
➢ arrays number.
➢ interface Floating point data type:
It is also called as Real number and when we require
accuracy then we can use it.
There are two types of floating point data type.

Prepared by S.P.CHITRA PREETHA Page 8


PDC –CS –Department JAVA PROGRAMMING SAE4A

1. float 3.Multiple branching.


2. double 1.Conditional statements
It is represent single and double precision numbers. These statements are classified into
The float type is used for single precision and it uses 4 bytes for storage 1.Simple if
space. 2.if ….. else
The double type is used for double precision and it uses 8 bytes for storage 3.if ….. elseif ladder
space. 4.Nested if
Character data type: 1.Simple if
It is used to store single character in memory. It uses 2 bytesstorage space. If statement execute set of command when the condition is true
Boolean data type: syntax
It is used when we want to test a particular condition during the excution If (condition)
of the program. There are only two values that a boolean type can hold: {
true and false. Statement;
}
CONTROL STATEMENT In these type of statements, if the condition is true, then respective
This statements define the flow of control in a program block of code is executed.
They are Example program for if:
1.Conditional control statements. Class myclass
2. Conditional uncontrol statements. {
Conditional control statements. int a=10,b=5;
These statements arw classified into if (a==b)
1.Conditional {
2.Looping or iteration. System.out.System.out.println("a and b are equal");
Prepared by S.P.CHITRA PREETHA Page 9
PDC –CS –Department JAVA PROGRAMMING SAE4A

} else
} {
Output: a and b are equal System.out.System.out.println ("a and b are not equal");
2.if ….. else }
If the condition is true then the statements inside the if block will }
be executed otherwise the else block will be executed. Output: a and b are not equal
syntax 3.if ….. else ladder
If (condition) IThis type of nesting there is an if else statement in every else part
{ except the last part.
Statement; Syntax is :-
} if (condition)
else Statement1;
{ else if (condition)
Statements; statement2;
} else if (condition)
Example program for if: statement3;
class myclass else
{ statement4;
int a=10,b=5; 4.Nesting of if …else
if (a==b) When there are another if else statement in if-block or else-block,
{ then it is called
System.out.System.out.println ("a and b are equal"); nesting of if-else statement.
} Syntax is :-
Prepared by S.P.CHITRA PREETHA Page 10
PDC –CS –Department JAVA PROGRAMMING SAE4A

if (condition) 1.While loop


{ 2.do while loop
If (condition) 3.for loop
Statement1;
Else 1.While loop
statement2; Syntax:-
} while(condition)
Example program for if: {
class myclass Statements;
{ }
int a=10,b=5; Here first condition is checked if, it is true body of the loop is
if (a==b) executed else, If condition is false control will be come out of loop.
{ Example:-
If (a== c) class myclass
printf("a is greater"); {
else int i=1;
printf(“c is greater”); while(i<10)
} {
} printf("%d\n",i);
2.Looping or iteration. i++;
It is a block of statement that executes more than once until a }
particular no of condition is being satisfied. }
There are three types of loops in c Output : 123456789
Prepared by S.P.CHITRA PREETHA Page 11
PDC –CS –Department JAVA PROGRAMMING SAE4A

2.do while loop 3.for loop


Syntax:- for(initialized counter; test counter; update counter)
do {
{ Statement;
Statement; }
} Example:-
while(condition); class myclass
Here firstly statement inside body is executed then condition is checked. If {
the int i;
condition is true again body of loop is executed and this process continue for(i=1;i<10;i++)
until the {
condition becomes false. System.out.println (“ %d ”, i);
int class myclass }
{ }
int i=1; Output:-1 2 3 4 5 6 7 8 9
do Nesting of loop
{ When a loop written inside the body of another loop then, it is known as
printf("%d\n",i); nesting of loop.
i++; class myclass
} while(i<10) {
} int i,j;
for(i=0;i<2;i++)
for(j=0;j<5; j++)
Prepared by S.P.CHITRA PREETHA Page 12
PDC –CS –Department JAVA PROGRAMMING SAE4A

printf(“%d %d”, i, j); Output:


} 0
Output: i=0 Continue
j=0 1 2 3 4 Continue statement is used for continuing next iteration of loop after
i=1 skipping some statement of loop.
j=0 1 2 3 4 Syntax
38 continue;
Break statement(break) Example:-
Sometimes it becomes necessary to come out of the loop even before loop class myclass
condition becomes false then break statement is used. Break statement is {
used inside loop and switch statements. It cause immediate exit from that int n;
loop. for(n=2; n<=9; n++)
Syntax {
break; if(n==4)
Example : continue;
class myclass System.out.println (“%d”, n);
{ }
int j=0; }
for(;j<6;j++) Printf(“out of loop”);
if(j==4) }
break; Output: 2 3 5 6 7 8 9 out of loop
}

Prepared by S.P.CHITRA PREETHA Page 13


PDC –CS –Department JAVA PROGRAMMING SAE4A

switch case statement: case 2:


This is used to execute only specific case statements based on the switch System.out.println ("Value is 2 \n" );
expression. break;
Syntax : case 3:
switch (expression) System.out.println ("Value is 3 \n" );
{
break;
case label1;
statements; default :
break; System.out.println ("Value is other than 1,2,3,4 \n" );
case label2; }
statements;
break; return 0;
default: }
statements;
The difference between while & do while loop?
break;
} S.no while do while
Example program for switch..case:
Loop is executed for first time
Class myclass Control goes into the
irrespective of the condition. After
{ 1 while loop only when
executing the while loop for first time,
int value = 3; condition is true.
condition is being checked.
switch(value)
{
case 1:
System.out.println "Value is 1 \n" );
break;

Prepared by S.P.CHITRA PREETHA Page 14


PDC –CS –Department JAVA PROGRAMMING SAE4A

VARIABLE class Student

➢ It’s a Temporary memory location used to store values. {


➢ Values can be changed. String name;
Rules for naming variables
int age;
1.First letter should be an alphabet.
}
2.No white spaces or blank spaces
Here name and age are instance variable of Student class.
3.No special characters except underscore

4.keywords should not be used as variables.

Java Programming language defines mainly three kind of variables.


2) Static variables

1. Instance variables Static are class variables declared with static keyword. Static variables are
2. Static Variables initialized only once. Static variables are also used in declaring constant
3. Local Variables along with final keyword.

class Student

{
1) Instance variables
String name;
Instance variables are variables that are declare inside a class but outside
any method,constructor or block. Instance variable are also variable of int age;

object commonly known as field or property. static int instituteCode=1101;

Prepared by S.P.CHITRA PREETHA Page 15


PDC –CS –Department JAVA PROGRAMMING SAE4A

Here instituteCode is a static variable. Each object of Student class will ➢ Memory space is not allocated for a variable while declaration. It
share instituteCode property. happens only on variable definition.

➢ Variable initialization means assigning a value to the variable

General Format :
3) Local variables Datatype variablename1,
Local variables are declared in method constructor or blocks. Local variablename2,……….variablenameN;
variables are initialized when method or constructor block start and will be Example
destroyed once its end. Local variable reside in stack. Access modifiers are
int a,b,c;
not used for local variable.
float a,b;

Initializing a variable
float getDiscount(int price)
General Format :
{ Datatype variablename1= value , variablename2=

float discount; value,……….variablenameN=value;

Example
discount=price*(20/100);
int a=10 ,b =5;
return discount;
float a=10.5;
}
S.No Type Syntax Example
Variable declaration

➢ Variables should be declared in the C program before to use.

Prepared by S.P.CHITRA PREETHA Page 16


PDC –CS –Department JAVA PROGRAMMING SAE4A

Variable int x, y, z; char


1 data_type variable_name;
declaration flat, ch;

int x = 50, y = 30;


Variable data_type variable_name =
2 char flag = ‘x’, Relational Expression
initialization value;
ch=’l’; An relational expression is a combination of variables,
constants, and arithmetic expressionsand operators arranged as
per the syntax of the language.
EXPRESSIONS GF:

The combination of operators and operands is said to be an Constant or vble or Arithmetic exp relational operator
expression. Constant or vble Arithmetic exp

TYPES Eg x + y > a + b;

➢ Arithmetic expressions Logical Expression


➢ Relational Expression An Logical expression is a combination of relational
➢ Logical Expression expressions and operators arranged as per the syntax of the
Arithmetic expressions language.

An arithmetic expression is a combination of variables, GF:


constants, and operators arranged as per the syntax of the language. relational exp logical operator relational exp

GF: Eg x > y && a > b;


Evaluation of expressions
Constant or vble arithmetic operator Constant
Expressions are evaluated using an assignment statement of the
or vble
form
Eg x + y;
Prepared by S.P.CHITRA PREETHA Page 17
PDC –CS –Department JAVA PROGRAMMING SAE4A

variable = expression; 7. Conditional operators (ternary operators)

Eg:1) x = a * b –c

OPERATORS ARITHMETIC OPERATORS:


The symbols which are used to perform logical and Following table shows all the arithmetic operators
mathematical operations are called java operators supported by java language. Assume variable A holds 10 and
Example : A + B * 5 variable B holds 20 then.

where, Operator Description Example


▪ +, * - operators
A + B will give
▪ A, B - variables + Adds two operands
30
▪ 5 – constant
Subtracts second operand A - B will give -
▪ A + B * 5 - expression -
from the first 10
C language offers many types of operators. They are,
A * B will give
1. Arithmetic operators * Multiply both operands
200
2. Unary operators
Divide numerator by de- B / A will give
3. Assignment operators /
numerator 2
4. Relational operators

5. Logical operators

6. Bit wise operators


Prepared by S.P.CHITRA PREETHA Page 18
PDC –CS –Department JAVA PROGRAMMING SAE4A

Modulus Operator and > Checks if the value of left operand is greater (A > B) is
B % A will give
% remainder of after an integer than the value of right operand, if yes then not true.
0
division condition becomes true.

< Checks if the value of left operand is less (A < B) is


than the value of right operand, if yes then true.
condition becomes true.

RELATIONAL OPERATORS: >= Checks if the value of left operand is greater (A >= B) is
than or equal to the value of right operand, not true.
Following table shows all the relational operators supported
if yes then condition becomes true.
by java language. Assume variable A holds 10 and variable B
holds 20 then. <= Checks if the value of left operand is less (A <= B) is

Operator Description Example than or equal to the value of right operand, true.
if yes then condition becomes true.
== Checks if the value of two operands is equal (A == B) is
or not, if yes then condition becomes true. not true.
LOGICAL OPERATORS:
!= Checks if the value of two operands is equal (A != B) is
Following table shows all the logical operators supported by
or not, if values are not equal then condition true.
java language. Assume variable A holds 1 and variable B holds
becomes true.
0 then.

Operator Description Example

Prepared by S.P.CHITRA PREETHA Page 19


PDC –CS –Department JAVA PROGRAMMING SAE4A

Called Logical AND operator. If both (A | B) will


(A && B)
&& the operands are non zero then Binary OR Operator copies a give 61
is false. |
condition becomes true. bit if it exists in either operand. which is

Called Logical OR Operator. If any 0011 1101


(A || B) is
|| of the two operands is non zero then (A ^ B) will
true.
condition becomes true. Binary XOR Operator copies
give 49
^ the bit if it is set in one operand
Called Logical NOT Operator. Use to which is 0011
but not both.
reverses the logical state of its 0001
!(A && B)
! operand. If a condition is true then
is true. (~A ) will give
Logical NOT operator will make Binary Ones Complement
-60
false. ~ Operator is unary and has the
which is 1100
effect of 'flipping' bits.
BITWISE OPERATORS:
0011
Bitwise operator works on bits and performs bit by bit
Binary Left Shift Operator. The A << 2 will give
operation
left operands value is moved 240
Operator Description Example <<
left by the number of bits which is 1111
specified by the right operand. 0000
(A & B) will
Binary AND Operator copies a
give 12
& bit to the result if it exists in Binary Right Shift Operator.
A >> 2 will give
which is >> The left operands value is
both operands. 15
0000 1100 moved right by the number of

Prepared by S.P.CHITRA PREETHA Page 20


PDC –CS –Department JAVA PROGRAMMING SAE4A

bits specified by the right which is 0000 Unary Operator:


operand. 1111 Operator which acts upon a single operand.

Operator Description Example

Unary
To denote negative numbers -23 , -a
Assignment Operators: minus (-)

These are following assignment operators supported by java language Increment


operator To increase the value by 1 ++a (a=a+1)
Operator Description Example
(++)
Simple assignment operator,
C = A + B will assign Increment
= Assigns values from right side
value of A + B into C operator To decrease the value by 1 --a (a=a-1)
operands to left side operand
(--)
Conditional operators:
To return the size of operand in
Conditional operators return one value if condition is true and Sizeof int a sizeof a ans : 2
bytes
returns other value is condition is false.

This operator is also called as ternary operator.


TYPE CASTING
Syntax : (Condition? true_value: false_value);
Assigning a value of one type to a variable of another type is known
Example : (A > 100 ? 0 : 1);
as Type Casting.
Here, if A is greater than 100, 0 is returned else 1 is returned. This
Example :
is equal to if else conditional statements.

Prepared by S.P.CHITRA PREETHA Page 21


PDC –CS –Department JAVA PROGRAMMING SAE4A

int x = 10; • the two types are compatible


• the target type is larger than the source type
byte y = (byte)x;

In Java, type casting is classified into two types, Example :

public class Test


• Widening Casting(Implicit)
{

public static void main(String[] args)

int i = 100;

long l = i; //no explicit type casting required

• Narrowing Casting(Explicitly done) float f = l; //no explicit type casting required

System.out.println("Int value "+i);

System.out.println("Long value "+l);

System.out.println("Float value "+f);

Widening or Automatic type converion


}
Automatic Type casting take place when,
Output :

Prepared by S.P.CHITRA PREETHA Page 22


PDC –CS –Department JAVA PROGRAMMING SAE4A

Int value 100 System.out.println("Int value "+i);

Long value 100

Float value 100.0 }

Narrowing or Explicit type conversion

When you are assigning a larger type value to a variable of smaller type, }
then you need to perform explicit type casting.
Output :
Example :
Double value 100.04
public class Test
Long value 100
{
Int value 100
public static void main(String[] args)

double d = 100.04;

long l = (long)d; //explicit type casting required

int i = (int)l; //explicit type casting required

System.out.println("Double value "+d);

System.out.println("Long value "+l);

Prepared by S.P.CHITRA PREETHA Page 23

You might also like