You are on page 1of 34

Java

The Fast Way

The Ultimate, Quick and Easy


Java Programming Guide

Eng. Alexander Mosgov


Chapter 1:
Data Types
Java provides a rich set of data types. Every variable declared or defined has a particular data type.
The assigned data type specifies the size and the type of values that a variable can store. Java
provides a number of data types which have their particular significant in deciding the appropriate
type by the programmer while programming.
There are mainly two basic types of data types:
Primitive
Non- primitive

Primitive data types


Primitive data types are also known as built-In data types. These data types are predefined by the
language. Java provides the following primitive data types in Java :
Byte
8 bit signed integer
Rang e is -128 to 127
Used in arrays where
memory significantly matters.
Int
Integer Data type are 32 bit signed integer
Minimum value is - 2,147,483,648.(-2^31) and Maximum value is 2,147,483,647
Default value is zero.

Short
16 bit signed integer
Range -32,768 to 32,767
Used in larger arrays to save memory space

Long
64 bit signed integer
Minimum value is -9,223,372,036,854,775,808 and Maximum value is
9,223,372,036,854,775,807
This data type is used if there is a need of higher range than Int.
Default value for Long data type is 0L

Float
Single precision 32bit IEEE 754 floating point.
Float is used to deal with larger array of floating point numbers to save the
memory.
Default value for Float data type is 0.0f.

Double
Double precision 64 bit IEEE 754 floating point.
Default data type / choice for decimal values
Default value for Double data type is 0.0d.

Boolean
Boolean data types represent 1 bit information
There are only two possible values for Boolean data types i.e true and false
Boolean data types are mainly used in condition checking.
Default value for Boolean data type is false.

Char
Single 16 bit Unicode Character
Range is '\u0000' - '\uffff'
Used to store a character

Range
Data Type

Size Default Value

int 32-bit 0 -2,147,483,648 to 2,147,483,647

short 16-bit 0 -32,768 to 32,767

long 64-bit 0L -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

byte 8-bit 0 -128 to 127

float 32-bit 0.0f 1.40129846432481707e-45 to 3.40282346638528860e+38

double 64-bit 0.0d 4.94065645841246544e-324d to


1.79769313486231570e+308d

char 16-bit '\u0000' 0 to 65,535

boolean 1-bit False True or False only

Non-Primitive data types


Non-primitive data types are not predefined data type in any programming language. They are created
by the programmer for their use and efficiency of a program. These variables are also known as
reference variables.
Non-primitive data types in Java include Arrays, Interfaces and class.
Chapter 2:
Operators and Expressions
Java is a very rich language in terms of operators it has. Java operators can be classified into the
following categories:
Arithmetic Operator
Relational operator
Logical operator
Assignment operator
Increment-decrement operator
Conditional operator
Bitwise operator
Other special operators

Arithmetic operators
These operators are used in constructing mathematical expressions. Java provides all the common
arithmetic operators used algebra including +,-,/,*,% . All these operators have the same
functionalities as in other languages.
Operator Meaning
+ Unary plus or addition operator
- Unary minus or subtraction
operator
/ Division operator
* Multiplication
% Modulo Division (Remainder
Operator)

Some example of arithmetic operators are as shown below:


P+q P-q p*q p/q p%q -p/q
In the above given examples, p and q are known as operands and these p and q can be variable or
constants.
Integer arithmetic
When all the operands in the expression are integers then the expression is known as integer
expression and the operations in this expression are known as integer arithmetic. The output of an
integer arithmetic is always an integer value. For the above given example in Arithmetic operators ,
if we take p=14 and q=4,
p + q=18
p-q =10
p*q= 56
p/q = 3
p % q=2

All the results are very easy to understand except / and %.


As we know an integer expression results in an integer, therefore results of p/q is 3 where decimal
part is truncated. Also modulo operator results in remainder thus provides 2 as result.
In case of Modulo division operator , the sign assigned to result is the sign of the first operand i.e
dividend.
18%4 =2
18%-4=2
-18%4=-2
-18%-4= -2
Real Arithmetic
A real arithmetic involves real operands. In real arithmetic, operands are either in exponential
notation or decimal notation. As we know, in floating point, values are rounded-off to the permitted
numbers of digits.
Mixed Mode arithmetic
An expression having operands of both types as integers and real is known as mixed mode arithmetic
expression. If one operand is real and other is integer then the integer operator is itself converted to
real and after conversion, real arithmetic is performed.
e.g. 25/10.0= 2.5 whereas 25/10 produces 2.

