Overview of C, C++, and Java Programming
Overview of C, C++, and Java Programming
======
--> it is a Procedure or Structure Oriented Programming Language
--> It was Developed by "Dennis Ritchie" in the year 1972.
c++
===
--> c++ is derived from c-lang(basic syntax) and
simula-67(class concepts)
--> It was developed "Bjarne Stroustrup" in the year 1980.
--> it was Initially called "c with classes"
Java
==
--> it is a Pure Object Oriented Programming Language..
Define OOP
=========
--> representing the relationships b/w Objects
--> Object holds Datas and Methods
--> It is derived from c[Basic Syntax] and c++[OOPs]
--> it is mainly used for Developing Internet based applications
--> it was developed by sun-micro system Engineers in the year1991(James
Gosling,Chris Warth,Patrick Naughton)
--> it is a Plat-form independent Language
--> it is a compiler and Interpreter Language
--> it is a case-sensitive Language
*) RMI
*) Servlets
*) EJB
*) JSP
*) Struts
*) Hibernate
*) Springs
*) NetBeans
*) Eclipse
Packages
=======
--> collection of classes and Interfaces
*) [Link]
*) [Link](Default Package)
*) [Link]
*) [Link]
"import" statement is used to access the classes from the Specified package.
Output Statements
=============
*) [Link]()
*) [Link]()
Ex:
==
[Link]("hi..");
[Link]("Welcome to Java");
o/P:
==
Hi..Welcome to Java
Ex:
==
[Link]("hi..");
[Link]("Welcome to Java");
o/P:
==
Hi..
Welcome to Java
Java code Execution Process
====================
|| javac [Link]
|| java test
*) BufferedReader
*) DataInputStream
*) char read() --> used to read a single character data from the user
and returns in the form of integer(ASCII value)
A --> 65
a --> 97
char ch;
[Link]("Enter a character : ");
ch=(char)[Link]();
[Link]("The Given character is "+ch);
o/p:
==
Enter a Character : s
The Given character is s
*) String readLine() --> used to read a String values from the user and also
returns as a String
Ex:
==
String s;
[Link]("Enter ur name : ");
s=[Link]();
[Link]("ur name is "+s);
o/p:
==
Enter ur name : nawin
ur name is nawin
Ex:
==
int a;
[Link]("Enter a value : ");
a=[Link]([Link]());
[Link]("a = "+a);
o/p:
==
Enter a value : 100
a = 100
*) datatypes
1. Integral Datatype
2. Floating Datatype
--> float (4 bytes)
--> double (8 bytes)
3. character datatypes
--> char (2 bytes)
--> String --> it is a class
Ex:
==
String s="java";
4. boolean Datatype
--> boolean (true/false)
*) variables
*) operators
Expression:
========
--> combination of operators and operands
Ex: a + b
a,b --> operands
+ --> Operator
Types of Operators
==============
[Link] Operators(+,-,*,/,%)
Ex:
==
int a=10,b=2,c;
c=a/b; //c=5;
c=a%b; //c=0;
[Link] Operators (<,>,<=,>=,==,!=)
--> used to compare two values and produces the result as either true
or false
[Link] Operators
Ex : int a=10,b=20,c=15;
[Link] Operators
--> used to perform operations based on binary digits
*) bitwise AND (&)
*) bitwise OR ( | )
Ex : int a=14,b=10,c;
c=a&b; //c=10;
c=a| b; //c=14;
16 8 4 2 1
14 --> 0
1 1 1 0
10 --> 0
1 0 1 0
-------------
0 1 0 1 0
-------------
0 1 1 1 0
------------
[Link] Operators(=)
--> used to assign the values to a Variable
syntax
===
varname=value;
Ex: int a;
a=100;
6. Arithmetic Assignment Operators(Short-hand assignment operator)
--> used to assign the Expression to a Variable
Ex:
==
int a=10,b=20;
a=a+b; // a+=b;
a=a-b; // a-=b;
a=b+c; //
syntax
=====
Exp1 ? Exp2 : Exp3 ;
(cond) T F
Ex:
==
int a=10,b=8,max;
max=(a>b)?a:b;
[Link]("max = "+max);
o/p:
==
max = 10
Increment[++]
------------
--> used to increment the value by 1 from its current value
syntax : ++varname;
syntax
=====
varname++;
Ex: int a=10,b;
==
b=a++; //a=11;b=10;
*) operands
*) keywords
--> keywords are resrved words, they already defined in the Compiler
--> we cant change the meaning of those keywords
--> we cant use the keyword name for variable
*) Identifiers
--> user-defined things like variable,classes and methods are considered
as " Identifiers"
Control Statements
==============
--> used to change the Flow of Execution of a Program
syntax
=====
if(cond)
{
//true block statements
}
Ex:
==
int a=3;
if(a>0)
{
S.o.p("Positive Number..");
}
syntax
=====
if(cond)
{
//true block statements
}
else
{
//false block statements
}
Ex:
==
int a=10,b=14;
if(a>b)
{
S.o.p("a is greatest..");
}
else
{
S.o.p("b is greatest..");
}
syntax
=====
if(cond-1)
{
//statements1
}
else if(cond-2)
{
//statements2
}
.
.
else if(cond-n)
{
//statement-n
}
else
{
//default statements
}
Ex:
==
int mark=35;
if(mark>=90)
S.o.p("Grade: A");
else if(mark>=70)
S.o.p("Grade : B");
else if(mark>=40)
S.o.p("Grade : C");
else
S.o.p("Grade : D");
[Link] If statement
==============
--> if inside another if is called "nested if"
--> it is also used to check more than one condition
syntax
====
if(cond-1)
{
if(cond-2)
//statements
else
//statements
}
else
{
if(cond-3)
//statements
else
//statements
}
// program to check greatest among given three nos
int a=10,b=20,c=56;
if(a>b)
{
if(a>c)
S.o.p("a is greatest..");
else
S.o.p("c is greatest..");
}
else
{
if(b>c)
S.o.p("b is greatest..");
else
S.o.p("c is greatest..");
[Link]() statement
=============
--> it is similar to else if ladder statement
--> used to compare a single value with list values and where match is found
that the statements are Executes
syntax
=====
switch(expression)
{
case 1:
//stmts
break;
case 2:
//stmts
break;
.
.
case n:
//stmts
break;
default:
//stmts
break;
}
Ex:
==
int no=1;
switch(no)
{
case 1:
S.o.p("ONE");
break;
case 2:
S.o.p("TWO");
break;
case 3:
S.o.p("THREE");
break;
default:
S.o.p("no is > 3");
break;
}
Looping Statements
=============
---> used for repeated Execution of Particular stmts in a Program
*) Initialization
*) Condition
*) Inc/Dec
--> syntax
Ex:
===
//print nos from 1 to 10
int i;
for(i=1;i<=10;i++)
{
S.o.p(i);
}
[Link] loop
========
--> it is also "Entry Check Loop"
--> it is similar to for loop
syntax
=====
initialization;
while(condition)
{
//statements
//inc/dec
}
[Link]..while loop
===========
--> it is a bottom or exit check loop
syntax
===
initialization;
do
{
//stmts;
//inc/dec
}while(cond);
int i;
i=10;
do
{
S.o.p(i);
i++;
}while(i<=20);
Nested Loops
========
--> loop inside another loop is called "Nested Loops"
--> used to show the Data in the Form of rows and cols
syntax
=====
for(init-1;cond-1;inc/dec) (row)
{
for(init-2: cond-2; inc/dec) (column)
{
//inner loop stmts
}
//outer loop stmts
}
Ex:
==
1 1 1
2 2 2
3 3 3
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
[Link](i+"\t");
}
[Link]();
}
Ex:
==
1)
1 2 3
1 2 3
1 2 3
2)
1
2 3
4 5 6
7 8 9 10
foreach loop
=========
--> used to iterate the control through a raw of values
syntax
=====
for(datatype var:arrayname)
{
//loop statements
}
Ex:
==
for(int s : a)
{
[Link](s);
}
break
====
--> it is used to terminate the block or loop
--> it must be placed inside the Looping or decision making stmts
syntax
=====
break;
Ex:
==
for(i=1;i<=10;i++)
{
S.o.p(i);
if(i==5)
break;
}
continue
======
--> it is used to bypass the control to next iteration(re-initialization step)
--> The statements after the "continue" will not be executes inside the loop.
syntax
=====
continue;
Ex:
==
for(i=1;i<=10;i++)
{
if(i<=5)
{
continue;
}
S.o.p(i);
}
Arrays
=====
--> Group of similar data items, which can shares the Common Variablename and
are stored in sequence memory locations
--> Array Index starts with "0" and ends with "n-1"
*) Numeric Array(1-D,2-D)
*) Non-Numeric Array(1-D)
syntax
=====
datatype varname[];
Ex:
==
int a[];
syntax
===
varname=new datatype[size];
Ex:
==
a=new int[5];
or
Ex:
===
a[0]=10;
.
a[4]=50;
Array Initialization
============
*) Compile-time
*) Run-time
*) Compile-time
=========
int a[]={10,20,30,40,50};
*) Run-time
======
int a[]=new int[3];
int i;
[Link]("Enter the Array Elements..");
for(i=0;i<[Link];i++)
{
a[i]=[Link]([Link]());
}
2-D array
=======
--> used to store data in the form of rows and cols
--> used to perform matrix operations like add,sub and so on..
syntax
=====
datatype[] var=new datatype[row,col];
Ex:
==
int[,] a=new int[2,2];
Array Initialization
==============
*) compile-time Initialization
int[,] a= {
{10,20,30},
{24,56,78},
{10,34,67}
};
*) Run-time Initialization
===============
int[,] a=new int[2,2];
int i,j;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
a[i,j]=[Link]([Link]());
}
}
OOPs Concepts
============
--> Representing the relationship among methods and fields of one object to
another object
[Link]
[Link]
[Link] Abstraction
4. Data Encapsulation
5. Data Hiding
6. polymorphism
7. Inheritance
8. message passing
[Link]
=====
--> A class is a Template, which defines the Data members and member functions
common to all Objects of a certain kind.
--> Hence, a class is a collection of Objects..
--> classes define
*) what an Object knows (Data members)
*) what an Object Does(Member functions)
--> a class is a blue print for an Object..
syntax
=====
class classname
{
access datatype mem1,...;
access returntype methodname(parameters)
{
//local variables
//stmts
}
.
.
}
Access_Specifiers
============
--> defines the scope of the data members and member functions
*) private --> inside the class
*) public --> inside + outside
*) protected ---> base + derived
Ex:
==
class Sample
{
priavte int a,b; //data members (attributes or fields)
public void get() // methods
{
---
}
public void show()
{
---
}
}
[Link]
=====
--> it is an Instance of a class
--> class type variable is called "Object"
--> it is a real-time entity
syntax
=====
Ex:
==
Sample s;
s=new Sample();
or
syntax
=====
[Link];
[Link]();
Ex:
==
[Link]();
[Link]();
3. Data Abstraction
============
--> it is the process of representing the essential features without
including the background details or Explanations..
Ex:
==
input(3) ----> factorial --> output(6)
4. Data Encapsulation
===============
-> combining the data members and member functions into a single unit(class)
is called "data Encapsulation"
5. Data Hiding
=========
--> the datas and functions declared under the private access cannot be
accessed by outside members.
--> only those functions which are wrapped in the class, can access the
private members..
6. polymorphism
==========
poly--> many
morph --> forms or shapes
types
====
*) compile-time polymorphism(static binding or Early binding)
Ex:
==
function overloading
Ex:
==
*) abstract classes
[Link]
========
---> deriving a new class from an Existing class
Advantages
========
*) code reusability
*) new datas and methods can be added easily without modifying the
Existing one
types
====
*) single inheritance
*) multi-level inheritance
*) multiple inheritance[ we can implement through "interface" ]
*) hierarchical inheritance
*) hybrid Inheritance
syntax
=====
static datatype var;
static Methods
===========
--> no need to create instance to access the static members of a class
--> they can be called by using classname
Rules
====
--> static methods can call only another static methods
--> they can access only static data members
Ex:
==
class Sample
{
private int x;
private static int y;
public void M1()
{
//x is accessable
//y is accessable
}
public static void M2()
{
//x is not accessable
//y is accessable
}
String Handling
===========
--> Group of characters is called "String"
--> we can use the "String" class object to store the string values
--> once string object is created it cannot be changed
--> like array string index also begins with "0"
--> it is defined in "[Link]" package
String s="welcome";
int len=[Link]();
[Link](len);
2) boolean equals() --> used to check whether two strings are equal or not
--> it considers the case differences
Ex:
==
String s1="welcome";
String s2="Welcome";
boolean b=[Link](s2);
Ex:
==
String s1="welcome";
String s2="Welcome";
boolean b=[Link](s2);
4) String toUpperCase() --> used to convert from lower case to upper case
Ex:
==
String s1="java";
String s2=[Link]();
Ex:
==
String s1="JAVA";
String s2=[Link]();
5) boolean startsWith() --> used to Check whether the Given String starts with
the pecified String or not
Ex:
==
String s="Visual Basic";
boolean b=[Link]("Vis");
6) boolean endsWith() --> used to Check whether the Given String Ends with the
specified String or not
Ex:
==
String s="Visual Basic";
boolean b=[Link]("sic");
Ex:
==
String s="Java";
char c[]=[Link]();
9. String[] split() --> used to divide the String into sub strings based on the
Specifed value
Ex:
==
String s = "3452-2455-56433-34234";
String g[] = [Link]("-");
for (int i = 0; i < [Link]; i++)
{
[Link](g[i]);
}
10. String replace() --> replaces the all occurance of old char by new char
Ex:
==
String s = "Core Java";
String r = [Link]('a', 'A');
[Link]("s = " + s);
[Link] ("r = "+r);
constructor
========
--> it is a special type of method, which is used to initialize the data members
of a class..
points to Constructor
==============
*) it must have the same name of class.
*) it doesnot have any returntype
*) it allows arguments, hence it can be overloaded.
*) it executes automatically when we create an Object
*) it allocates memory space to an Object.
Types of Constructor
==============
*) Default Constructor --> it doesnot contins arguments
*) Parameterized Constructor ---> it contains arguments
*) Copy Constructor --> used to copy the content of One Object to another
Object..
(Object must be passed as argument to copy Constructor)
Ex:
==
class Sample
{
public Sample()
{
//default Constructor
}
public Sample(int i,int j)
{
//Parameterized Constructor
}
public Sample(Sample s)
{
//copy Constructor
}
Ex:
==
class OverDemo
{
public void add(int i,int j)
{
//adding two integers
}
public void add(float i,float j)
{
//adding two floats
}
public void add(int i,int j,int k)
{
//adding three integers
}
}
Exception Handling
=================
Exception : it is a run-time Error, that occurs in a program code during Run-
time..
Types of Error
==========
*) Compile-time Errors
--> all syntax errors will be considered as Compile-time errors
*) Run-time Errors
--> occurs when the user have done the following mistakes during the
execution time
*) giving the Invalid input
*) when dividing the Integer value by zero
*) when the File is not found for read or write Operation
*) Logical Errors
--> System cant find the logical errors
--> when we get the partial o/p instead of Expected output,
the program may contains the Logical Error
*) try
*) catch
*) throw
*) throws
*) finally
try
{
throw Keyword
============
--> it is used to throw an Exception Explicitly
--> it is also used to throw a user-defined Exception
syntax
===
Ex:
==
Example:
=======
//Exception Handling
class ExceptionDemo
{
public static void main(String args[])
{
int a=10,b=0,c;
try
{
c=a/b;
[Link]("c = "+c);
}
catch(Exception ae)
{
[Link]("Attempting to Divide by Zero..");
}
finally
{
[Link]("Finaly block is Executed..");
}
}
}
throw Example
===========
class MyException extends Exception
{
private String str;
public MyException (String s)
{
str=s;
}
public String getMessage()
{
return str;
}
}
class UserException
{
p.s.v.m(String Args[])
{
Scanner sr=new Scanner([Link]);
int age;
[Link]("Enter ur age");
age= [Link]([Link]());
try
{
if(age>100)
throw new MyException("plz enter age b/w 1 to 100.....");
else
{
if(age>=18)
[Link]("major");
else
[Link]("minor");
}
}
catch(Exception me)
{
[Link]([Link]());
}
}
}
Files
====
--> Collection of records or Information, which are stored in Memory of
Computer HardDisk.
Operations on File
=============
--> Read
--> Write
--> Read/Write
--> copy
Stream
=====
--> Flow of Data from one point to another Point
--> File Stream classes defined in "[Link]" package
types
====
--> byte based
*) FileInputStream
*) FileOutputStream
*) FileReader
*) FileWriter
types of Streams
=============
*) Input Stream
*) Output Stream
try
{
FileInputStream fin=new FileInputStream("d:/java/[Link]");
}
catch(FileNotFoundException fe)
{
S.o.p("File is not Existing..");
}
methods
======
1) int read() --> it is used to read one byte data at a time from the File
and returns in the form of Integer (ASCII value)
Ex :
==
int i;
do
{
i=[Link]();
if(i!=-1)
[Link]((char)i);
}while(i!=-1);
Ex : [Link]();
try
{
FileOutputStream fout=new FileOutputStream("[Link]");
}
catch(FileNotFoundException fe)
{
[Link]("File is not Created for Write operation..");
}
PRogram(FileReadDemo)
========
import [Link].*;
class FileReadDemo
{
public static void main(String args[]) throws IOException
{
FileInputStream fin;
int i;
try
{
fin=new FileInputStream("D:\\java notes (1)\\java notes\\Files
[Link]");
do
{
i=[Link]();
if(i!=-1)
[Link]((char)i);
}while(i!=-1);
[Link]();
}
catch(FileNotFoundException fe)
{
[Link]("File is not Existing..");
}
}
}
program
======
}
catch(FileNotFoundException fe)
{
[Link]("File is not Created..");
}
}
}
Inheritance
========
---> deriving a new class from an Existing class
Advantages
========
*) code reusability
*) new datas and methods can be added easily without modifying the
Existing one
types
====
*) single inheritance
*) multi-level inheritance
*) multiple inheritance[ we can implement through "interface" ]
*) hierarchical inheritance
*) hybrid Inheritance
single inheritance
=============
if a new class is created from only one base class
Synatx
---------
Example
=======
class A
{
class B Extends A
{
Multi-Level Inheritance
==================
if a new class is created from another derived class,that is already
from base class
Example
=======
class A
{
class B Extends A
{
}
class C Extends B
{
Hierarchical Inheritance
==================
If a new serval classes are created from only one base class
Example
=======
class A
{
}
class B Extends A
{
}
class C Extends A
{
}
class D Extends A
{
Hybrid Inheritance
===============
It combination of any two types of Inhertiance
Syntax
=====
interface interfaceName
{
//methods DEclartions
}
Example
======
interface one
{
void display();
}
Syntax
=====
interface Der_interface extends Base_interface
{
Example
======
interface two extends one
{
Syntax
=====
Example
=======
{
public void show()
}
public void dispaly()
}
}
// void main
B b1=new B();
A a1=new A();
Mutiple Inheritance
===============
If a new class is created from two or more base classes,then it is
called "Mutiple Inheritance"
Example
======
class A
{
Interface B
{
interface c
{
}
class D extends A implements B,c
{
Threading
========
--> a thread is a separate unit of dispatchable code in a program
Multi-Tasking
=============
--> two or more tasks can perform at the same time
Eg: os
Muti-Threading
============
--> two or more parts of the program can run different or similar
Syntax
=====
}
}
--> we can create a new thread class by extending the "Thread" class..
syntax
======
class <thread_class_name> extends Thread
{
Ex:
==
class ChildThread extends Thread
{
public void run()
{
packages
======= import [Link]
class ThreadDemo
{
p.s.v.m(string args[])
{
for(int i=1;i<=5;i++)
{
[Link]("i="+i);
[Link](3000);
}
}
}
Final Keyword
============
Example
=======
class A{.............................}
Final class B extends A {........................}
class C extends B {...................................}
class A
{
public final void show()
{
}
}
class B extends A
{
public void show()
{
}
}
"this" keyword
===========
--> it is an implicit Object
--> used to reffered as current class object
class Sample
{
private int x,y;
public void set(int x,int y)
{
this.x=x;
this.y=y;
}
}
"Super" Keyword
=============
It used to access the hidden feature of base class in derived class
it must be first statment in the derived class "constructor"
Example
========
class Sample
{
public Sample(int x)
{
}
public void show()
{
}
}
Applets
======
--> an applet is window based Java Program(GUI)
--> it is executed on appletviewer or InternetExplorer (Web browser)
--> The Packages required for creating the Applets are
*) [Link]
*) [Link]
*) [Link]
--> it is Embed in Web page using the <applet>....</applet> tag
syntax
=====
<applet code="classname" width="value" height="value">
</applet>
*) init()
public void init() { ------ }
*) start()
public void start() { ----- }
*) paint()
public void paint(Graphics g) { ----- }
*) stop()
public void stop() { ------ }
*) destroy()
public void destroy() { ---- }
/*
<applet code="MyApplet" width=400 height=400>
</applet>
*/