What is the purpose of a default constructor? The purpose of the default constructor is to assign the default value to the objects. The java compiler creates a default constructor implicitly if there is no constructor in the class. more questions visit:- https://cjavapoint.blogspot.com/
Example:ββ- class Student3{
int id;
String name;
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student3 s1=new Student3();
Student3 s2=new Student3();
s1.display();
s2.display();
}
} more questions visit:- https://cjavapoint.blogspot.com/
int id;
String name;
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student3 s1=new Student3();
Student3 s2=new Student3();
s1.display();
s2.display();
}
} more questions visit:- https://cjavapoint.blogspot.com/
Can we overload the constructors? Yes, the constructors can be overloaded by changing the number of arguments accepted by the constructor or by changing the data type of the parameters. Consider the following example. class Test
{
int i;
public Test(int k)
{
i=k;
}
public Test(int k, int m)
{
System.out.println("Hi I am assigning the value max(k, m) to i");
if(k>m)
{
i=k;
}
else
{
i=m;
}
}
}
public class Main
{
public static void main (String args[])
{
Test test1 = new Test(10);
Test test2 = new Test(12, 15);
System.out.println(test1.i);
System.out.println(test2.i);
}
}
{
int i;
public Test(int k)
{
i=k;
}
public Test(int k, int m)
{
System.out.println("Hi I am assigning the value max(k, m) to i");
if(k>m)
{
i=k;
}
else
{
i=m;
}
}
}
public class Main
{
public static void main (String args[])
{
Test test1 = new Test(10);
Test test2 = new Test(12, 15);
System.out.println(test1.i);
System.out.println(test2.i);
}
}
What do you understand by copy constructor in Java? There is no copy constructor in java. However, we can copy the values from one object to another like copy constructor in C++.
There are many ways to copy the values of one object into another in java. They are:
By constructor
By assigning the values of one object into another
By clone() method of Object class
There are many ways to copy the values of one object into another in java. They are:
By constructor
By assigning the values of one object into another
By clone() method of Object class
Forwarded from java interview Questions
Core java interview Questionsπππ Composition
without existing container object if there is no chance of existing contained object
then the relation between container object and contained object is called composition
which is a strong association.
Example:
Any university consists of several departments whenever university object destroys automatically
all the department object will be destroys that is without existing dependent object hence there are associated and this relationship is called composition .more questions visit:- πππhttps://cjavapoint.blogspot.com/
without existing container object if there is no chance of existing contained object
then the relation between container object and contained object is called composition
which is a strong association.
Example:
Any university consists of several departments whenever university object destroys automatically
all the department object will be destroys that is without existing dependent object hence there are associated and this relationship is called composition .more questions visit:- πππhttps://cjavapoint.blogspot.com/
Forwarded from java interview Questions (Raj patel)
Without existing container object if there is a chance of existing contained objects
such type of relationship is called aggregation .
in aggregation objects have weak association. https://cjavapoint.blogspot.com/2019/12/aggregation.htmlππππππ
such type of relationship is called aggregation .
in aggregation objects have weak association. https://cjavapoint.blogspot.com/2019/12/aggregation.htmlππππππ
What is meant by Interface?
Answer: Multiple inheritances cannot be achieved in java. To overcome this problem Interface concept is introduced.
An interface is a template which has only method declarations and not the method implementation.
For more questions visit
ππhttps://cjavapoint.blogspot.com/?m=1ππ
Answer: Multiple inheritances cannot be achieved in java. To overcome this problem Interface concept is introduced.
An interface is a template which has only method declarations and not the method implementation.
For more questions visit
ππhttps://cjavapoint.blogspot.com/?m=1ππ
Blogspot
Core Java Interview Questions - Learn Java
Java Tutorial or Learn Java or Core Java Tutorial or Java Programming Tutorials for beginners and professionals with core concepts and examples
For more questions visitππhttps://cjavapoint.blogspot.com/?m=1ππ
Visit ππ https://cjavapoint.blogspot.com/?m=1ππ
What is finally and finalize in java ?
The finally block is used with try-catch to put the code that you want to get executed always, even if an exception is thrown by the try-catch block. finally block is mostly used to release resources created in the try block.
finalize() is a special method in Object class that we can override in our classes. This method gets called by the garbage collector when the object is getting garbage collected. This method is usually overridden to release system resources when the object is garbage collected.
For more questions visit πππ https://cjavapoint.blogspot.com/?m=1
The finally block is used with try-catch to put the code that you want to get executed always, even if an exception is thrown by the try-catch block. finally block is mostly used to release resources created in the try block.
finalize() is a special method in Object class that we can override in our classes. This method gets called by the garbage collector when the object is getting garbage collected. This method is usually overridden to release system resources when the object is garbage collected.
For more questions visit πππ https://cjavapoint.blogspot.com/?m=1
Blogspot
Core Java Interview Questions - Learn Java
Java Tutorial or Learn Java or Core Java Tutorial or Java Programming Tutorials for beginners and professionals with core concepts and examples
What is static import?
If we have to use any static variable or method from other class, usually we import the class and then use the method/variable with class name.
import java.lang.Math; //inside class double test = Math.PI * 5;
We can do the same thing by importing the static method or variable only and then use it in the class as if it belongs to it.
import static java.lang.Math.PI; //no need to refer class now double test = PI * 5;
Use of static import can cause confusion, so itβs better to avoid it. Overuse of static import can make your program unreadable and unmaintainable.
More questions visit https://cjavapoint.blogspot.com/?m=1 ππ
If we have to use any static variable or method from other class, usually we import the class and then use the method/variable with class name.
import java.lang.Math; //inside class double test = Math.PI * 5;
We can do the same thing by importing the static method or variable only and then use it in the class as if it belongs to it.
import static java.lang.Math.PI; //no need to refer class now double test = PI * 5;
Use of static import can cause confusion, so itβs better to avoid it. Overuse of static import can make your program unreadable and unmaintainable.
More questions visit https://cjavapoint.blogspot.com/?m=1 ππ
Blogspot
Core Java Interview Questions - Learn Java
Java Tutorial or Learn Java or Core Java Tutorial or Java Programming Tutorials for beginners and professionals with core concepts and examples
What is Marker interface?
A marker interface is an empty interface without any method but used to force some functionality in implementing classes by Java. Some of the well known marker interfaces are Serializable and Cloneable.
For More questions visit https://cjavapoint.blogspot.com/?m=1 πππ
A marker interface is an empty interface without any method but used to force some functionality in implementing classes by Java. Some of the well known marker interfaces are Serializable and Cloneable.
For More questions visit https://cjavapoint.blogspot.com/?m=1 πππ
Blogspot
Core Java Interview Questions - Learn Java
Java Tutorial or Learn Java or Core Java Tutorial or Java Programming Tutorials for beginners and professionals with core concepts and examples
Q) What are the main differences between Process and thread? Explain in brief.
1) One process can have multiple threads. A thread is a smaller part of a process.
2) Every process has its own memory space, executable code and a unique process identifier (PID) while every thread has its own stack in Java but it uses process main memory and shares it with other threads.
3) Threads of same process can communicate with each other using keyword like wait and notify etc. This process is known as inter process communication
For questions visit:-
https://cjavapoint.blogspot.com/?m=1
πππππππππ
1) One process can have multiple threads. A thread is a smaller part of a process.
2) Every process has its own memory space, executable code and a unique process identifier (PID) while every thread has its own stack in Java but it uses process main memory and shares it with other threads.
3) Threads of same process can communicate with each other using keyword like wait and notify etc. This process is known as inter process communication
For questions visit:-
https://cjavapoint.blogspot.com/?m=1
πππππππππ
Blogspot
Core Java Interview Questions - Learn Java
Java Tutorial or Learn Java or Core Java Tutorial or Java Programming Tutorials for beginners and professionals with core concepts and examples
Q) How can we create a thread in java?
There are following two ways of creating a thread:
1) By Implementing Runnable interface.
2) By Extending Thread class
ππππππππππ
There are following two ways of creating a thread:
1) By Implementing Runnable interface.
2) By Extending Thread class
ππππππππππ
Blogspot
Core Java Interview Questions - Learn Java
Java Tutorial or Learn Java or Core Java Tutorial or Java Programming Tutorials for beginners and professionals with core concepts and examples
Java program to ReverseString using ByteArrayCore java String πππππππππππππ/ Java program to ReverseString using ByteArray.
import java.lang.*;
import java.io.*;
import java.util.*;
// Class of ReverseString
class ReverseString
{
public static void main(String[] args)
{
String input = "JavaInterview";
// getBytes() method to convert string
// into bytes[].
byte [] strAsByteArray = input.getBytes();
byte [] result =
new byte [strAsByteArray.length];
// Store result in reverse order into the
// result byte[]
for (int i = 0; i<strAsByteArray.length; i++)
result[i] =
strAsByteArray[strAsByteArray.length-i-1];
System.out.println(new String(result));
}
}
Output:
weivretniavaj More java interview questions and java program visit my blog πππππhttps://cjavapoint.blogspot.com/πππππππ
import java.lang.*;
import java.io.*;
import java.util.*;
// Class of ReverseString
class ReverseString
{
public static void main(String[] args)
{
String input = "JavaInterview";
// getBytes() method to convert string
// into bytes[].
byte [] strAsByteArray = input.getBytes();
byte [] result =
new byte [strAsByteArray.length];
// Store result in reverse order into the
// result byte[]
for (int i = 0; i<strAsByteArray.length; i++)
result[i] =
strAsByteArray[strAsByteArray.length-i-1];
System.out.println(new String(result));
}
}
Output:
weivretniavaj More java interview questions and java program visit my blog πππππhttps://cjavapoint.blogspot.com/πππππππ