Relational Operator
When there is a need to compare two quantities, relational operators are used. An expression like
p>q or p>=30 having a relational is known as relational expression. The output of a relational
expression is either TRUE or FALSE. There are 6 relational operators in Java which are given in the
following table.
Operator Meaning
== Is equal to
!= Not equal to
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to

Example to demonstrate the relational operators:


public class Test {
public static void main(String args[]) {
int p = 10;
int q = 20;
System. out. println ("p == q = " + (p == q) );
System.out.println ("p != q = " + (p != q) );
System.out.println ("p > q = " + (p > q) );
System.out.println ("p < q = " + (p < q) );
System.out.println ("q >= p = " + (q >= p) );
System.out.println ("q <= p = " + (q <= p) );
}
}
This would produce the following result:
p == q = false
p != q = true
p > q = false
p < q = true
q >= p = true
q <= p = false

These relational operators are used in Decision making and Branching to decide the execution
control in a program.
Logical Operators
There are three logical operators in Java. These operators are logical AND, logical OR and logical
NOT. The logical operators logical AND ,logical OR are used when there is a need to form a
compound condition with a combination of two or more relational expressions.

Operator Meaning and Description


This operator is known as Logical AND
operator. When both operand are true or
&& non-zero, then the condition is true
otherwise false
This operator is known as Logical OR
operator. When at least one operand is
|| true or non-zero, then the condition is true
otherwise false
This operator is known as Logical NOT
operator and Used to reverse the logical
! state of any operand. If a condition is false
then this operator will make it true and if
true then it will make false.

Assignment Operator
Assignment operators come in existence when there is a need of assigning value of a specified
expression to a given variable.
Instead of assignment operator, java also provides shorthand assignment operators with the following
syntax:
Variable operator = expression;
The above expression is equivalent to variable=variable operator (expression)

Shorthand Assignment Operator


Statement with simple assignment Corresponding statements with
operator shorthand operator
P=p+1 P+=1
P=p-1 p-=1
P=p*(n+1) P*=n+1
P=p/(n+1) p/=n+1
P=p%q P%=q

public class Demo1


{

public static void main(String args[])


{
int value = 10;
System. out .println( "value is " +value);

// Shorthand assignment operators


int a1 = 4;
int a2 = 8;
a2 += a1;
System. out .println( "a1 is " +a1);
System. out .println( "a2 is " +a2);
a1 = 4;
a2 = 8;
a2 -= a1;
System. out .println( "a1 is " +a1);
System. out .println( "a2 is " +a2);

a1 = 4;
a2 = 8;
a2 *= a1;
System. out .println( "a1 is " +a1);
System. out .println( "a2 is " +a2);

System. out .println( "********** /= Assignment Operator*********" );


a1 = 4;
a2 = 8;
a2 /= a1;
System. out .println( "a1 is " +a1);
System. out .println( "a2 is " +a2);
a1 = 4;
a2 = 8;
a2 %= a1;
System. out .println( "a1 is " +a1);
System. out .println( "a2 is " +a2);
}
}

value is 10
a1 is 4
a2 is 12
a1 is 4
a2 is 4
a1 is 4
a2 is 32
a1 is 4
a2 is 2
a1 is 4
a2 is 0

Increment and Decrement Operator


Java provides two very important operators i.e increment and decrement operators having syntax ++
and – respectively.
These operators are specific types of assignment operators with following features
Unary operators working with a single operand.
Increment operator increases the value of the operator to which it is
applied by 1
Decrement operator decreases the value of the operator to which it is
applied by 1

There are two versions of these operators:


Preincrement and predecrement operator
Postincrement and postdecrement operator

Pre-increment operator
Preincrement operator “++” is applied to a Variable by writing it
before the variable’s name.
Preincrement operator firstly increases the value of variable by 1 and
then that incremented value is used in that expression.
We can’t apply this operator with final variables or constants.

Pre- decrement Operator


Predecrement operator “--” is applied to a Variable by writing it
before the vatiable’s name.
Predecrement operator firstly decreases the value of variable by 1 and
then that decremented value is used in that expression.
We can’t apply this operator with final variables or constants.

