0% found this document useful (0 votes)
14 views74 pages

Introduction to Java Programming Basics

Uploaded by

rk2493853
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views74 pages

Introduction to Java Programming Basics

Uploaded by

rk2493853
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Slides part- I

By Aman Tiwari
What is Java?

● Java is a widely-used High level programming language for coding. It has been a popular choice among developers for
over two decades, with millions of Java applications in use today. Java is a multi-platform(hardware+OS), object-
oriented, and network-centric language that can be used as a platformin itself. It is a fast, Highly secure, reliable
programming language for coding everything frommobile apps and enterprise software to big data applications and
server-side technologies.
● How to programin Java
● To begin programming in Java, you need to install a Java Edition on your system. There are three main Java editions:
1. Java Standard Edition (Java SE)
2. Java Enterprise Edition (Java EE)
3. Java Micro Edition (Java ME)
● What is Java SE?
● Java Standard Edition is the core Java programming platform. It contains all of the libraries and APIs that any
programmer needs for java development. Open Java Development Kit(OpenJDK) is the free and open source
implementation of Java SE.
Application

● According to Sun Micro System, 3 billion devices run Java. There are many devices where Java is currently used. Some of
themare as follows:
1. Desktop Applications such as acrobat reader, media player, antivirus, etc.
2. Web Applications such as [Link], [Link], etc.
3. Enterprise Applications such as banking applications.
4. Mobile Apps
5. Embedded System
6. Smart Card
7. Robotics
8. Games
9. Testing
[Link] data etc
Top Applications Build using Java
Features of java
If a programwritten on a particular platformcan run on other platforms without any recompilation, it is known as
[Link] Java is platform-independent, any programwritten using Java on Windows will
execute without any recompilation on any other [Link] see what platformindependence actually is and how
it matters, let us compare Java to a platform-dependent language like C.
Java is platform-independent whereas JVMis platformdependent. Let us see how Java code executes in
different platforms.

JRE
Object-oriented
Java is an object-oriented programming language. Everything in Java is an object.
Object-oriented means we organize our software as a combination of different
types of objects that incorporates both data and behavior.
Object-oriented programming (OOPs) is a software developement methodology that
simplifies software development and maintenance by providing some rules.

Basic concepts of OOPs are:


