1. sleep() is a blocking operation that keeps a hold on the monitor / lock of the shared object for the specified number of milliseconds.
2. wait(), on the other hand, simply pauses the thread until either (a) the specified number of milliseconds have elapsed or (b) it receives a desired notification from another thread (whichever is first), without keeping a hold on the monitor/lock of the shared object.
sleep() is most commonly used for polling, or to check for certain results, at a regular interval. wait() is generally used in multithreaded applications, in conjunction with notify() / notifyAll(), to achieve synchronization and avoid race conditions.
Posted Date:- 2021-08-18 09:15:27
The name reflection is used to describe code which is able to inspect other code in the same system (or itself) and to make modifications at runtime.
For example,
say you have an object of an unknown type in Java, and you would like to call a 'doSomething' method on it if one exists. Java's static typing system isn't really designed to support this unless the object conforms to a known interface, but using reflection, your code can look at the object and find out if it has a method called 'doSomething' and then call it if you want to
Posted Date:- 2021-08-18 09:13:16
JDBC is an abstraction layer that allows users to choose between databases. JDBC enables developers to write database applications in Java, without having to concern themselves with the underlying details of a particular database.
Posted Date:- 2021-08-18 09:11:59
On the surface, an application context is the same as a bean factory. Both load bean definitions, wire beans together, and dispense beans upon request. But it also provides:
A means for resolving text messages, including support for
1. Internationalization
2. A generic way to load file resources
3. Events to beans that are registered as listeners
Posted Date:- 2021-08-18 09:11:00
These methods can be used as a hint to the JVM, in order to start a garbage collection. However, this it is up to the Java Virtual Machine (JVM) to start the garbage collection immediately or later in time.
Posted Date:- 2021-08-18 09:08:01
Spring is an open source development framework for enterprise Java. The core features of the Spring Framework can be used in developing any Java application, but there are extensions for building web applications on top of the Java EE platform. Spring framework targets to make J2EE development easier to use and promote good programming practice by enabling a POJO-based (Plain Old Java Object) programming model.
Posted Date:- 2021-08-18 09:07:04
A Java virtual machine (JVM) is a process virtual machine that can execute Java bytecode. Each Java source file is compiled into a bytecode file, which is executed by the JVM. Java was designed to allow application programs to be built that could be run on any platform, without having to be rewritten or recompiled by the programmer for each separate platform. A Java virtual machine makes this possible, because it is aware of the specific instruction lengths and other particularities of the underlying hardware platform.
Posted Date:- 2021-08-18 09:02:49
Java program to find all substrings of a String.
For example: If input is “abb†then output should be “aâ€, “bâ€,â€bâ€, “abâ€, “bbâ€, “abbâ€
We will use String class’s subString method to find all subString.
Posted Date:- 2021-08-18 08:56:54
Anagrams means if two String have same characters but in different order.
For example: Angel and Angel are anagrams
There are many ways to check if Strings are anagrams. Some of them are:
1. Using String methods
2. Using array.sort
Posted Date:- 2021-08-18 08:53:27
There are many ways to do it, some of them are:
1. Using for loop
2. Using recursion
3. Using StringBuffer
Posted Date:- 2021-08-18 08:50:51
Even if no explicit constructor is defined in a java class, objects get created successfully as a
default constructor is implicitly used for object creation. This constructor has no parameters.
Posted Date:- 2021-08-18 08:48:46
Break is used after each case (except the last one) in a switch so that code breaks after the
valid case and doesn't flow in the proceeding cases too.
If break isn't used after each case, all cases after the valid case also get executed resulting in
wrong results.
Posted Date:- 2021-08-18 08:47:35
In java, string objects are called immutable as once value has been assigned to a string, it
can't be changed and if changed, a new object is created.
In below example, reference str refers to a string object having value "Value one".
String str="Value One";
When a new value is assigned to it, a new String object gets created and the reference is moved to
the new object.
str="New Value";
Posted Date:- 2021-08-18 08:46:26
The constructor of a class is invoked every time an object is created with new keyword.
For example, in the following class two objects are created using new keyword and hence,
constructor is invoked two times.
public class const_example {
const_example() {
system.out.println("Inside constructor");
}
public static void main(String args[]) {
const_example c1=new const_example();
const_example c2=new const_example();
}
}
Posted Date:- 2021-08-18 08:45:08
1) Accessing an element that does not exist in the array.
2) Invalid conversion of number to string and string to a number.
(NumberFormatException)
3) The invalid casting of class
(Class cast Exception)
4) Trying to create an object for interface or abstract class
(Instantiation Exception)
Posted Date:- 2021-08-18 08:43:02
Yes an Interface can inherit another Interface, for that matter an Interface can extend more than
one Interface.
Posted Date:- 2021-08-18 08:42:09
A static method should not refer to instance variables without creating an instance and cannot use
"this" operator to refer the instance.
Posted Date:- 2021-08-18 08:41:31
setDaemon method is used to create a daemon thread.
Posted Date:- 2021-08-18 08:40:48
Daemon thread is a low priority thread, which runs intermittently in the back ground doing the
garbage collection operation for the java runtime system.
Posted Date:- 2021-08-18 08:39:27
There are various reasons to make String immutable.
1. Thread Safe
2. Security
3. Class Loading
4. Cache hash value
5. String pool
Posted Date:- 2021-08-18 08:38:13
We have inserted three elements and printed the size of the ArrayList.
Then, we have used While Loop with an iterator. Whenever the iterator has (next) element, it will display that element until we reach the end of the list. So it will iterate three times.
Likewise, we have done for Advanced For Loop where we have created an object called obj for the ArrayList called list. Then printed the object.
Thereafter, we have put the condition of For Loop where the iterator i is set to 0 index, then it is incremented by 1 until the ArrayList limit or size is reached. Finally, we have printed each element using a get(index) method for each iteration of For Loop.
import java.util.*;
public class arrayList {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("20");
list.add("30");
list.add("40");
System.out.println(list.size());
System.out.println("While Loop:");
Iterator itr = list.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
System.out.println("Advanced For Loop:");
for(Object obj : list) {
System.out.println(obj);
}
System.out.println("For Loop:");
for(int i=0; i<list.size(); i++) {
System.out.println(list.get(i));
}
}
}
Output:
3
While Loop:
20
30
40
Advanced For Loop:
20
30
40
For Loop:
20
30
40
Posted Date:- 2021-08-18 08:34:51
We can use only two access modifiers for class public and default.
public: A class with a public modifier can be visible
1) In the same class
2) In the same package subclass
3) In the same package nonsubclass
4) In the different package subclass
5) In the different package nonsubclass.
Posted Date:- 2021-08-18 08:31:01
When we declare variables are created in the stack. So when the variable is out of scope those variables get garbage collected.
Posted Date:- 2021-08-18 08:30:11
We can handle exceptions in either of the two ways :
1) By specifying a try-catch block where we can catch the exception.
2) Declaring a method with throws clause.
Posted Date:- 2021-08-18 08:29:14
There are two ways to do synchronization in java:
1) Synchronized methods
2) Synchronized blocks
To do synchronization we use the synchronized keyword.
Posted Date:- 2021-08-18 08:27:36
If there is no chance of raising an exception in our code then we can’t declare catch block for handling
checked exceptions. This raises a compile-time error if we try to handle checked exceptions when there is
no possibility of causing an exception.
Posted Date:- 2021-08-18 08:26:51
The term synchronization is the ability to control the access of multiple threads to shared resources. And it is important because, without it, it is not possible for one thread to modify a shared object while another thread is in the process of using or updating that object’s value. This often leads to major errors.
Posted Date:- 2021-08-18 08:25:43
We need to implement comparable interface to custom object class(Lets say Country) and then implement compareTo(Object o) method which will be used for sorting. It will provides default way of sorting custom objects.
Posted Date:- 2021-08-18 08:20:51
IS-A relationship HAS- A RELATIONSHIP
Is a relationship also known as inheritance Has a relationship also known as composition or
aggregation.
For IS-A relationship we use extends keyword For Has a relationship we use the new keyword
Posted Date:- 2021-08-18 08:19:59
Thread Groups are a group of threads and other thread groups. It is a way of grouping threads so that
actions can be performed on a set of threads for easy maintenance and security purposes.
Posted Date:- 2021-08-18 08:16:30
If a class is declared within a class and specify the static modifier, the compiler treats the class just
like any other top-level class. Nested top-level class is an Inner class.
Posted Date:- 2021-08-18 08:15:45
java.net.Socket class represents the socket that both the client and server use to communicate
with each other.
Posted Date:- 2021-08-18 08:15:14
It is a public static method used to obtain a reference to the current thread.
Posted Date:- 2021-08-18 07:19:05
Each try block requires at least one catch block or finally block. A try block without a catch or finally will
result in a compiler error. We can skip either of catch or finally block but not both.
Posted Date:- 2021-08-18 07:18:29
The readLine() method returns null when it has reached the end of a file.
Posted Date:- 2021-08-18 07:17:19
No, Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. Java training.
Posted Date:- 2021-08-18 07:16:40
The Wrapped classes are those classes that allow primitive types to be accessed as objects.
Posted Date:- 2021-08-18 07:16:19
If a checked exception may be thrown within the body of a method, the method must either catch the exception or declare it in its throws clause.
Posted Date:- 2021-08-18 07:15:09
String str1= new String("Peter");
String str2= new String("Peter");
Three objects will be created here, two in heap memory and one in String constant pool.
Posted Date:- 2021-08-18 07:14:24
Marker interfaces are those interfaces which do not have any method in it.
Examples of marker interfaces are : Serializable and Cloneable.
Posted Date:- 2021-08-18 07:13:31
ClassPath is environment variable which java virtual machine (JVM) uses to locate all classes which is used by the program.
For example: jre/lib/rt.jar has all java classes and you also need to include jar files or class file which is being used by program.
Posted Date:- 2021-08-18 07:10:05
In java, we can pass argument to a function only by value and not by reference.
Posted Date:- 2021-08-18 07:08:08
The code sleep2000; puts thread aside for exactly two seconds. The code wait2000, causes a wait of
up to two second. A thread could stop waiting earlier if it receives the notify or notifyAll call. The
method wait is defined in the class Object and the method sleep is defined in the class Thread.
Posted Date:- 2021-08-18 07:06:56
The Frame class extends Window to define a main application window that can have a menu bar.
Posted Date:- 2021-08-18 07:05:43
Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set uses only 7 bits,
it is usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18 bit patterns.
UTF-16 uses 16-bit and larger bit patterns.
Posted Date:- 2021-08-18 07:05:11
This is Web Archive File and used to store XML, java classes, and JavaServer pages. which is used
to distribute a collection of JavaServer Pages, Java Servlets, Java classes, XML files, static Web
pages etc.
Posted Date:- 2021-08-18 07:04:39
The preferred size of a component the minimum size of the component, enough to display its label is known as the preferred size of the component. For example, the button size should display its label with platform-specific decoration like dotted lines around the label, etc. The FlowLayout manager gives the preferred size to a component.
Posted Date:- 2021-08-18 06:59:10
Under preemptive scheduling, the highest priority task performs until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task performs for a predefined slice of time and then re-enters the pool of ready tasks.
Posted Date:- 2021-08-18 06:57:39
A transient variable is a variable that may not be serialized.
Posted Date:- 2021-08-18 06:56:45
The inner class defined without any class name is called an anonymous inner class. An inner class is declared and instantiated using the new keyword. The main purpose of anonymous inner classes in java is to provide
interface implementation. We use anonymous classes when we need only one instance for a class.
We can
use all members of the enclosing class and final local variables.
When we compile anonymous inner classes compiler creates two files
Posted Date:- 2021-08-18 06:56:07
Method overloading when a Java program contains more than one methods with the same name but different properties, then it is called method overloading.
Posted Date:- 2021-08-18 06:54:18
The right data type to represent a price in Java BigDecimal, if memory is not a concern and Performance, is not critical, otherwise double with predefined precision.
Posted Date:- 2021-08-18 06:53:09