Documente Academic
Documente Profesional
Documente Cultură
Java Programming
Theory exam Marks 80-20
Practical exam Marks 50-25
Term work Marks 25
Unit I
Java Fundamentals
History of Java, Features of Java, Object oriented
concepts related to java,
Portability :
1. Java compiler generates byte code that can be implemented on
any machine
2. Size of the primitive data types are machine independent
Object-oriented
Almost everything in java is an Object
Object has data or state (instance variables)and behavior (methods)
Security
Security becomes important issue for a language that is used for
programming on Internet
Threats of viruses
Distributed
High performance
Encapsulation
Wrapping up of data and methods into a single unit is known as
encapsulation
The data can be accessed only through the methods which are wrapped in
the class
Inheritance
Inheritance is the process by which object of one class acquire the
properties of objects of another class
Message Communication
Eg :
Employee objEmployee;
objEmployee = new Employee();
OR
Employee objEmployee = new Employee();
objEmployee.cal_salary();
Java Program Structure
Documentation Section
This section comprises a set of comment lines giving the name of
the program , the author and other details
Package Statement
The first statement allowed in java file is a package statement
Import Statement
This is similar to the #include statement in c, c++
Interface Statement
An interface is like a class , generally used to implement multiple
inheritance
Class Definition
Java programs may contain multiple class definitions
Classes are the primary and essential elements of java program
Public : access specifier that declares the main method as unprotected and
therefore making it accessible to all other classes
Static : this method as one that belongs to the entire class and not a part of any
object of the class
This method is static because interpreter calls this method before creating any
objects
}//end of class
Type all code commands and file names exactly as shown , Java is
highly case sensitive
Java Tokens :
Reserved key words
Eg: abstract, continue , goto, package , this, super, throw, try, static ,
switch , final, finally etc
Identifiers
They are programmer designed tokens
Operators
Arithmetic, relational , bitwise , Shift , Conditional
(+,+=), (<,<=), (|, ^=) , (>>,<<) , (? :)
Separators
These are the symbols used to indicate where group of code are
divided and arranged
Eg: ( (), {}, [], ;, , , :, . )
Integer Data Types
Type Bits
Byte 8 -127 128
short 16 -32768 32767
int 32 -2,147,483,648..+7
long 64 -9,223,372,036,854,775,808..+7
Floating Point
A char value stores a single character from the Unicode character set
A character set is an ordered list of characters
A, B, C, , a, b, ,0, 1, , $,
The char type assumes a size of 2 bytes but basically it holds only a
single character
Boolean
A boolean value represents a true/false condition.
LD2\Conversion.java
class Student
{
String name=XYZ";
int roll_no;
void getData()
{
System.out.println ("Name is"+name);
}
}
class Display
{
public static void main (String args[])
{
Student objStudent = new Student();
objStudent.getData();
}
interface
interface Student
{
int getRollNum(int roll_no);
}
}
class MyInterface
Creation
int number [ ] = new int[5]; OR int number [ ] ;
number= new int [5];
Initialization
int number [ ] = {1, 2 ,3 ,4 ,5};
Array length
Eg:
int table [ ] [ ]= {
{0, 0, 0,},
{1, 1, 1 },
{2, 2, 2 }
};
OR
1. Instance variables //every object has its own personal copy of an instance
variable
2. Class variables //Class variables only have one copy that is shared by all the
different objects of a class
3. Local variables
Instance and class variables are declared inside a class, Instance variables are
created when the objects are instantiated and they are associated with the
objects
{
int x=10;
{
int y=5;
}
{
int z=4;
}
}
Constants
E. g.
final int x=10;
final double PI= 3.14
Advantages:
readability Constant declaration helps to improve the
readability of the program
efficiency It improves the efficiency as changing the value
becomes very easy
error detection Error detection becomes very easy
Arithmetic Expressions
ab/c a*b/c
x/y+c x/y+c
2x+1 2*x+1
LD2\Expressions.java
Operators
1.Arithmatic Operators
(+ - * / %)
2.Relational Operators
(< > <= >= == !=)
3.Logical Operators
(&& || !)
4.Assignment Operator
(=)
5.Incremental & Detrimental Operator
(++m,--m)
6.Conditional Operator
(a>b ? a: b)
7.Bitwise Operator
(<< >> >>> ` )
8.Special Operator
(. )
Control Structures
Selection statements
Iteration statements
Control Statements
If
If else
If else
else if
else if
.
.
default
if (condition)
{
statement 1;
}
else
{
statement 2;
}
switch used as an alternative to multiple if blocks
switch (expression)
{
case value1: //statement sequence
break;
Eg :
If else statement
switch (expression) {
case Constant1: // Do following if expression==Constant1
Bunch of Stuff
break;
case Constant2: // Do following if expression==Constant2
Bunch of Stuff
break;
default: // Do the following otherwise
Bunch of Stuff
break;
}
LD3\DemoSwitch.java
Iteration Statements
While loop
while ( condition )
{
Body of the loop
}
LD3\SampleWhile.java
do while loop
Loop will execute at least one time even if the condition is false
do
{
}
while ( condition )
LD3\DemoDoWhile.java
for loop
When the for loop first starts the initialization portion of the loop is
executed , which sets the value of the loop control variable which acts
as a counter that controls the loop
The initialization expression is executed only once
{
body of the loop
continue label;
}
LD3\ContinueLabel.java
1.WAP to demonstrate array structure
Eg: read and display array elements
Each class has a name, attribute and operations which provides the
behavior for the object
Field Declaration
Class Rectangle
{
int length , width ;
}
Method Declaration
{
method body;
}
LD4\DemoConBox.java
Copy constructor
LD4\ConstructorDemo.java
Method Overloading
LD4\OverLoadDemo.java
Static
Java creates only one copy of a static variable which can be used
even if class is never actually instantiate
LD4\DemoStatic.java
LD4\Nesting.java
1. Write a class Rectangle with members as length and width and a
constructor that initializes length and width to the given values.
Write a function that returns the area of rectangle.
2. Write a class Test with a function that takes the parameter by value.
3. Write a class Test with a function that takes the parameter as object.
String
String is immutable ( once created can not be changed )object .
The object created as a String is stored in the Constant String Pool .
Every immutable object in Java is thread safe ,that implies String is
also thread safe . String can not be used by two thread
simultaneously.
String once assigned can not be changed .
Next, you want to append Guest to the same String. What do you do?
Due to this it does not allow two threads to simultaneously access the
same method . Each method can be accessed by one thread at a time .
whereas StringBuilder is not synchronized( which implies it isnt thread
safe).
String s = Hello;
s = s + World;
system.out.println(s);
String firstname;
Or
String firstname = new String(anil);
replace (char oldChar, char newChar) Returns a new string resulting from
replacing all occurrences of oldChar in this string with newChar.
Trim() Returns a copy of the string, with leading and trailing whitespace
omitted.
valueOf(char c)
Returns the string representation of the char argument.
StringBuffer functions
insert()
This is the insert() function used to insert any string or
character at the specified position in the given string.
reverse()
This is the reverse() function used to reverse the string
present in string buffer.
setCharAt()
This is the setCharAt() function which is used to set the
specified character in buffered string at the specified position
of the string in which you have to set the given character.
charAt()
This is the charAt() function which is used to get the
character at the specified position of the given string.
substring()
This is the substring() function which is used to get the sub
string from the buffered string from the initial position to end
position (these are fixed by you in the program).
substring()
This is the substring() function which is used to get the sub string from the
buffered string from the initial position to end position (these are fixed by
you in the program).
deleteCharAt()
This is the deleteCharAt() function which is used to delete the specific
character from the buffered string by mentioning that's position in the string.
length()
This is the length() function is used to finding the length of the buffered
string.
delete()
This is the delete() function is used to delete multiple character at once
from n position to m position (n and m are will be fixed by you.) in the
buffered string.
capacity()
This is the capacity() function is used to know about the current characters
kept
..\Unit2\LD2\SortStringNum.java
..\Unit2\LD2\StringDemo.java
..\Unit2\LD2\stringBufferDemo.java
..\Unit1\LD4\DistructorDemo.java