Unit 1 answers
Unit 1 answers
Unit 1 answers
{
Public static void main(String[] args)
{
System.out.println(“My first program”);
}
}
Besides this learning Java is easy doesn’t require any previous knowledge
of other programming language but requires some logics for writing
programs.
2. The string array can be written in many ways all have the same
meaning.
Public static void main(String[] args) Public static void main(String []args)
Public static void main(String args[])
3. By using three ellipses we can provide var-args to the main().
Public static void main(String...args)
4. Placing a semicolon at the end of class is optional.
Class FirstProgram
{
Static public vid main(String...args)
{
System.out.println(“My first program”);
}
};
Constructor
Constructors perform the job of initializing an object. It resembles a method
in Java, but a constructor is not a method and it doesn’t have any return
type.
Properties of a constructor:
1. Name of the constructor is the same as its class name.
2. A default constructor is always present in a class implicitly.
3. There are three main types of constructors namely default,
parameterized and copy constructors.
4. A constructor in Java can not be abstract, final, static and
Synchronized.
1. Default constructor:
A default constructor is a constructor that is created implicitly by the JVM.
Program to demonstrate default constructors:
Class student
{
Int id;
String name; Student()
{
System.out.println("Student id is 20 and name is Amruta");
}
Public static void main(String args[])
{
Student s = new student();
}
}
2. Parameterized constructor:
It is a constructor that has to be created explicitly by the programmer. This
constructor has parameters that can be passed when a constructor is
called at the time of object creation.
Program:
Class Student4
{
Int id;
String name; Student4(int i,String n)
{
Id = i; Name = n;
}
Void display()
{
System.out.println(id+" "+name);
}
Public static void main(String args[])
{
Student4 s1 = new Student4(20,"Rema");
Student4 s2 = new Student4(23,"Sadhna");
s1.display();
s2.display();
}
}
3. Copy constructor:
A content of one constructor can be copied into another constructor using
the objects created. Such a constructor is called as a copy constructor.
Program:
Class Student6
{
Int id;
String name; Student6(int i,String n)
{
Id = i; Name = n;
}
Student6(Student6 s)
{
Id = s.id;
Name =s.name;
}
Void display()
{
System.out.println(id+" "+name);
}
Public static void main(String args[])
{
Student6 s1 = new Student6(20,"AMRUTA");
Student6 s2 = new Student6(s1);
s1.display();
s2.display();
}
}
Output
111 Karan ITS
222 Aryan ITS
Static method
Any method that uses the static keyword is referred to as a static method.
A class's static method, rather than the class's object, belongs to the class.
A static method can be called without having to create a class instance.
A static method can access and update the value of a static data member.
With variables, methods, blocks, and nested classes, we can use the static
keyword. The static keyword refers to a class rather than a specific
instance of that class
Output
Primitive data types - Boolean, char, byte, short, int, long, float, and double
are examples of primitive data types.
Non - primitives data types - Classes, Interfaces, and Arrays are examples
of non-primitive data types.