Post-increment operator
Postincrement operator “++” is applied to a Variable by writing it after
the variable’s name.
Postincremnt operator firstly use the same non-incremented value in
that expression and after that increases that variable by 1.
We can’t apply this operator with final variables or constants.

Post- decrement Operator


Postdecrement operator “--” is applied to a Variable by writing it after
the vatiable’s name.
Postdecrement operator firstly use the same non-decremented value in
that expression and after that decreases that variable by 1.
We can’t apply this operator with final variables or constants.

Example:

class Increment {
public static void main (String args[]) {
int p = 1;
int q = 2;
int r;
int s;
r = ++q;
s =p++;
r++;
System.out.println(“p = “ + p);
System.out.println(“q= “ + q);
System.out.println(“r = “ + r);
System.out.println(“s = “ +s);
}
}

The output of this program follows:

p=2
q=3
r= 4
s=1

If you are well understood with the above program,, then you please try decrement
operator yourself. It works in the same way as increment with a single difference that
it decreases value by 1 instead of increasing.

Conditional Operator
Ternary operator is a very important conditional operator provided by Java. It has the conditional
statements of following form:
Expression 1 ? expression 2 : expression 3
In the above conditional statement, firstly expression 1 is calculated and the result of the conditional
expression depends on its result whether it is true or false.
If expression 1 is true then expression 2 is evaluated and it becomes the resultant or final value of the
above given conditional statement.
If expression 1 is false then expression 3 is evaluated and it becomes the resultant or final value of
the above given conditional statement. Always remember, only one of the both expression 2 and
expression 3 is calculated depending of expression1’s result.

p = 10;
q = 15;
r= ( p > q ) ? p : q ;
In the above given example, r is assigned the same value as q.
This operator behaves same as if-else. Above program in if-else

if(p>q)
r=p;
else
r=q;

Bitwise operators
Java provides a rich set of bitwise operators which operate on individual bits of an integer. In case,
operand is shorter than int then before applying bitwise operations that is converted into int. These
operators can’t be applied to double or float.

Firstly an integer is represented in binary form and after that bitwise operators are applied on the
individual bits of the binary forms of the corresponding operands. For example 6 is represented in
binary as 110 . 2’s complement is used for storing negative numbers.

public class Bitwise {

public static void main(String args[]) {


int p = 60; /* Binary representation 60 = 0011 1100 */
int q = 13; /* Binary representation 13 = 0000 1101 */
int r = 0;

r = p & q; /* Binary representation 12 = 0000 1100 */


System.out.println("p & q = " + r );

r = p | q; /* Binary representation 61 = 0011 1101 */


System.out.println("p | q = " + r );

r = p ^ q; /* Binary representation 49 = 0011 0001 */


System.out.println("p ^ q = " + r );

r = ~p; /* Binary representation -61 = 1100 0011 */


System.out.println("~p = " + r );

r = p << 2; /* Binary representation 240 = 1111 0000 */


System.out.println("p << 2 = " + r );

r = p >> 2; /* Binary representation 15 = 1111 */


System.out.println("p >> 2 = " + r );

r = p >>> 2; /* Binary representation 15 = 0000 1111 */


System.out.println("p >>> 2 = " + r );
}
}
This would produce the following result:
p & q = 12
p | q = 61
p ^ q = 49
~p = -61
p << 2 = 240
p >> 15
p >>> 15

Operators Precedence and Associativity


.
Every operator has a precedence and associativity associated with it. Precedence of operators is used
in determining the sequence of operators for evaluating an expression in case expression has a
number of operators. In an expression, the operators having higher precedence are evaluated first.
Associativity is the concept for determining the evaluation order of operators when there are multiple
operators with same precedence then associative determines either to be evaluated from left to right
or right to left.