[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
A Java programrequires a Java development kit (JDK) for library support and development tools.
Java compiler ([Link]): A Java compiler is a programthat converts source files into the respective byte code.
The byte code is platform-independent.
Java launcher ([Link]): Java launcher launches the Java application.
Java standard packages and runtime libraries contain the necessary code for executing Java applications.
JDK

JRE

Note: Previously, JDK came with a separate implementation for JRE (Java Runtime Environment). Now, starting
fromJava 11, Adopt OpenJDK does not come with separate JRE implementation; JDK will include JRE
implementation. We will be using Adopt OpenJDK in this course.
JAVA Architecture
Source code: Programwritten in Java language
Byte code: A .class file is generated after the Java code is compiled
ClassLoader: Loads all the class files needed for execution
Byte code verifier: Checks code for fragments for illegal code
Interpreter: Converts byte code instruction to machine code
Compiler: Compiles reusable byte code instructions to machine code
Runtime: The overall execution of the programis assisted by Runtime.
JIT: just in time compilation
First Java Program| Hello World Example
The requirement for Java Hello World Example
1. For executing any java program, you need to Install the JDK if you don't have installed it,
2. download the JDK and install it.
3. Set path of the jdk/ bin directory (only in below JDK 11).
4. Create the java program

Let's create the hello world programin java (make a file [Link]):

class Simple
{
public static void main(String args[ ]){
[Link]("Hello Everyone");
}
}
save this file as [Link]
TO COMPILE javac [Link]
TO EXECUTE/ RUN java Simple
Parameters used in First Java Program
Let's see what is the meaning of class, public, static, void, main, String[ ],
[Link]().
class keyword is used to declare a class in java.
public keyword is an access modifier which represents visibility. It means it is visible to
all.
static is a keyword. If we declare any method as static, it is known as the static
method. The core advantage of the static method is that there is no need to create
an object to invoke the static method. The main method is executed by the JVM, so it
doesn't require to create an object to invoke the main method. So it saves memory.
void is the return type of the method. It means it doesn't return any value.
main represents the starting point of the program.
String[ ] args is used for command line argument. We will learn it later.
● [Link]() is used to print statement. Here, Systemis a class, out is the
object of PrintStreamclass, println() is the method of PrintStreamclass.

● How many ways can we write a Java program


● Valid java main method signature
1. public static void main(String[] args)
1. public static void main(String []args)
1. public static void main(String args[])
1. public static void main(String... args)
1. static public void main(String[] args)
1. public static final void main(String[] args)
1. final public static void main(String[] args)
1. final strictfp public static void main(String[] args)
● Invalid java main method signature
1. public void main(String[] args)
1. static void main(String[] args)
1. public void static main(String[] args)
1. abstract public static void main(String[] args)
Java Keywords and data type

● In programs, data values are stored in memory locations identified by names (identifiers). Such named
memory locations are called variables.

● Notice how variables are declared using types, identifiers, and assigned values:
● As you have seen, a programis composed of several components, blocks, and words.
● Some of these words are reserved and have a special meaning in Java, e.g. class, public, void, for, int, static.
These words are called keywords. There are 50 keywords in Java 8.
Rules for Naming a Identifier ( Variable )

[Link] sensitive
[Link] not start with a number
[Link] start with a letter, $ or _
[Link] not have spaces
[Link] not be a Java keyword or a literal
[Link] restriction on the length
Note: The identifier should not have the only _ in its name.
Example :
Valid Identifiers: CustomerName, grade_in_first_attempt,
marksScoredIn3rdAttempt
Invalid Identifiers: model number, 1ofAKind, double
Types of Variables
There are three types of variables in java:
[Link] variable
[Link] variable
[Link] variable

We will study about themin later chapters


● Java Variable Example: Add Two Numbers
public class Simple
{
public static void main(String[] args)
{
int a=10;
int b=10;
int c=a+b;
[Link](c);
}
}
Data Types in Java
● Data types specify the different sizes and values that can be stored in the variable.
There are two types of data types in Java:
1. Primitive data types: The primitive data types include boolean, char, byte, short, int,
long, float and double.
● There are 8 types of primitive data types:
1. boolean data type
1. byte data type
1. char data type
1. short data type
1. int data type
1. long data type
1. float data type
1. double data type
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces, and
Arrays.
Operators in java :
● Operator in java is a symbol that is used to performoperations. For example: +, -, * , /
etc.
● There are many types of operators in java which are given below:

1. Unary Operator,
1. Arithmetic Operator,
1. Shift Operator,
1. Relational Operator,
1. Bitwise Operator,
1. Logical Operator,
1. Ternary Operator and
1. Assignment Operator.
Oper ator Type Categor y Pr ecedence
Unar y postfix expr++ expr- -
pr efix ++expr - - expr +expr - expr ~!

Ar ithmetic multiplicative * / %
additive + -
Shift shift << >> >>>
Relational compar ison < > <= >= instanceof

equality == !=
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
Logical logical AND &&
logical OR ||
Ter nar y ter nar y ? :
Assignment assignment = += - = * = / = %= &= ^ = |= <<= >>= >>>=
Java Unary Operator
● The Java unary operators require only one operand. Unary operators are used to
performvarious operations i.e.:
● incrementing/ decrementing a value by one
● negating an expression
inverting the value of a Boolean
Java Unary Operator Example: ++ and --
public class OperatorExample{
public static void main(String args[]){
int x=10;
[Link](x++);/ / 10 (11)
[Link](++x);/ / 12
[Link](x--);/ / 12 (11)
[Link](--x);/ / 10
}}
● Java Unary Operator Example 2: ++ and –-

public class OperatorExample{


public static void main(String args[]){
int a=10;
int b=10;
[Link](a++ + ++a);/ / 10+12=22
[Link](b++ + b++);/ / 10+11=21

}}
● Java Unary Operator Example: ~ and !
public class OperatorExample{
public static void main(String args[]){
int a=10;
int b=-10;
boolean c=true;
boolean d=false;
[Link](~a);/ / -11(minus of total positive value which starts from0)
[Link](~b);/ / 9 (positive of total minus, positive starts from0)
[Link](!c);/ / false (opposite of boolean value)
[Link](!d);/ / true
}
}
● Java Arithmetic Operators
● Java arithmetic operators are used to performaddition, subtraction, multiplication, and division.
They act as basic mathematical operations.
● Java Arithmetic Operator Example

public class OperatorExample{


public static void main(String args[]){
int a=10;
int b=5;
[Link](a+b);/ / 15
[Link](a-b);/ / 5
[Link](a* b);/ / 50
[Link](a/ b);/ / 2
[Link](a%b);/ / 0
}}
● Java Arithmetic Operator Example: Expression

class OperatorExample{
public static void main(String args[]){
[Link](10* 10/ 5+3-1* 4/ 2); / / 21
}}

● Java Left Shift Operator


● The Java left shift operator << is used to shift all of the bits in a value to the left side of a
specified number of times.
public class OperatorExample{
public static void main(String args[]){
[Link](10<<2);/ / 10* 2^ 2=10* 4=40
[Link](10<<3);/ / 10* 2^ 3=10* 8=80
[Link](20<<2);/ / 20* 2^ 2=20* 4=80
[Link](15<<4);/ / 15* 2^ 4=15* 16=240
}}
Java Right Shift Operator
● The Java right shift operator >> is used to move left operands value to right by the number of
bits specified by the right operand.
● Java Right Shift Operator Example
public class OperatorExample{
public static void main(String args[]){
[Link](10>>2);/ / 10/ 2^ 2=10/ 4=2
[Link](20>>2);/ / 20/ 2^ 2=20/ 4=5
[Link](20>>3);/ / 20/ 2^ 3=20/ 8=2
}}
● Java AND Operator Example: Logical && and Bitwise &
● The logical && operator doesn't check second condition if first condition is false. It checks
second condition only if first one is true.
● The bitwise & operator always checks both conditions whether first condition is true or false.
public class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
[Link](a<b&&a<c);/ / false && true = false
[Link](a<b&a<c);/ / false & true = false
}}
● Java AND Operator Example: Logical && vs Bitwise &

