Found 9150 Articles for Object Oriented Programming

Deadlock in Java Multithreading

Samual Sam
Updated on 19-Jun-2020 12:30:28

2K+ Views

Deadlock describes a situation where two or more threads are blocked forever, waiting for each other. Deadlock occurs when multiple threads need the same locks but obtain them in a different order. A Java multithreaded program may suffer from the deadlock condition because the synchronized keyword causes the executing thread to block while waiting for the lock, or monitor, associated with the specified object. Here is an example.ExampleLive Demopublic class TestThread {    public static Object Lock1 = new Object();    public static Object Lock2 = new Object();    public static void main(String args[]) {       ThreadDemo1 T1 = ... Read More

GregorianCalendar Class in Java

Samual Sam
Updated on 19-Jun-2020 12:40:18

258 Views

GregorianCalendar is a concrete implementation of a Calendar class that implements the normal Gregorian calendar with which you are familiar. We did not discuss Calendar class in this tutorial, you can look up standard Java documentation for this.The getInstance( ) method of Calendar returns a GregorianCalendar initialized with the current date and time in the default locale and time zone. GregorianCalendar defines two fields: AD and BC. These represent the two eras defined by the Gregorian calendar.There are also several constructors for GregorianCalendar objects −Sr.No.Constructor & Description1GregorianCalendar()Constructs a default GregorianCalendar using the current time in the default time zone with the default ... Read More

Sleeping for a while in Java

Samual Sam
Updated on 19-Jun-2020 12:04:25

830 Views

You can sleep for any period of time from one millisecond up to the lifetime of your computer. For example, the following program would sleep for 3 seconds −Example Live Demoimport java.util.*; public class SleepDemo {    public static void main(String args[]) {       try {                    System.out.println(new Date( ) + "");                    Thread.sleep(5*60*10);                    System.out.println(new Date( ) + "");               } catch (Exception e) {          System.out.println("Got an exception!");             }    } }This will produce the following result −OutputSun May 03 18:04:41 GMT 2009 Sun May 03 18:04:51 GMT 2009

Create translucent windows in Java Swing

Samual Sam
Updated on 19-Jun-2020 12:10:44

413 Views

With JDK 7, we can create a translucent window using swing very easily. With following code, a JFrame can be made translucent.// Set the window to 55% opaque (45% translucent). frame.setOpacity(0.55f);ExampleSee the example below of a window with 55% translucency.import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class Tester {    public static void main(String[] args)  {                     JFrame.setDefaultLookAndFeelDecorated(true);          // Create the GUI on the event-dispatching thread          SwingUtilities.invokeLater(new Runnable() {          @Override          public void ... Read More

Are static local variables allowed in Java?

karthikeya Boyini
Updated on 18-Jun-2020 15:41:40

1K+ Views

Unlike C, C++, Java does not allow static local variables. The compiler will throw the compilation error.ExampleCreate a java class named Tester.Tester.javaLive Demopublic class Tester {    public static void main(String args[]) {       static int a = 10;    } }OutputCompile and Run the file to verify the result.Tester.java:3: error: illegal start of expression                    static int a = 10;

Assigning values to static final variables in java

Samual Sam
Updated on 18-Jun-2020 15:45:03

935 Views

In java, a non-static final variable can be assigned a value at two places.At the time of declaration.In constructor.ExampleLive Demopublic class Tester {    final int A;    //Scenario 1: assignment at time of declaration    final int B = 2;    public Tester() {       //Scenario 2: assignment in constructor       A = 1;    }    public void display() {       System.out.println(A + ", " + B);    }    public static void main(String[] args) {               Tester tester = new Tester();   ... Read More

Can we overload or override a static method in Java?

Shriansh Kumar
Updated on 16-Apr-2025 19:38:32

4K+ Views

In Java, you cannot override static methods but you can overload them. Overloading a static method is allowed because the JVM determines which method to call at compile time based on the method signature, rather than the object's type. However, overriding is not allowed for static methods because an overridden method is called based on the object's type. In this article, we are going to discuss is it possible to overload and override a static method in Java. Also, we will understand static methods, method overloading and method overriding. What is a Static Method? A method defined using the static ... Read More

Assigning long values carefully in java to avoid overflow

karthikeya Boyini
Updated on 18-Jun-2020 15:24:22

314 Views

In case of having the operation of integer values in Java, we need to be aware of int underflow and overflow conditions. Considering the fact that in Java, The int data type is a 32-bit signed two's complement integer having a minimum value of -2, 147, 483, 648 and a maximum value of 2, 147, 483, 647. If a value goes beyond the max value possible, the value goes back to minimum value and continue from that minimum. In a similar way, it happens for a value less than the min value. Consider the following example.ExampleLive Demopublic class Tester { ... Read More

Copying file using FileStreams in Java

Samual Sam
Updated on 18-Jun-2020 15:26:45

346 Views

This example shows how to copy the contents of one file into another file using read & write methods of FileStreams classes.ExampleLive Demoimport java.io.*; public class Main {    public static void main(String[] args) throws Exception {       BufferedWriter out1 = new BufferedWriter(new FileWriter("srcfile"));       out1.write("string to be copied");       out1.close();       InputStream in = new FileInputStream(new File("srcfile"));       OutputStream out = new FileOutputStream(new File("destnfile"));       byte[] buf = new byte[1024];       int len;       while ((len = in.read(buf)) > 0) { ... Read More

Conversion of Stream To Set in Java

karthikeya Boyini
Updated on 18-Jun-2020 15:28:33

3K+ Views

We can convert a stream to set using the following ways.Using stream.collect() with Collectors.toSet() method - Stream collect() method iterates its elements and stores them in a collection.collect(Collector.toSet()) method.Using set.add() method - Iterate stream using forEach and then add each element to the set.ExampleLive Demoimport java.util.*; import java.util.stream.Stream; import java.util.stream.Collectors;   public class Tester {            public static void main(String[] args) {       Stream stream = Stream.of("a", "b", "c", "d");       // Method 1       Set set = stream.collect(Collectors.toSet());       set.forEach(data -> System.out.print(data + " ")); ... Read More

Advertisements