Precedence with
Operator Type Associativity
Rank
() Parentheses
1 [] Array subscript Left to Right
· Member selection
++ Unary post-increment
2 Right to left
-- Unary post-decrement
Unary pre-increment
++
Unary pre-decrement
--
Unary plus
3 + Unary minus Right to left
- Unary logical negation
! Unary bitwise
~ complement
( type ) Unary type cast
* Multiplication
4 / Division Left to right
% Modulus
+ Addition
5 Left to right
- Subtraction
Bitwise left shift
<< Bitwise right shift with
6 >> sign extension Left to right
>>> Bitwise right shift with
zero extension
Relational less than
Relational less than or
<
equal
<=
Relational greater than
7 > Left to right
Relational greater than
>=
or equal
instanceof
Type comparison
(objects only)
Relational is equal to
==
8 Relational is not equal Left to right
!=
to
9 & Bitwise AND Left to right
10 ^ Bitwise exclusive OR Left to right
11 | Bitwise inclusive OR Left to right
12 && Logical AND Left to right
13 || Logical OR Left to right
14 ?: Ternary conditional Right to left
Assignment
=
Addition assignment
+=
Subtraction assignment
-=
15 Multiplication Right to left
*=
assignment
/=
Division assignment
%=
Modulus assignment
Chapter 3:
Decision Making Statements in Java
Sometimes there is a need to take decisions on the basis of given condition. Based on the given
conditions, it is the decided that which condition is satisfied and statements corresponding to
that conditions is executed.
There are mainly three types of decision making statements in Java
1. If statement
2. switch statement
3. conditional operator statement

If statement is further implemented as


Simple if statement
If-else statement
If-else Ladder
Nested if –else statement
if statement
if statement is helpful in making a decision in our program.
1. Simple if statement
If the condition given in if is true then only the statements corresponding to that if block is executed.
Syntax of if statement
if(condition)
{
If condition is true , these Statements written inside curly brackets will be executed .
}

Program

public class ifstatement


{
public static void main(String args[])
{

int test1=10;

if (test1>10)
system.out.println(“you are wrong”);
if(test1>5)
{
System.out.println("good");
}
}
}
Result of this program is “good”.
2. if else statement
In case the if condition is true then the statements inside curly brackets of if block is executed
otherwise statements of the else block are executed.

If(condition)
{
if condition is true, these statements will be executed.
}
else
{
in case the condition given in if statement is false , these statements will be executed.
}
these statements are executed always.

Program

public class if-elsestatement


{
public static void main(String args[])
{
int test1=10;

if (test1>10)
{
system.out.println(“you are wrong”);}
else
{
System.out.println("good");
}

}
}
Output
good
3. if–else-if ladder
Syntax with meaning
If(condition1)
{
Statements // executed only if condition1 is true.}
else if (condition2)
{
Statements // executed only if condition1 is true
}
.
.
.
else if (condition-n)
{
Statements // execute only when condition-n is true}
else
{
Statements // executes when all above given conditions are false.
}
Statements // these statements are always executed.
4. Nested if…else statement
When there are a series of decision, then we have to use a number of if-else statements in nested
form.
if( condition1){
//Executes when the condition 1 is true
if( condition2){
//Executes when the condition 2 is true
}
}
public class Test1 {

public static void main(String args[]){


int a = 30;
int b = 10;

if( a >10 ){
if(a== 30)
system.out.println(” a is equal to 30”);
else
system.out.println(“ a is not equal to 30”);
}
else System.out.print("a is less than 10");
}
}
}
}
Output : 30
The switch Statement
As we studied, if statements are useful in deciding the sequence control of program. But in case when
there a number of alternatives to be tested against a variable, then the complexity of the program with
a number of IF statements increase. Switch statements solves this problem by allowing a variable to
be tested against a list of values. These values are called cases. When the variable is matched with a
particular case then the statements of that particular case is executed.
Syntax:
Switch (expression ){
case value1 :
block1
break; //optional
case value2 :
block2
break; //optional
…………………………………………..
……………………………………………….
………………………………………………………
default : //Optional
//default block
}
Rules for switch statement
The switch statement can have a variable of type short,int,byte or char.
A Switch block can have any number of case statements with the following syntax
Case value :
any The value specified in case should be of same data type as variable in the switch
statement. Also this value must be any constant or literal.
When the variable of switch statement is matched with nay case value then the
statements following that case will be executed until Break statement is reached.
As the execurion reaches to a break statement, the switch terminates, and the execution
is transferred to the next line following the switch block.
Putting Break in each case is not necessary. In case no Break appears, the execution falls
through the subsequent given cases until a Break is executed or the switch terminates.
There may be an optional default case in a switch at the end of switch. Default case is
executed when value of a variable is unmatched with all given cases.
Example:
public class Test1 {

public static void main(String args[]){


char letter = 'S';

switch(letter)
{
case 'P' :
System.out.println("Duffer");
break;
case 'Q' :
case 'S' :
System.out.println("Excellent");
break;
case 'R' :
System.out.println("You failed");
Case 'F' :
System.out.println("try again");
break;
default :
System.out.println("Invalid input");
}
}
}
Output:
Excellent