public class OperatorExample{


public static void main(String args[]){
int a=10;
int b=5;
int c=20;
[Link](a<b&&a++<c);/ / false && true = false
[Link](a);/ / 10 because second condition is not checked
[Link](a<b&a++<c);/ / false && true = false
[Link](a);/ / 11because second condition is checked
}}
Java OR Operator Example: Logical || and Bitwise |
● The logical || operator doesn't check second condition if first condition is true. It checks second condition only if first one is
false.
● The bitwise | operator always checks both conditions whether first condition is true or false.
public class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
[Link](a>b||a<c);/ / true || true =true
[Link](a>b|a<c);/ / true | true =true
/ / || vs |
[Link](a>b||a++<c);/ / true || true =true
[Link](a);/ / 10because second condition is not checked
[Link](a>b|a++<c);/ / true | true =true
[Link](a);/ / 11because second condition is checked
}}
Java Ternary Operator
● Java Ternary operator is used as one liner replacement for if-then-else statement and
used a lot in java programming. it is the only conditional operator which takes three
operands.
● Java Ternary Operator Example
class OperatorExample{
public static void main(String args[]){
int a=2;
int b=5;
int min=(a<b)?a:b;
[Link](min);
}}
Conditional Statements

● If-else Statement
The Java if statement is used to test the condition. It checks boolean condition: true or false. There are
various types of if statement in java.
1. if statement
2. if-else statement
3. if-else-if ladder
4. nested if statement
public class IfExample {
public static void main(String[] args) {
/ / defining an 'age' variable
int age=20;
/ / checking the age
if(age>18){
[Link]("Age is greater than 18");
}
}
}
● if-else Statement
● The Java if-else statement also tests the condition. It executes the if block if condition is true
otherwise else block is executed.

