Saturday, September 27, 2025
HomeLanguagesJavaAtomicReference get() method in Java with Examples

AtomicReference get() method in Java with Examples

The get() method of a AtomicReference class is used to return the value of this AtomicReference object with memory semantics of reading as if the variable was declared volatile type of variable.

Syntax:

public final V get()

Parameters: This method accepts nothing.

Return value: This method returns current value of this AtomicReference.

Below programs illustrate the get() method:
Program 1:




// Java program to demonstrate
// AtomicReference.get() method
  
import java.util.concurrent.atomic.AtomicReference;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an atomic reference object
        // which stores Integer.
        AtomicReference<Integer> ref
            = new AtomicReference<Integer>();
  
        // set some value
        ref.set(12);
  
        // get and print value
        int value = ref.get();
        System.out.println("value  = " + value);
    }
}


Output:

value  = 12

Program 2:




// Java program to demonstrate
// AtomicReference.get() method
  
import java.util.concurrent.atomic.AtomicReference;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an atomic reference object
        // which stores String.
        AtomicReference<String> ref
            = new AtomicReference<String>();
  
        // set some value
        ref.set("GEEKS FOR GEEKS");
  
        // get and print value
        String value = ref.get();
        System.out.println("V  = " + value);
    }
}


Output:

V  = GEEKS FOR GEEKS

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/atomic/AtomicReference.html#get()

RELATED ARTICLES

Most Popular

Dominic
32323 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6690 POSTS0 COMMENTS
Nicole Veronica
11857 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11913 POSTS0 COMMENTS
Shaida Kate Naidoo
6806 POSTS0 COMMENTS
Ted Musemwa
7073 POSTS0 COMMENTS
Thapelo Manthata
6763 POSTS0 COMMENTS
Umr Jansen
6768 POSTS0 COMMENTS