Data Types:
Wrapper classes:parseXXX()
int a;
a=Integer.parseInt(args[0]);
1.Integer(int)
2.Character(char)
3.Float(float)
4.Double(double)
5.Boolean(boolean)
6.String(String)
7.Long / Short
Operator: it specifies the operation between two or more operands.
c= a+b;
Arithemetic Opearator(+,-,*,/,%):
Assignement Opearator(=,+=,-=,*=,/=):
int a;
a=10;
a*=2;
a=a+5;
a+=5;
Increment/Decrement Opearator(++,--):
Condition Opearator(<,>,<=,>=,!=):
Logical Opearator(&&, ||):
Ternary Opearator(?:)
(condition)?expresssion1:expresssion2;
Statements:
1.Conditonal Statements:
1.if statement:
syntax:
if(condition)
//block of code
2.if else statement:
if(condition)
//block of code
else
//block of code
3.Nested/ladder if statement(if..elsif statement)
Syntax:
if(codition)
{
//block of code
else if(condition)
//block of code
else if(condition)
//block of code
else
//block of code
2.Looping Statements:
1.for loop:
syntax:
for(intialization; condition; increment/decrement)
//block of code
2.while loop:
syntax:
initialization;
while(condition)
//block of code
increment/decrement;
3.do while loop:
syntax:
intialization;
do
//block of code
increment/decrement;
while(condition);
4.foreach loop:
foreach loop is used with the arrays and collection framework.
syntax:
forach(Object: variable)
//block of code
3.Branching/Jumping Statements:
1.break
if we want to break the certain condition, we will use break.
2.continue
if we want to skip the certain condition, we wil use continue
3.switch case:
if we want to select a specific condition from the available conditions.
syntax:
swtich(choice)
case 1:
//block of code
break;
case 2:
//block of code
break;
case N;
//block of code
break;
}
default:
//block of code
write a java program to print the factors of a number.
Arrays:
Array is collection of similar types of data elements. It is zero based indexed, it means the index position
of the array will always be start from Zero(0) and the highest index position of the array is (Size-1)th
index.
Syntax:
data type arrayName[]= new data type[size];
ex.
int a[]= new int[5];
arrayName[index]=value;
ex.
a[3]=10;
Multi Dimensional Array:
data type arrayName[][]= new data type[row][col];
Accepting values from the user:
1.Command Line argument:
int a= Integer.parseInt(args[0]);
2.Scanner Class:
java.util:-
utility classes, interfaces and abstract classes are included in the java.util package.
import java.util.*;
Scanner sc = new Scanner(System.in);
Scanner methods:
nextXXX();
int a= sc.nextInt();
float x= sc.nextFloat();
double d= sc.nextDouble();
String str= sc.next();
String str1= sc.nextLine();
3.BufferedReader Class:
java.io.*;
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
readLine(); - to get the values from the console(String).
Int a= Integer.parseInt(br.readLine());
float f= Float.parseFloat(br.readLine());
4.DataInputStreamReader
Access Specifiers:
it specifies the scope of the data elements, functions or classes.
1.public- can be accessible anywhere.
2.private:- it can be accessed by the derived classes.
3.protected:- it can be accessed by it self, within the class
Package:
package is the group of similar type classes, interfaces or abstract class, grouped under the
single name.
Syntax:
package packageName;
to access the package , we have to import that package.
import packagename.subclassname;
import packagename.*;
Object Oriented Programming:
1.Class: It is a logical entity , it is a blueprint from which we can create the multiple instances of it.
syntax:
access specifier class classname extends classname
access specifier data type data memeber1;
access specifier data type data memeber2;
access specifier data type data memeberN;
access specifier return type member-function1([list of argument])
// member function body
}
access specifier return type member-function2([list of argument])
// member function body
2.Object:
Object is real-time entity which exists in this world, it has state and behaviour wherein state
represents the attributes and behaviour represents the actions or the operations that can be performed
by the Object of the class.
syntax:
classname objectName= new className([list of arg..]);
3.Constructor:
it is special member function of the class, which has same name as that of the class, it
doesn't have any return type. it gets invoked automatically when we create an instance of the class.
syntax:
class classname
accessspecifier classname()
//constructor body
}
In Java there are two types of constructors:
1.Default Constructor:
constructor without arguments are called as default construcor. Even if we don't declare
the constructor inside the class, compiler automatically adds the default constructor at the object
invokation.
2.Parameterized Constructor:
Constructor with the arguments is known as parameterized constructor. Basically we
use constructors to utilize the object memory allocation.
4.Inheritance:
It is a mechanism which allows the programmer to acquire the properties of the
base/parent/superclass class into the derived/child class. we use inheritance for the code-reusability.
extends keyword is used to achieve the inheritance in java.
class className extends classname2
// data members;
//member functions(){}
there are 3 types of inheritance in java.
1.Single Inheritance:
2.Multi level Inheritance:
3.Hierarchical Inheritance:
this Keyword: this keyword is used to access the current instances of the class. whenever you want to
refer to the current instance we have to use this keyword.
Types of variables:
1.instance variable:
the variables which are declared inside the class but outside of any method is known
instance variables.
local variable:
the variables which are declared inside the any method, is known as local to that
method.
static variable:
the variable declared with the static keyword is known as static variable, the static
variables are common for all the instances of the class. it is shared property of the class among all the
instances. the static variable belongs to class.
Super Keyword:
Super keyword can be used to access the base class data members and member functions.
Final Keyword:
we can use final keyword with, class, method and variable.
we can not extend class if it is defined as final class.
we can not modify the values of final variable
we can not override the final method.
5.Polymorphism(Poly- Many , morphism- Forms)
Performing the same task in different ways is known as polymorphism. In java polymorphism
can be achieved using function overloading and function overriding.
1. Compiletime Polymorphism:
can be achieved using function overloading
2.Runtime Polymorphism:
can be achieved using Function Overriding.
Function overloading:
class consisting the functions with the same name but with different list of parameters is
known as Function Overloading.
Function Overriding:
if base class method is redefined inside the derived class, without making any changes to the
signiture of the method is known as functio Overriding.
6.Encapsulation: Encapsulation is binding into the single unit. This can be achieved using a class in Java,
wherein we do bind the data members and member functions and access that using the instance of the
class.
7.Data Abstraction:
it is a mechanism which hides the code shows the functionality. in java abstraction can be
achieved using 'abstract' keyword. We can achieved it using
1.abstract class:
a method without a defination is known as abstract method and the class which
contains an abstract method is known as abstract class.
The abstract class can have , concreate methods, constructors, and abstract methods.
the class which extends the abstract class it must provide the definations of the all the abstract methods
otherwise the derived class has to be declared as an abstract class ans so on.
- we can not directly instantiate the abstract class.
syntax:
class className extends classname
// data memebrs
//member functions()
abstract member function();
2.interface:
interface is 100% abstract, it does provide abstract methods only.
we use interface keyword to declare it. it does not contain concrete methods, constructors. we have to
use 'implements' keyword to inherit the member functions of the interface.
"a single class can extends the single class only."
"a single class can implements multiple interfaces."
"a single interface can extends single interface only."
syntax:
interface interfaceName
abstract member function1();
abstract member function2();
abstract member functionN();
8.Exception Handling