public class IfElseExample {


public static void main(String[] args) {
/ / defining a variable
int number=13;
/ / Check if the number is divisible by 2 or not
if(number%2==0){
[Link]("even number");
}else{
[Link]("odd number");
} }}
public class IfElseIfExample {
public static void main(String[] args) {
int marks=65;

if(marks<50){
[Link]("fail");
}
else if(marks>=50 && marks<60){
[Link]("D grade");
}
else if(marks>=60 && marks<70){
[Link]("C grade");
}
else if(marks>=70 && marks<80){
[Link]("B grade");
}
else if(marks>=80 && marks<90){
[Link]("A grade");
} else{
[Link]("Invalid!");
} }}
● Nested if statement
The nested if statement represents the if block within another if block. Here, the inner if block condition executes only
when outer if block condition is true.
public class JavaNestedIfExample {
public static void main(String[] args) {
/ / Creating two variables for age and weight
int age=20;
int weight=80;
/ / applying condition on age and weight
if(age>=18){
if(weight>50){
[Link](" You are eligible to donate blood" );
}
}
}}
● Switch Statement
● The Java switch statement executes one statement frommultiple conditions. It is like if-else-if ladder statement.
The switch statement works with byte, short, int, long, enumtypes, String and some wrapper types like Byte,
Short, Int, and Long. Since Java 7, you can use strings in the switch statement
● In other words, the switch statement tests the equality of a variable against multiple values.

● Points to Remember
1. There can be one or N number of case values for a switch expression.
1. The case value must be of switch expression type only. The case value must be literal or constant. It doesn't allow
variables.
1. The case values must be unique. In case of duplicate value, it renders compile-time error.
1. The Java switch expression must be of byte, short, int, long (with its Wrapper type), enums and string.
1. Each case statement can have a break statement which is optional. When control reaches to the break statement,
it jumps the control after the switch expression. If a break statement is not found, it executes the next case.
public class SwitchExample {
public static void main(String[] args) {
/ / Declaring a variable for switch expression
int number=20;
/ / Switch expression
switch(number){
/ / Case statements
case 10: [Link](" 10" );
break;
case 20: [Link](" 20" );
break;
case 30: [Link](" 30" );
break;
/ / Default case statement
default:[Link](" Not in 10, 20or 30" );
}
} }
● Java Switch Statement is fall-through
● The Java switch statement is fall-through. It means it executes all statements after the first
match if a break statement is not present.

public class SwitchExample2 {


public static void main(String[] args) {
int number=20;
/ / switch expression with int value
switch(number){
/ / switch cases without break statements
case 10: [Link](" 10" );
case 20: [Link](" 20" );
case 30: [Link](" 30" );
default:[Link](" Not in 10, 20or 30" );
}
} }
Loops in java

● In programming languages, loops are used to execute a set of instructions/ functions repeatedly when some conditions
become true. There are three types of loops in java.
1. for loop
● The Java for loop is a control flow statement that iterates a part of the programs multiple times. If the number of
iteration is fixed, it is recommended to use for loop.

public class ForExample {


public static void main(String[] args) {
/ / Code of Java for loop
for(int i=1;i<=10;i++){
[Link](i);
}} }
● Java Infinitive For Loop
● If you use two semicolons ;; in the for loop, it will be infinitive for loop.
● Syntax:

public class ForExample {


public static void main(String[] args) {
/ / Using no condition in for loop
for(;;){
[Link]("infinitive loop");
}
}
}

press ctrl+c to exit fromthe program


1. while loop
The Java while loop is a control flow statement that executes a part of the programs repeatedly
on the basis of given boolean condition. If the number of iteration is not fixed, it is recommended to
use while loop.

public class WhileExample {


public static void main(String[] args) {
int i=1;
while(i<=10){
[Link](i);
i++;
}
}
}
● Infinitive While Loop
If you pass true in the while loop, it will be infinitive while loop.

public class WhileExample2 {


public static void main(String[] args) {
while(true){
[Link]("infinitive while loop");
}
}
}
● do-while loop
● The Java do while loop is a control flow statement that executes a part of the programs at least once anIf the
number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use
the do-while loop.d the further execution depends upon the given boolean condition.

public class DoWhileExample {


public static void main(String[] args) {
int i=1;
do{
[Link](i);
i++;
}while(i<=10);
}
}
● Infinitive do-while Loop
● If you pass true in the do-while loop, it will be infinitive do-while loop.

public class DoWhileExample2 {


public static void main(String[] args) {
do{
[Link]("infinitive do while loop");
}while(true);
}
}
● Break Statement
● When a break statement is encountered inside a loop, the loop is immediately terminated and
the programcontrol resumes at the next statement following the loop.
● The Java break is used to break loop or switch statement. It breaks the current flow of the
programat specified condition. In case of inner loop, it breaks only inner loop.
● We can use Java break statement in all types of loops such as for loop, while loop and do-while
loop.

