Java Unit 1
Java Unit 1
Java Unit 1
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
FEATURES OF JAVA ARE AS FOLLOWS: ➢ programming on Internet which interconnects different kind of
3. Object- oriented 1. First way is, Java compiler generates the bytecode and that
5. Distributed
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
2. Classes
• A class is a set of objects with similar properties (attributes),
common behaviour (operations).
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
7. Synchronization statement
Ex: If statement
A=3; switch
Labled statement: Says how and when the looping will take place
10 : A=3; While
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
These are the statement used in Exception Handling. 2. It must have a decimal point
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.
} 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
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;
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.
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=
Example
discount=price*(20/100);
int a=10 ,b =5;
return discount;
float a=10.5;
}
S.No Type Syntax Example
Variable declaration
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;
Eg:1) x = a * b –c
5. Logical operators
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.
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.
Unary
To denote negative numbers -23 , -a
Assignment Operators: minus (-)
int i = 100;
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;