Looping statements in java

Sometimes there is a need to execute a block of statements several times depending on any condition.
This type of situation is handled by Loops in java.
Java allows these loops mechanisms with three types of loops:
for Loop
while Loop
do...while Loop

The for Loop


A for loop is a control structure which executes the statements within this IF block several times
depending on conditions.
A for loop is beneficial when we already know for how many times to repeat a particular task.
Syntax:
for (initialization ; Boolean_expression ; update)
{
//body for the loop
}
Sequence of steps followed in a for loop:
First of all initialization statement is executed. This statement allows declaring or
initializing loop control variables.
Now the Boolean expression or test condition is evaluated. If this Boolean expression
comes to true, the defined body of the for loop is executed. Otherwise in case of false, the
body does not execute and control is transferred to the statement following the for loop.
After once execution of body of loop, the control is transferred to the update statement
which generally includes increment or decrement. This statement is generally used to update
any loop variable.
Again there is a evaluation of Boolean expression. If it is evaluated true, the loop again
executes and this process (loop body , then update step, then Boolean expression) is repeated
until Boolean expression is false. As Boolean expression evaluates false, loop is terminated.
Example:
public class demo {

public static void main(String args[]) {

for(int a = 10; a < 20; a = a+1) {


System.out.print("value of a : " + a );
}
}
}
This program results in printing value of a from 10 to 19. Because as a is incremented to 20 , the
condition a<20 is false and loop is terminated.
The while Loop
A while loop also performs the same task as for loop with a small difference which you will
understand with the following syntax and example.
Syntax:
While ( Boolean_expression)
{
//body of the While Loop
}
If the Boolean expression is true then the body defined for the While loop is executed and this repeat
execution for same body continues until the Boolean expression is evaluated to false.
As the loop terminates or the Boolean-expression is false , the control is transferred to the first
statement following the while loop.
Example:
For the last given example of for loop, corresponding While Loop is given below.
public class Demo1 {
public static void main(String args[]) {
int a= 10;

while( a < 20 ) {
System.out.print("value of a : " + a);
a++;
}
}
}
The do...while Loop
This loop is similar to other loops with a major difference that this loop is always executed at least
once while the other for and While loops are executed only when condition or Boolean expression is
true. For and while loop may be executed for Zero times if the condition is false.
Syntax:
do
{
//body for the loop
}while ( Boolean_expression);
Since this loop has the testing condition at the end of loop , the loop is executed at least once always.
The execution of this loop is otherwise very similar to While loop. Firstly loop is executed and then
boolen_expression is evaluated. If expression is true then loop is executed again and again until the
Boolean expression is false.
Example:
Similar program for printing value of a from 10 to 19 using do….. while loop is given below.
public class demo2 {
public static void main(String args[]){
int a = 10;

do{
System.out.print("value of a : " + a );
a++;
}while( a< 20 );
}
}
Enhanced for loop
Enhanced for loop is mainly used in case of arrays.
Syntax:
for (declaration : expression)
{
// Body for enhanced for loop
}
Expression in the above given syntax generally represents the array which is nneeded to lop through.
This expression is either an array variable or a method call returning array.
Declaration is a block variable with the same type as the array we are accessing. This variable has
its scope within this for block with a value equal to current array element.
Example:
public class Demo {
public static void main(String args[]){
int [] values = {10, 20, 30, 40, 50};

for(int a : values ){
System.out.print( a );
System.out.print(" ");
}
System.out.print("\n");
String [] students ={"James", "Anil", "Tom", "Aman"};
for( String name : students ) {
System.out.print( name );
System.out.print(" ");
}
}
}
Result:
10 20 30 40 50
James Anil Tom Aman
break Keyword
Java provides break keyword which is used to terminate the entire loop. This keyword can be used
inside a loop or switch statement to terminate that lop or switch.
Keep in mind that in case of nested loops, break keyword only terminates the execution of innermost
loop transferring the execution to the first statement following that loop.
Example:
public class Demo {
public static void main(String args[]) {
int [] values = {10, 20, 30, 40, 50};

for(int a : values) {
if( a == 40 ) {
break;
}
System.out.print( a );
System.out.print("\n");
}
}
}
Output:
10
20
30
As the value of a is equal to 40 , the break statement executes and terminates the loop.
Continue Keyword
Continue keyword can be used in any loop structure for deciding the execution control.
When continue is used in for loop then the control is immediately transferred to the
update statement without executing the statements after continue.
In case of while or do-while, control is immediately transferred to the Boolean
expression without executing the statements after continue.
Example:
public class Continue {
public static void main(String args[]) {
int [] values = {10, 20, 30, 40, 50};

for ( int a : values ) {


if( a == 40) {
continue;
}
System.out.print( a );
System.out.print("\n");
}
}
}
This would produce the following result:
10
20
30
50
When the value of a is equal to 40, the continue statement executes which transfers the control at the
beginning of loop without printing the value of a equal to 40.
Chapter 4;
Classes, objects and methods
As Java is a pure object – oriented language, all java programs have at least a class. Everything we
want to represent through a Java program must be encapsulated in a class. Classes define the state and
behaviors for the basic component of a class known as objects. Here the object – oriented refers that
classes create objects and the objects uses the defined methods for their mutual communication.
Since Java is an object-oriented language and therefore supports the following feature:
Objects
Classes
Encapsulation
Abstraction
Polymorphism
Instance
Methods