Syntax:
jump-statement;
break;
public class BreakExample {
public static void main(String[] args) {
/ / using for loop
for(int i=1;i<=10;i++){
if(i==5){
/ / breaking the loop
break;
}
[Link](i);
}
}
}
● Break Statement with Inner Loop
It breaks inner loop only if you use break statement inside the inner loop.
Example:
/ / Java Programto illustrate the use of break statement
/ / inside an inner loop
public class BreakExample2 {
public static void main(String[] args) {
/ / outer loop
for(int i=1;i<=3;i++){
/ / inner loop
for(int j=1;j<=3;j++){
if(i==2&&j==2){
/ / using break statement inside the inner loop
break;
}
[Link](i+" "+j);
}
} } }
● Break Statement in while loop
Example:
/ / Java Programto demonstrate the use of break statement
/ / inside the while loop.
public class BreakWhileExample {
public static void main(String[] args) {
/ / while loop
int i=1;
while(i<=10){
if(i==5){
/ / using break statement
i++;
break;/ / it will break the loop
}
[Link](i);
i++;
} }}
● Br eak Statement in do- while loop
Example:
/ / Java Pr ogr amto demonstr ate the use of br eak statement
/ / inside the Java do- while loop.
public class Br eakDoWhileExample {
public static void main(Str ing[] ar gs) {
/ / declar ing var iable
int i=1;
/ / do- while loop
do{
if(i==5){
/ / using br eak statement
i++;
br eak;/ / it will br eak the loop
}
[Link] intln(i);
i++;
}while(i<=10);
}}
● Continue Statement
The continue statement is used in loop control structure when you need to jump to the next
iteration of the loop immediately. It can be used with for loop or while loop.
The Java continue statement is used to continue the loop. It continues the current flow of the
programand skips the remaining code at the specified condition. In case of an inner loop, it
continues the inner loop only.

● We can use Java continue statement in all types of loops such as for loop, while loop and do-
while loop.
● Syntax:
● jump-statement;
● continue;
public class ContinueExample {
public static void main(String[] args) {
/ / for loop
for(int i=1;i<=10;i++){
if(i==5){
/ / using continue statement
continue;/ / it will skip the rest statement
}
[Link](i);
}
}
}
● Continue Statement with Inner Loop
It continues inner loop only if you use the continue statement inside the inner loop.

public class ContinueExample2 {


public static void main(String[] args) {
/ / outer loop
for(int i=1;i<=3;i++){
/ / inner loop
for(int j=1;j<=3;j++){
if(i==2&&j==2){
/ / using continue statement inside inner loop
continue;
}
[Link](i+" "+j);
} }}}
● Types of Java Comments
● There are 3 types of comments in java.

1. Single Line Comment


/ / This is single line comment
1. Multi Line Comment
/*
multi line
comment
*/
1. Documentation Comment
/ **
documentation
comment
*/
● Java Naming conventions
Java naming convention is a rule to follow as you decide what to name your identifiers such as class,
package, variable, constant, method, etc

Advantage of naming conventions in java

● The following are the key rules that must be followed by every identifier:
● The name must not contain any white spaces.
● The name should not start with special characters like & (ampersand), $ (dollar), _ (underscore).
What is a class in Java
● A class is a group of objects which have common properties. It is a template or blueprint from
which objects are created. It is a logical entity. It can't be physical.
● A class in Java can contain:
● Fields
● Methods
● Constructors
● Blocks
● Nested class and interface

Method in Java
● In Java, a method is like a function which is used to expose the behavior of an object.
● Advantage of Method
1. Code Reusability
1. Code Optimization
● Class
It should start with the uppercase letter.
It should be a noun such as Color, Button, System, Thread, etc.
Use appropriate words, instead of acronyms.
Example: -
public class Employee
{
/ / code snippet
}
● What is an object in Java

● An object has three characteristics:


● State:represents the data (value) of an object.
● Behavior: represents the behavior (functionality) of an object such as deposit, withdraw, etc.
● Identity: An object identity is typically implemented via a unique ID. The value of the ID is not
visible to the external user. However, it is used internally by the JVMto identify each object
uniquely.

