0% found this document useful (0 votes)
19 views

Chapter 2 Java

Constructor properties in Java include: - Must have the same name as the class - Must be declared public - Is invoked automatically when an object is created - Should not have a return type - Can allocate/deallocate memory - Multiple constructors can exist in a class The document discusses default constructors in Java and how to call them. It also covers copy constructors, nesting methods, overloading constructors, and the 'this' keyword. Finally, it provides an example of passing command line arguments to a Java program.

Uploaded by

shrey6346
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Chapter 2 Java

Constructor properties in Java include: - Must have the same name as the class - Must be declared public - Is invoked automatically when an object is created - Should not have a return type - Can allocate/deallocate memory - Multiple constructors can exist in a class The document discusses default constructors in Java and how to call them. It also covers copy constructors, nesting methods, overloading constructors, and the 'this' keyword. Finally, it provides an example of passing command line arguments to a Java program.

Uploaded by

shrey6346
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 70

2: Derived Syntactical Constructs in Java

S. A. Salunkhe
Properties of Constructor

• Name of constructor must be the same as the name


of class for which it is being used.
• The constructor must be declared in the public mode
• The constructor gets invoked automatically when an
object gets created.
• The constructor should not have any return type.
Even void data type should not written for the
constructor.
• The constructor can make use of new or delete
operators for allocating and deallocating memory
• Multiple constructors can be used by the same class.
• //Java Program to create and call a default constructor
• class Bike1
• {
• //creating a default constructor
• Bike1()
• {System.out.println("Bike is created");
• }
• //main method
• public static void main(String args[])
• {
• //calling a default constructor
• Bike1 b=new Bike1();
• }
• }
• Output:
• Bike is created
Copy Constructor

• There is no copy constructor in Java. However, we can copy


the values from one object to. another like copy constructor
• class Student6{
• int id;
• String name;
• //constructor to initialize integer and string
• Student6(int i,String n){
• id = i;
• name = n;
• }
• //constructor to initialize another object
• Student6(Student6 s){
• id = s.id;
• name =s.name;
• }
• void display()
• {System.out.println(id+" "+name);}
Output:

111 Karan
• public static void main(String args[]){
111 Karan
• Student6 s1 = new Student6(111,"Karan");
• Student6 s2 = new Student6(s1);
• s1.display();
• s2.display();
• }
• }
NESTING METHODS

• A method of a class can be called only by an object


of that class using the dot operator.
• However there is an exception to this a method can
be called by using only its name by another method
of the same class. This is known as nesting of
methods.
Difference between constructor and method in Java

Java Constructor Java Method

A constructor is used to initialize the A method is used to expose the behavior


state of an object. of an object.

A constructor must not have a return A method must have a return type.
type.

The constructor is invoked implicitly. The method is invoked explicitly.

The Java compiler provides a default The method is not provided by the
constructor if you don't have any compiler in any case.
constructor in a class.

The constructor name must be same as The method name may or may not be
the class name. same as the class name.
• //Java program to overload constructors • void display()
• class Student5 • {
• { •
• int id; System.out.println(id+" "+name+" "+
• String name; age);
• int age; • }
• //creating two arg constructor

• Student5(int i,String n)
• public static void main(String args[]
• {
)
• id = i;
• {
• name = n;
• } Student5 s1 = new Student5(111,"Karan"
• //creating three arg constructor );
• Student5(int i,String n,int a) Student5 s2 = new Student5(222,"Aryan"
• { ,25);
• id = i; • s1.display();
• name = n; • s2.display();
• age=a; • }
• } • }
This Keyword
Using This Keyword Without This Keyword

• class Student{ • lass Student{


• int rollno; • int rollno;
• String name; • String name;
• float fee; • float fee;
• Student(int rollno,String name,float fee){ • Student(int r,String n,float f){
• this.rollno=rollno; • rollno=r;
• this.name=name; • name=n;
• this.fee=fee; • fee=f;
• } • }
• void display() • void display()
{System.out.println(rollno+" "+name+" "+fee); {System.out.println(rollno+" "+name+" "+fee);
} }
• } • }
• •
• class TestThis2{ • class TestThis3{
• public static void main(String args[]){ • public static void main(String args[]){
• Student s1=new Student(111,"ankit",5000f); • Student s1=new Student(111,"ankit",5000f);
• Student s2=new Student(112,"sumit",6000f); • Student s2=new Student(112,"sumit",6000f);
• s1.display(); • s1.display();
• s2.display(); • s2.display();
• }} • }}
Output: Your first argument is: sonoo
Output: Your first argument is: sonoo
Output: Your first argument is: sonoo
class CommandLineExample Output: Your first argument is: sonoo
Output: Your first argument is: sonoo
{ Output: Your first argument is: sonoo
public static void main(String args[])
{
System.out.println("Your first argument is: "+args[0]);
}
}

compile by > javac CommandLineExample.java

run by > java CommandLineExample sonoo

Output: Your first argument is: sonoo


ARRAY
Operations on String using String Class
Wrapper Class
• Wrapper classes are classes that allow
primitive types to be accessed as objects.

• Wrapper class is wrapper around a primitive


data type because they "wrap" the primitive
data type into an object of that class.
What is Wrapper Class?
• Each of Java's eight primitive data types has a
class dedicated to it.
• They are one per primitive type.
• Wrapper classes make the primitive type data to
act as objects. Simple Type Wrapper class
boolean Boolean
char Character
double Double
float Float
int Integer
long Long
short Short
byte Byte
Difference b/w Primitive Data Type and
Object of a Wrapper Class
• The following two statements illustrate the difference
between a primitive data type and an object of a wrapper
class:
int x = 25;
Integer y = new Integer(33);

Clearly x and y differ by more than their values:


• x is a variable that holds a value;
• y is an object variable that holds a reference to an
object.
So, the following statement using x and y as declared
above is not allowed:
int z = x + y; // wrong!

You might also like