Class: A class is a user-defined data type which acts as a blueprint to represent the states and
behaviors for the corresponding objects.
Object: Objects are instances of a class and have their own states and behaviors. For example an
object dog for an animal class has state as name, color, breed and behaviors as eating, barking and
wagging.
There are a number of objects in our surroundings which include dog, human, table, bird, mobile , etc.
All these objects have their specific states and behaviors.
In case of a person as object, corresponding states are name, color, height, age and behaviors are
eating, walking, running, etc.
Defining a class
Class is a user defined data type serving as a blueprint. A simple class is defined with the following
form:
Class classname
{
Data members or fields declaration; // optional
Methods or member functions declaration; // optional
}
As data members and member functions are optional, so Java allows even an empty class.
Fields and Methods declaration
As we know, data is encapsulated in Java by placing its data members and member functions inside a
class. These variables are known as instance variables as they are created as an object is instantiated.
The method of declaring instance variables is same as local variables.
Class Person
{
Int age;
String name;
String color;
}
This class has three instance variables one age of integer type and other two name and color of string
type.
Declaring Methods
A method declaration generally has the following things:
Name for the method
Return type of that method
List of arguments or parameters
Body for that method which actually defines the operations to be performed on data.
The general form for method declaration is
returnType MethodName ( argument-list)
{
Body for the method;
}
A class can have following types of variables:
Instance variables: The variables defined inside a class but outside any method are
known as instance variables. As a class is loaded, these variables are instantiated. These
variables can be accessed from any method or block including constructor of that particular
class.
Local variables: The variables defined inside any method, constructor or blocks are
known as local variables. The variable are declared and initialized within a particular
method and destroyed with the completion of that method.
Class variables: These variables are defined inside a class but outside any method with
a single difference that they are declared using Static keyword.
In the above given example name, color, age are data member and eating, hungry, walking, running,
sleeping all these are functions.
Example for a class
public class Person {
String Name;
String color;
int age;
void eating(){
}
void hungry(){
}
void Walking (){
}
void Running (){
}
void sleeping(){
}
}

Creating Objects for a class


As we know objects are the instances of the classes. Creating a object is also known as instantiating
an object.
Java provides a new operator for creating object. New operator creates an object for the specified
class and also returns a reference for that created object. As this returns a reference of class type
therefore there is aneed to create a reference type variable so that that can hold that reference.
Example for creating object for class square
Square s1; // declaring object
S1= new square(); // instantiate object
First statement declares a reference type variable to store the object reference of square type
whereas second statements actually assigns the object reference to S1 variable.
These both statements can be combined in a single statement to create an object as
Square s1= new square();
With the above given method, we can create any number of objects for a class.

Constructors
Constructor is the most important topic which comes into the mind as there is a discussion about a
class. Every class has at least one constructor. If a programmer does not explicitly specify any
constructor, there is always a default constructor for that class. As a object is created, a constructor is
invoked.
Important points for a constructor
Constructor has the same name as class.
There is no return type for a constructor even not void.
There can be multiple constructors for a single class.
If constructors are explicitly defined by the programmer then default constructor not
comes in existence.

You might also like