● Object Definitions:
● An object is a real- world entity.
● An object is a runtime entity.
● The object is an entity which has state and behavior.
● The object is an instance of a class.
● new keyword in Java
The new keyword is used to allocate memory at runtime. All objects get memory in Heap memory
area.

● we created a Student class which has two data members id and name. We are creating the
object of the Student class by new keyword and printing the object's value.
● Here, we are creating a main() method inside the class.
● Object and Class Example: main within the class
● In this example, we have created a Student class which has two data members id and name.
We are creating the object of the Student class by new keyword and printing the object's
value.
● public class Student{
● / / defining fields
● int id;/ / field or data member or instance variable
● String name;
● / / creating main method inside the Student class
● public static void main(String args[]){
● / / Creating an object or instance
● Student s1=new Student();/ / creating an object of Student
● / / Printing values of the object
● [Link]([Link]);/ / accessing member through reference variable
● [Link]([Link]);
● } }
3 Ways to initialize object
There are 3 ways to initialize object in java.
1. By reference variable
1. By method
1. By constructor
● Initialization through reference
class Student{
int id;
String name; }
class TestStudent2{
public static void main(String args[]){
Student s1=new Student();
[Link]=101;
[Link]=“Aman Tiwari";
[Link]([Link]+" "+[Link]);/ / printing members with a white space
} }
We can also cr eate multiple objects and stor e infor mation in it thr ough r efer ence var iable
class Student{
int id;
Str ing name;
}
class TestStudent3{
public static void main(Str ing ar gs[]){
/ / Cr eating objects
Student s1=new Student();
Student s2=new Student();
/ / Initializing objects
[Link]=101;
[Link]="Sonoo";
[Link]=102;
[Link]="Amit";
/ / Pr inting data
[Link] intln([Link]+" "+[Link]);
[Link] intln([Link]+" "+[Link]);
} }
● Initialization throughmethod
● In this example, we are creating the two objects of Student class andinitializing the value to these objects by invoking the
insertRecordmethod.
class Student{
int rollno;
String name;
voidinsertRecord(int r, String n){
rollno=r;
name=n;
}
voiddisplayInformation(){[Link](rollno+" "+name);}
}
class TestStudent4{
public static voidmain(String args[]){
Student s1=new Student();
Student s2=new Student();
[Link](111,"Karan");
[Link](222,"Aryan");
[Link]();
[Link]();
} }
● Initialization through a constructor
class Employee{
int id; String name; float salary;
void insert(int i, String n, float s) { id=i; name=n; salary=s; }
void display(){[Link](id+" "+name+" "+salary);}
}
public class TestEmployee {
public static void main(String[] args) {
Employee e1=new Employee();
Employee e2=new Employee();
Employee e3=new Employee();
[Link](101,"ajeet",45000);
[Link](102,"irfan",25000);
[Link](103,"nakul",55000);
[Link](); [Link](); [Link]();
} }
● Constructors in Java
● In Java, a constructor is a block of codes similar to the method. It is called when an instance of
the object is created, and memory is allocated for the object.

● When is a constructor called


● Every time an object is created using new() keyword, at least one constructor is called. It calls a
default constructor.

Rules for creating Java constructor


● There are two rules defined for the constructor.
1. Constructor name must be the same as its class name
1. A Constructor must have no explicit return type
1. A Java constructor cannot be abstract, static, final, and synchronized
● Types of Java constructors
● There are two types of constructors in Java:
1. Default constructor (no-arg constructor)
1. Parameterized constructor

Example of default constructor


we are creating the no-arg constructor in the Bike class.
class Bike1{
/ / creating a default constructor
Bike1(){[Link]("Bike is created");}
/ / main method
public static void main(String args[]){
/ / calling a default constructor
Bike1b=new Bike1();
} }
● Parameterized Constructor
● A constructor which has a specific number of parameters is called a parameterized constructor.
● Why use the parameterized constructor?
● The parameterized constructor is used to provide different values to the distinct objects.
However, you can provide the same values also.
class Student4{
int id; String name;
/ / creating a parameterized constructor
Student4(int i,String n){ id = i; name = n; }
/ / method to display the values
void display(){[Link](id+" "+name);}
public static void main(String args[]){
/ / creating objects and passing values
Student4 s1= new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
/ / calling method to display the values of object
[Link]();
[Link]();
} }
Thank You
amantiwari8861@[Link]

You might also like