How to use Reference class of org.openqa.selenium.docker.internal package

Best Selenium code snippet using org.openqa.selenium.docker.internal.Reference

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1// Licensed to the Software Freedom Conservancy (SFC) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The SFC licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18package org.openqa.selenium.docker.v1_40;
19
20import com.google.common.collect.ImmutableMap;
21import org.openqa.selenium.docker.Image;
22import org.openqa.selenium.docker.internal.ImageSummary;
23import org.openqa.selenium.docker.internal.Reference;
24import org.openqa.selenium.internal.Require;
25import org.openqa.selenium.json.Json;
26import org.openqa.selenium.json.TypeToken;
27import org.openqa.selenium.remote.http.HttpHandler;
28import org.openqa.selenium.remote.http.HttpRequest;
29import org.openqa.selenium.remote.http.HttpResponse;
30
31import java.lang.reflect.Type;
32import java.util.Map;
33import java.util.Set;
34
35import static com.google.common.collect.ImmutableSet.toImmutableSet;
36import static org.openqa.selenium.json.Json.JSON_UTF_8;
37import static org.openqa.selenium.remote.http.Contents.string;
38import static org.openqa.selenium.remote.http.HttpMethod.GET;
39
40class ListImages {
41
42  private static final Json JSON = new Json();
43  private static final Type SET_OF_IMAGE_SUMMARIES = new TypeToken<Set<ImageSummary>>() {}.getType();
44
45  private final HttpHandler client;
46
47  public ListImages(HttpHandler client) {
48    this.client = Require.nonNull("HTTP client", client);
49  }
50
51  public Set<Image> apply(Reference reference) {
52    Require.nonNull("Reference to search for", reference);
53
54    String familiarName = reference.getFamiliarName();
55    Map<String, Object> filters = ImmutableMap.of("reference", ImmutableMap.of(familiarName, true));
56
57    // https://docs.docker.com/engine/api/v1.40/#operation/ImageList
58    HttpRequest req = new HttpRequest(GET, "/v1.40/images/json")
59      .addHeader("Content-Length", "0")
60      .addHeader("Content-Type", JSON_UTF_8)
61      .addQueryParameter("filters", JSON.toJson(filters));
62
63    HttpResponse response = DockerMessages.throwIfNecessary(
64      client.execute(req),
65    "Unable to list images for %s", reference);
66
67    Set<ImageSummary> images =
68      JSON.toType(string(response), SET_OF_IMAGE_SUMMARIES);
69
70    return images.stream()
71      .map(org.openqa.selenium.docker.Image::new)
72      .collect(toImmutableSet());
73  }
74}
75
Full Screen
copy
1public void test() {
2    MyClass obj = null;
3    init(obj);
4    //After calling init method, obj still points to null
5    //this is because obj is passed as value and not as reference.
6}
7private void init(MyClass objVar) {
8    objVar = new MyClass();
9}
10
Full Screen
copy
1public class Test
2{
3    public static void reverseArray(int[] array1)
4    {
5        // ...
6    }
7
8    public static void main(String[] args)
9    {
10        int[] array1 = { 1, 10, -7 };
11        int[] array2 = { 5, -190, 0 };
12
13        reverseArray(array1);
14    }
15}
16
Full Screen
copy
1public class Test
2{
3    public static int[] reverseArray(int[] array1)
4    {
5        int[] array2 = { -7, 0, -1 };
6
7        array1[0] = 5; // array a becomes 5, 10, -7
8
9        array1 = array2; /* array1 of reverseArray starts
10          pointing to c instead of a (not shown in image below) */
11        return array2;
12    }
13
14    public static void main(String[] args)
15    {
16        int[] array1 = { 1, 10, -7 };
17        int[] array2 = { 5, -190, 0 };
18
19        array1 = reverseArray(array1); /* array1 of 
20         main starts pointing to c instead of a */
21    }
22}
23
Full Screen
copy
1public class PassByCopy{
2    public static void changeName(Dog d){
3        d.name = "Fido";
4    }
5    public static void main(String[] args){
6        Dog d = new Dog("Maxx");
7        System.out.println("name= "+ d.name);
8        changeName(d);
9        System.out.println("name= "+ d.name);
10    }
11}
12class Dog{
13    public String name;
14    public Dog(String s){
15        this.name = s;
16    }
17}
18
Full Screen
copy
1public class Test {
2  public static void main(String[] args) {
3    Integer a = new Integer(2);
4    Integer b = new Integer(3);
5    System.out.println("Before: a = " + a + ", b = " + b);
6    swap(a,b);
7    System.out.println("After: a = " + a + ", b = " + b);
8  }
9
10  public static swap(Integer iA, Integer iB) {
11    Integer tmp = iA;
12    iA = iB;
13    iB = tmp;
14  }
15}
16
Full Screen
copy
1public void method (String param) {}
2...
3String variable = new String("ref");
4method(variable);
5method(variable.toString());
6method(new String("ref"));
7
Full Screen
copy
1void getValues(int& arg1, int& arg2) {
2    arg1 = 1;
3    arg2 = 2;
4}
5void caller() {
6    int x;
7    int y;
8    getValues(x, y);
9    cout << "Result: " << x << " " << y << endl;
10}
11
Full Screen
copy
1void getValues(int[] arg1, int[] arg2) {
2    arg1[0] = 1;
3    arg2[0] = 2;
4}
5void caller() {
6    int[] x = new int[1];
7    int[] y = new int[1];
8    getValues(x, y);
9    System.out.println("Result: " + x[0] + " " + y[0]);
10}
11
Full Screen
copy
1public class PassByValueString {
2    public static void main(String[] args) {
3        new PassByValueString().caller();
4    }
5
6    public void caller() {
7        String value = "Nikhil";
8        boolean valueflag = false;
9        String output = method(value, valueflag);
10        /*
11         * 'output' is insignificant in this example. we are more interested in
12         * 'value' and 'valueflag'
13         */
14        System.out.println("output : " + output);
15        System.out.println("value : " + value);
16        System.out.println("valueflag : " + valueflag);
17
18    }
19
20    public String method(String value, boolean valueflag) {
21        value = "Anand";
22        valueflag = true;
23        return "output";
24    }
25}
26
Full Screen
copy
1output : output
2value : Nikhil
3valueflag : false
4
Full Screen
copy
1public class PassByValueNewString {
2    public static void main(String[] args) {
3        new PassByValueNewString().caller();
4    }
5
6    public void caller() {
7        String value = new String("Nikhil");
8        boolean valueflag = false;
9        String output = method(value, valueflag);
10        /*
11         * 'output' is insignificant in this example. we are more interested in
12         * 'value' and 'valueflag'
13         */
14        System.out.println("output : " + output);
15        System.out.println("value : " + value);
16        System.out.println("valueflag : " + valueflag);
17
18    }
19
20    public String method(String value, boolean valueflag) {
21        value = "Anand";
22        valueflag = true;
23        return "output";
24    }
25}
26
Full Screen
copy
1output : output
2value : Nikhil
3valueflag : false
4
Full Screen
copy
1public class PassByValueObjectCase1 {
2
3    private class Student {
4        int id;
5        String name;
6        public Student() {
7        }
8        public Student(int id, String name) {
9            super();
10            this.id = id;
11            this.name = name;
12        }
13        public int getId() {
14            return id;
15        }
16        public void setId(int id) {
17            this.id = id;
18        }
19        public String getName() {
20            return name;
21        }
22        public void setName(String name) {
23            this.name = name;
24        }
25        @Override
26        public String toString() {
27            return "Student [id=" + id + ", name=" + name + "]";
28        }
29    }
30
31    public static void main(String[] args) {
32        new PassByValueObjectCase1().caller();
33    }
34
35    public void caller() {
36        Student student = new Student(10, "Nikhil");
37        String output = method(student);
38        /*
39         * 'output' is insignificant in this example. we are more interested in
40         * 'student'
41         */
42        System.out.println("output : " + output);
43        System.out.println("student : " + student);
44    }
45
46    public String method(Student student) {
47        student.setName("Anand");
48        return "output";
49    }
50}
51
Full Screen
copy
1output : output
2student : Student [id=10, name=Anand]
3
Full Screen
copy
1public class PassByValueObjectCase2 {
2
3    public static void main(String[] args) {
4        new PassByValueObjectCase2().caller();
5    }
6
7    public void caller() {
8        // student has the actual reference to a Student object created
9        // can we change this actual reference outside the local scope? Let's see
10        Student student = new Student(10, "Nikhil");
11        String output = method(student);
12        /*
13         * 'output' is insignificant in this example. we are more interested in
14         * 'student'
15         */
16        System.out.println("output : " + output);
17        System.out.println("student : " + student); // Will it print Nikhil or Anand?
18    }
19
20    public String method(Student student) {
21        student = new Student(20, "Anand");
22        return "output";
23    }
24
25}
26
Full Screen
copy
1output : output
2student : Student [id=10, name=Nikhil]
3
Full Screen
copy
1public static void swap(StringBuffer s1, StringBuffer s2) {
2    StringBuffer temp = s1;
3    s1 = s2;
4    s2 = temp;
5}
6
7
8public static void main(String[] args) {
9    StringBuffer s1 = new StringBuffer("Hello");
10    StringBuffer s2 = new StringBuffer("World");
11    swap(s1, s2);
12    System.out.println(s1);
13    System.out.println(s2);
14}
15
Full Screen
copy
1public static void appendWorld(StringBuffer s1) {
2    s1.append(" World");
3}
4
5public static void main(String[] args) {
6    StringBuffer s = new StringBuffer("Hello");
7    appendWorld(s);
8    System.out.println(s);
9}
10
Full Screen
copy
1public static void appendWorld(String s){
2    s = s+" World";
3}
4
5public static void main(String[] args) {
6    String s = new String("Hello");
7    appendWorld(s);
8    System.out.println(s);
9}
10
Full Screen
copy
1class StringWrapper {
2    public String value;
3
4    public StringWrapper(String value) {
5        this.value = value;
6    }
7}
8
9public static void appendWorld(StringWrapper s){
10    s.value = s.value +" World";
11}
12
13public static void main(String[] args) {
14    StringWrapper s = new StringWrapper("Hello");
15    appendWorld(s);
16    System.out.println(s.value);
17}
18
Full Screen
copy
1(Name)[Location] -> [Value at the Location]
2---------------------
3(Ref2Foo)[223]  -> 47
4(Foo)[47]       -> 5
5
Full Screen
copy
1using namespace std;
2#include <iostream>
3
4void change (char *&str){   // the '&' makes this a reference parameter
5    str = NULL;
6}
7
8int main()
9{
10    char *str = "not Null";
11    change(str);
12    cout<<"str is " << str;      // ==>str is <null>
13}
14
Full Screen
copy
1public class ValueDemo{
2
3    public void change (String str){
4        str = null;
5    }
6
7     public static void main(String []args){
8        ValueDemo vd = new ValueDemo();
9        String str = "not null";
10        vd.change(str);
11        System.out.println("str is " + str);    // ==> str is not null!!
12                                                // Note that if "str" was
13                                                // passed-by-reference, it
14                                                // WOULD BE NULL after the
15                                                // call to change().
16     }
17}
18
Full Screen
copy
1program passByRefDemo;
2type 
3   iptr = ^integer;
4var
5   ptr: iptr;
6
7   procedure setToNil(var ptr : iptr);
8   begin
9       ptr := nil;
10   end;
11
12begin
13   new(ptr);
14   ptr^ := 10;
15   setToNil(ptr);
16   if (ptr = nil) then
17       writeln('ptr seems to be nil');     { ptr should be nil, so this line will run. }
18end.
19
Full Screen
copy
1public class PassByValue {
2    public static void main(String[] args) {
3        Test t = new Test();
4        t.name = "initialvalue";
5        new PassByValue().changeValue(t);
6        System.out.println(t.name);
7    }
8    
9    public void changeValue(Test f) {
10        f.name = "changevalue";
11    }
12}
13
14class Test {
15    String name;
16}
17
Full Screen
copy
1public class PassByValue {
2    public static void main(String[] args) {
3        Test t = new Test();
4        t.name = "initialvalue";
5        new PassByValue().changeRefence(t);
6        System.out.println(t.name);
7    }
8    
9    public void changeRefence(Test f) {
10        f = null;
11    }
12}
13
14class Test {
15    String name;
16}
17
Full Screen
copy
1public static void tricky(Point arg1, Point arg2) {
2  arg1.x = 100;
3  arg1.y = 100;
4  Point temp = arg1;
5  arg1 = arg2;
6  arg2 = temp;
7}
8public static void main(String [] args) {
9  Point pnt1 = new Point(0,0);
10  Point pnt2 = new Point(0,0);
11  System.out.println("X1: " + pnt1.x + " Y1: " +pnt1.y); 
12  System.out.println("X2: " + pnt2.x + " Y2: " +pnt2.y);
13  System.out.println(" ");
14  tricky(pnt1,pnt2);
15  System.out.println("X1: " + pnt1.x + " Y1:" + pnt1.y); 
16  System.out.println("X2: " + pnt2.x + " Y2: " +pnt2.y);  
17}
18
Full Screen
copy
1Point pnt1 = new Point(0,0);
2Point pnt2 = new Point(0,0);
3
Full Screen
copy
1System.out.println("X1: " + pnt1.x + " Y1: " +pnt1.y); 
2System.out.println("X2: " + pnt2.x + " Y2: " +pnt2.y);
3System.out.println(" ");
4
Full Screen
copy
1tricky(pnt1,pnt2);           public void tricky(Point arg1, Point arg2);
2
Full Screen
copy
1Point temp = arg1;
2arg1 = arg2;
3arg2 = temp;
4
Full Screen
copy
1X1: 0         Y1: 0
2X2: 0         Y2: 0
3X1: 100       Y1: 100
4X2: 0         Y2: 0
5
Full Screen
copy
1private static void foo(Object bar) {
2    bar = null;
3}
4
5public static void main(String[] args) {
6    String baz = "Hah!";
7    foo(baz);
8    System.out.println(baz);
9}
10
Full Screen
copy
1void cppMethod(int val, int &ref, Dog obj, Dog &objRef, Dog *objPtr, Dog *&objPtrRef)
2{
3    val = 7; // Modifies the copy
4    ref = 7; // Modifies the original variable
5    obj.SetName("obj"); // Modifies the copy of Dog passed
6    objRef.SetName("objRef"); // Modifies the original Dog passed
7    objPtr->SetName("objPtr"); // Modifies the original Dog pointed to 
8                               // by the copy of the pointer passed.
9    objPtr = new Dog("newObjPtr");  // Modifies the copy of the pointer, 
10                                   // leaving the original object alone.
11    objPtrRef->SetName("objRefPtr"); // Modifies the original Dog pointed to 
12                                    // by the original pointer passed. 
13    objPtrRef = new Dog("newObjPtrRef"); // Modifies the original pointer passed
14}
15
16int main()
17{
18    int a = 0;
19    int b = 0;
20    Dog d0 = Dog("d0");
21    Dog d1 = Dog("d1");
22    Dog *d2 = new Dog("d2");
23    Dog *d3 = new Dog("d3");
24    cppMethod(a, b, d0, d1, d2, d3);
25    // a is still set to 0
26    // b is now set to 7
27    // d0 still have name "d0"
28    // d1 now has name "objRef"
29    // d2 now has name "objPtr"
30    // d3 now has name "newObjPtrRef"
31}
32
Full Screen
copy
1public static void javaMethod(int val, Dog objPtr)
2{
3   val = 7; // Modifies the copy
4   objPtr.SetName("objPtr") // Modifies the original Dog pointed to 
5                            // by the copy of the pointer passed.
6   objPtr = new Dog("newObjPtr");  // Modifies the copy of the pointer, 
7                                  // leaving the original object alone.
8}
9
10public static void main()
11{
12    int a = 0;
13    Dog d0 = new Dog("d0");
14    javaMethod(a, d0);
15    // a is still set to 0
16    // d0 now has name "objPtr"
17}
18
Full Screen
copy
1int x = 3;
2float y = 101.1f;
3boolean amIAwesome = true;
4
Full Screen
copy
1int problems = 99;
2String name = "Jay-Z";
3
Full Screen
copy
1JButton[] marxBros = new JButton[3];
2marxBros[0] = new JButton("Groucho");
3marxBros[1] = new JButton("Zeppo");
4marxBros[2] = new JButton("Harpo");
5
Full Screen
copy
1private static void shout(String name){
2    System.out.println("There goes " + name + "!");
3}
4
5public static void main(String[] args){
6    String hisName = "John J. Jingleheimerschmitz";
7    String myName = hisName;
8    shout(myName);
9}
10
Full Screen
copy
11. Person person;
22. person = new Person("Tom");
33. changeName(person);
44.
55. //I didn't use Person person below as an argument to be nice
66. static void changeName(Person anotherReferenceToTheSamePersonObject) {
77.     anotherReferenceToTheSamePersonObject.setName("Jerry");
88. }
9
Full Screen
copy
1public class Main {
2
3     public static void main(String[] args) {
4          Foo f = new Foo("f");
5          changeReference(f); // It won't change the reference!
6          modifyReference(f); // It will modify the object that the reference variable "f" refers to!
7     }
8
9     public static void changeReference(Foo a) {
10          Foo b = new Foo("b");
11          a = b;
12     }
13
14     public static void modifyReference(Foo c) {
15          c.setAttribute("c");
16     }
17
18}
19
Full Screen
copy
1Dog myDog = new Dog("Rover");
2foo(myDog);
3
Full Screen
copy
1public void foo(Dog someDog) {
2    someDog.setName("Max");     // AAA
3    someDog = new Dog("Fifi");  // BBB
4    someDog.setName("Rowlf");   // CCC
5}
6
Full Screen
copy
1public static void main(String[] args) {
2    Dog aDog = new Dog("Max");
3    Dog oldDog = aDog;
4
5    // we pass the object to foo
6    foo(aDog);
7    // aDog variable is still pointing to the "Max" dog when foo(...) returns
8    aDog.getName().equals("Max"); // true
9    aDog.getName().equals("Fifi"); // false
10    aDog == oldDog; // true
11}
12
13public static void foo(Dog d) {
14    d.getName().equals("Max"); // true
15    // change d inside of foo() to point to a new Dog instance "Fifi"
16    d = new Dog("Fifi");
17    d.getName().equals("Fifi"); // true
18}
19
Full Screen
copy
1public static void main(String[] args) {
2    Dog aDog = new Dog("Max");
3    Dog oldDog = aDog;
4
5    foo(aDog);
6    // when foo(...) returns, the name of the dog has been changed to "Fifi"
7    aDog.getName().equals("Fifi"); // true
8    // but it is still the same dog:
9    aDog == oldDog; // true
10}
11
12public static void foo(Dog d) {
13    d.getName().equals("Max"); // true
14    // this changes the name of d to be "Fifi"
15    d.setName("Fifi");
16}
17
Full Screen
copy
1DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder();
2dtfb.append(DateTimeFormatter.ISO_LOCAL_DATE);
3dtfb.appendLiteral(' ');
4dtfb.append(DateTimeFormatter.ISO_LOCAL_TIME);
5DateTimeFormatter dtf = dtfb.toFormatter();
6ZoneId shanghai = ZoneId.of("Asia/Shanghai");
7
8String str3 = "1927-12-31 23:54:07";  
9String str4 = "1927-12-31 23:54:08";  
10
11ZonedDateTime zdt3 = LocalDateTime.parse(str3, dtf).atZone(shanghai);
12ZonedDateTime zdt4 = LocalDateTime.parse(str4, dtf).atZone(shanghai);
13
14Duration durationAtEarlierOffset = Duration.between(zdt3.withEarlierOffsetAtOverlap(), zdt4.withEarlierOffsetAtOverlap());
15
16Duration durationAtLaterOffset = Duration.between(zdt3.withLaterOffsetAtOverlap(), zdt4.withLaterOffsetAtOverlap());
17
Full Screen
copy
1// Both have offsets +08:05:52
2ZoneOffset zo3Earlier = zdt3.withEarlierOffsetAtOverlap().getOffset();
3ZoneOffset zo3Later = zdt3.withLaterOffsetAtOverlap().getOffset();
4
Full Screen
copy
1// +08:05:52
2ZoneOffset zo4Earlier = zdt4.withEarlierOffsetAtOverlap().getOffset();
3
4// +08:00
5ZoneOffset zo4Later = zdt4.withLaterOffsetAtOverlap().getOffset();
6
Full Screen
copy
1// Null
2ZoneOffsetTransition zot3 = shanghai.getRules().getTransition(ld3.toLocalDateTime);
3
4// An overlap transition
5ZoneOffsetTransition zot4 = shanghai.getRules().getTransition(ld3.toLocalDateTime);
6
Full Screen
copy
1long difference = (sDt4.getTime() - sDt3.getTime()) / 1000;
2System.out.println(difference);
3
Full Screen
copy
1import java.util.TimeZone;
2
3public class Test {
4    public static void main(String[] args) throws Exception {
5        long startOf1900Utc = -2208988800000L;
6        for (String id : TimeZone.getAvailableIDs()) {
7            TimeZone zone = TimeZone.getTimeZone(id);
8            if (zone.getRawOffset() != zone.getOffset(startOf1900Utc - 1)) {
9                System.out.println(id);
10            }
11        }
12    }
13}
14
Full Screen
copy
1public void test() {
2    MyClass obj = null;
3    init(obj);
4    //After calling init method, obj still points to null
5    //this is because obj is passed as value and not as reference.
6}
7private void init(MyClass objVar) {
8    objVar = new MyClass();
9}
10
Full Screen
copy
1public class Test
2{
3    public static void reverseArray(int[] array1)
4    {
5        // ...
6    }
7
8    public static void main(String[] args)
9    {
10        int[] array1 = { 1, 10, -7 };
11        int[] array2 = { 5, -190, 0 };
12
13        reverseArray(array1);
14    }
15}
16
Full Screen
copy
1public class Test
2{
3    public static int[] reverseArray(int[] array1)
4    {
5        int[] array2 = { -7, 0, -1 };
6
7        array1[0] = 5; // array a becomes 5, 10, -7
8
9        array1 = array2; /* array1 of reverseArray starts
10          pointing to c instead of a (not shown in image below) */
11        return array2;
12    }
13
14    public static void main(String[] args)
15    {
16        int[] array1 = { 1, 10, -7 };
17        int[] array2 = { 5, -190, 0 };
18
19        array1 = reverseArray(array1); /* array1 of 
20         main starts pointing to c instead of a */
21    }
22}
23
Full Screen
copy
1public class PassByCopy{
2    public static void changeName(Dog d){
3        d.name = "Fido";
4    }
5    public static void main(String[] args){
6        Dog d = new Dog("Maxx");
7        System.out.println("name= "+ d.name);
8        changeName(d);
9        System.out.println("name= "+ d.name);
10    }
11}
12class Dog{
13    public String name;
14    public Dog(String s){
15        this.name = s;
16    }
17}
18
Full Screen
copy
1public class Test {
2  public static void main(String[] args) {
3    Integer a = new Integer(2);
4    Integer b = new Integer(3);
5    System.out.println("Before: a = " + a + ", b = " + b);
6    swap(a,b);
7    System.out.println("After: a = " + a + ", b = " + b);
8  }
9
10  public static swap(Integer iA, Integer iB) {
11    Integer tmp = iA;
12    iA = iB;
13    iB = tmp;
14  }
15}
16
Full Screen
copy
1public void method (String param) {}
2...
3String variable = new String("ref");
4method(variable);
5method(variable.toString());
6method(new String("ref"));
7
Full Screen
copy
1void getValues(int& arg1, int& arg2) {
2    arg1 = 1;
3    arg2 = 2;
4}
5void caller() {
6    int x;
7    int y;
8    getValues(x, y);
9    cout << "Result: " << x << " " << y << endl;
10}
11
Full Screen
copy
1void getValues(int[] arg1, int[] arg2) {
2    arg1[0] = 1;
3    arg2[0] = 2;
4}
5void caller() {
6    int[] x = new int[1];
7    int[] y = new int[1];
8    getValues(x, y);
9    System.out.println("Result: " + x[0] + " " + y[0]);
10}
11
Full Screen
copy
1public class PassByValueString {
2    public static void main(String[] args) {
3        new PassByValueString().caller();
4    }
5
6    public void caller() {
7        String value = "Nikhil";
8        boolean valueflag = false;
9        String output = method(value, valueflag);
10        /*
11         * 'output' is insignificant in this example. we are more interested in
12         * 'value' and 'valueflag'
13         */
14        System.out.println("output : " + output);
15        System.out.println("value : " + value);
16        System.out.println("valueflag : " + valueflag);
17
18    }
19
20    public String method(String value, boolean valueflag) {
21        value = "Anand";
22        valueflag = true;
23        return "output";
24    }
25}
26
Full Screen
copy
1output : output
2value : Nikhil
3valueflag : false
4
Full Screen
copy
1public class PassByValueNewString {
2    public static void main(String[] args) {
3        new PassByValueNewString().caller();
4    }
5
6    public void caller() {
7        String value = new String("Nikhil");
8        boolean valueflag = false;
9        String output = method(value, valueflag);
10        /*
11         * 'output' is insignificant in this example. we are more interested in
12         * 'value' and 'valueflag'
13         */
14        System.out.println("output : " + output);
15        System.out.println("value : " + value);
16        System.out.println("valueflag : " + valueflag);
17
18    }
19
20    public String method(String value, boolean valueflag) {
21        value = "Anand";
22        valueflag = true;
23        return "output";
24    }
25}
26
Full Screen
copy
1output : output
2value : Nikhil
3valueflag : false
4
Full Screen
copy
1public class PassByValueObjectCase1 {
2
3    private class Student {
4        int id;
5        String name;
6        public Student() {
7        }
8        public Student(int id, String name) {
9            super();
10            this.id = id;
11            this.name = name;
12        }
13        public int getId() {
14            return id;
15        }
16        public void setId(int id) {
17            this.id = id;
18        }
19        public String getName() {
20            return name;
21        }
22        public void setName(String name) {
23            this.name = name;
24        }
25        @Override
26        public String toString() {
27            return "Student [id=" + id + ", name=" + name + "]";
28        }
29    }
30
31    public static void main(String[] args) {
32        new PassByValueObjectCase1().caller();
33    }
34
35    public void caller() {
36        Student student = new Student(10, "Nikhil");
37        String output = method(student);
38        /*
39         * 'output' is insignificant in this example. we are more interested in
40         * 'student'
41         */
42        System.out.println("output : " + output);
43        System.out.println("student : " + student);
44    }
45
46    public String method(Student student) {
47        student.setName("Anand");
48        return "output";
49    }
50}
51
Full Screen
copy
1output : output
2student : Student [id=10, name=Anand]
3
Full Screen
copy
1public class PassByValueObjectCase2 {
2
3    public static void main(String[] args) {
4        new PassByValueObjectCase2().caller();
5    }
6
7    public void caller() {
8        // student has the actual reference to a Student object created
9        // can we change this actual reference outside the local scope? Let's see
10        Student student = new Student(10, "Nikhil");
11        String output = method(student);
12        /*
13         * 'output' is insignificant in this example. we are more interested in
14         * 'student'
15         */
16        System.out.println("output : " + output);
17        System.out.println("student : " + student); // Will it print Nikhil or Anand?
18    }
19
20    public String method(Student student) {
21        student = new Student(20, "Anand");
22        return "output";
23    }
24
25}
26
Full Screen
copy
1output : output
2student : Student [id=10, name=Nikhil]
3
Full Screen
copy
1public static void swap(StringBuffer s1, StringBuffer s2) {
2    StringBuffer temp = s1;
3    s1 = s2;
4    s2 = temp;
5}
6
7
8public static void main(String[] args) {
9    StringBuffer s1 = new StringBuffer("Hello");
10    StringBuffer s2 = new StringBuffer("World");
11    swap(s1, s2);
12    System.out.println(s1);
13    System.out.println(s2);
14}
15
Full Screen
copy
1public static void appendWorld(StringBuffer s1) {
2    s1.append(" World");
3}
4
5public static void main(String[] args) {
6    StringBuffer s = new StringBuffer("Hello");
7    appendWorld(s);
8    System.out.println(s);
9}
10
Full Screen
copy
1public static void appendWorld(String s){
2    s = s+" World";
3}
4
5public static void main(String[] args) {
6    String s = new String("Hello");
7    appendWorld(s);
8    System.out.println(s);
9}
10
Full Screen
copy
1class StringWrapper {
2    public String value;
3
4    public StringWrapper(String value) {
5        this.value = value;
6    }
7}
8
9public static void appendWorld(StringWrapper s){
10    s.value = s.value +" World";
11}
12
13public static void main(String[] args) {
14    StringWrapper s = new StringWrapper("Hello");
15    appendWorld(s);
16    System.out.println(s.value);
17}
18
Full Screen
copy
1(Name)[Location] -> [Value at the Location]
2---------------------
3(Ref2Foo)[223]  -> 47
4(Foo)[47]       -> 5
5
Full Screen
copy
1using namespace std;
2#include <iostream>
3
4void change (char *&str){   // the '&' makes this a reference parameter
5    str = NULL;
6}
7
8int main()
9{
10    char *str = "not Null";
11    change(str);
12    cout<<"str is " << str;      // ==>str is <null>
13}
14
Full Screen
copy
1public class ValueDemo{
2
3    public void change (String str){
4        str = null;
5    }
6
7     public static void main(String []args){
8        ValueDemo vd = new ValueDemo();
9        String str = "not null";
10        vd.change(str);
11        System.out.println("str is " + str);    // ==> str is not null!!
12                                                // Note that if "str" was
13                                                // passed-by-reference, it
14                                                // WOULD BE NULL after the
15                                                // call to change().
16     }
17}
18
Full Screen
copy
1program passByRefDemo;
2type 
3   iptr = ^integer;
4var
5   ptr: iptr;
6
7   procedure setToNil(var ptr : iptr);
8   begin
9       ptr := nil;
10   end;
11
12begin
13   new(ptr);
14   ptr^ := 10;
15   setToNil(ptr);
16   if (ptr = nil) then
17       writeln('ptr seems to be nil');     { ptr should be nil, so this line will run. }
18end.
19
Full Screen
copy
1public class PassByValue {
2    public static void main(String[] args) {
3        Test t = new Test();
4        t.name = "initialvalue";
5        new PassByValue().changeValue(t);
6        System.out.println(t.name);
7    }
8    
9    public void changeValue(Test f) {
10        f.name = "changevalue";
11    }
12}
13
14class Test {
15    String name;
16}
17
Full Screen
copy
1public class PassByValue {
2    public static void main(String[] args) {
3        Test t = new Test();
4        t.name = "initialvalue";
5        new PassByValue().changeRefence(t);
6        System.out.println(t.name);
7    }
8    
9    public void changeRefence(Test f) {
10        f = null;
11    }
12}
13
14class Test {
15    String name;
16}
17
Full Screen
copy
1public static void tricky(Point arg1, Point arg2) {
2  arg1.x = 100;
3  arg1.y = 100;
4  Point temp = arg1;
5  arg1 = arg2;
6  arg2 = temp;
7}
8public static void main(String [] args) {
9  Point pnt1 = new Point(0,0);
10  Point pnt2 = new Point(0,0);
11  System.out.println("X1: " + pnt1.x + " Y1: " +pnt1.y); 
12  System.out.println("X2: " + pnt2.x + " Y2: " +pnt2.y);
13  System.out.println(" ");
14  tricky(pnt1,pnt2);
15  System.out.println("X1: " + pnt1.x + " Y1:" + pnt1.y); 
16  System.out.println("X2: " + pnt2.x + " Y2: " +pnt2.y);  
17}
18
Full Screen
copy
1Point pnt1 = new Point(0,0);
2Point pnt2 = new Point(0,0);
3
Full Screen
copy
1System.out.println("X1: " + pnt1.x + " Y1: " +pnt1.y); 
2System.out.println("X2: " + pnt2.x + " Y2: " +pnt2.y);
3System.out.println(" ");
4
Full Screen
copy
1tricky(pnt1,pnt2);           public void tricky(Point arg1, Point arg2);
2
Full Screen
copy
1Point temp = arg1;
2arg1 = arg2;
3arg2 = temp;
4
Full Screen
copy
1X1: 0         Y1: 0
2X2: 0         Y2: 0
3X1: 100       Y1: 100
4X2: 0         Y2: 0
5
Full Screen
copy
1private static void foo(Object bar) {
2    bar = null;
3}
4
5public static void main(String[] args) {
6    String baz = "Hah!";
7    foo(baz);
8    System.out.println(baz);
9}
10
Full Screen
copy
1void cppMethod(int val, int &ref, Dog obj, Dog &objRef, Dog *objPtr, Dog *&objPtrRef)
2{
3    val = 7; // Modifies the copy
4    ref = 7; // Modifies the original variable
5    obj.SetName("obj"); // Modifies the copy of Dog passed
6    objRef.SetName("objRef"); // Modifies the original Dog passed
7    objPtr->SetName("objPtr"); // Modifies the original Dog pointed to 
8                               // by the copy of the pointer passed.
9    objPtr = new Dog("newObjPtr");  // Modifies the copy of the pointer, 
10                                   // leaving the original object alone.
11    objPtrRef->SetName("objRefPtr"); // Modifies the original Dog pointed to 
12                                    // by the original pointer passed. 
13    objPtrRef = new Dog("newObjPtrRef"); // Modifies the original pointer passed
14}
15
16int main()
17{
18    int a = 0;
19    int b = 0;
20    Dog d0 = Dog("d0");
21    Dog d1 = Dog("d1");
22    Dog *d2 = new Dog("d2");
23    Dog *d3 = new Dog("d3");
24    cppMethod(a, b, d0, d1, d2, d3);
25    // a is still set to 0
26    // b is now set to 7
27    // d0 still have name "d0"
28    // d1 now has name "objRef"
29    // d2 now has name "objPtr"
30    // d3 now has name "newObjPtrRef"
31}
32
Full Screen
copy
1public static void javaMethod(int val, Dog objPtr)
2{
3   val = 7; // Modifies the copy
4   objPtr.SetName("objPtr") // Modifies the original Dog pointed to 
5                            // by the copy of the pointer passed.
6   objPtr = new Dog("newObjPtr");  // Modifies the copy of the pointer, 
7                                  // leaving the original object alone.
8}
9
10public static void main()
11{
12    int a = 0;
13    Dog d0 = new Dog("d0");
14    javaMethod(a, d0);
15    // a is still set to 0
16    // d0 now has name "objPtr"
17}
18
Full Screen
copy
1int x = 3;
2float y = 101.1f;
3boolean amIAwesome = true;
4
Full Screen
copy
1int problems = 99;
2String name = "Jay-Z";
3
Full Screen
copy
1JButton[] marxBros = new JButton[3];
2marxBros[0] = new JButton("Groucho");
3marxBros[1] = new JButton("Zeppo");
4marxBros[2] = new JButton("Harpo");
5
Full Screen
copy
1private static void shout(String name){
2    System.out.println("There goes " + name + "!");
3}
4
5public static void main(String[] args){
6    String hisName = "John J. Jingleheimerschmitz";
7    String myName = hisName;
8    shout(myName);
9}
10
Full Screen
copy
11. Person person;
22. person = new Person("Tom");
33. changeName(person);
44.
55. //I didn't use Person person below as an argument to be nice
66. static void changeName(Person anotherReferenceToTheSamePersonObject) {
77.     anotherReferenceToTheSamePersonObject.setName("Jerry");
88. }
9
Full Screen
copy
1public class Main {
2
3     public static void main(String[] args) {
4          Foo f = new Foo("f");
5          changeReference(f); // It won't change the reference!
6          modifyReference(f); // It will modify the object that the reference variable "f" refers to!
7     }
8
9     public static void changeReference(Foo a) {
10          Foo b = new Foo("b");
11          a = b;
12     }
13
14     public static void modifyReference(Foo c) {
15          c.setAttribute("c");
16     }
17
18}
19
Full Screen
copy
1Dog myDog = new Dog("Rover");
2foo(myDog);
3
Full Screen
copy
1public void foo(Dog someDog) {
2    someDog.setName("Max");     // AAA
3    someDog = new Dog("Fifi");  // BBB
4    someDog.setName("Rowlf");   // CCC
5}
6
Full Screen
copy
1public static void main(String[] args) {
2    Dog aDog = new Dog("Max");
3    Dog oldDog = aDog;
4
5    // we pass the object to foo
6    foo(aDog);
7    // aDog variable is still pointing to the "Max" dog when foo(...) returns
8    aDog.getName().equals("Max"); // true
9    aDog.getName().equals("Fifi"); // false
10    aDog == oldDog; // true
11}
12
13public static void foo(Dog d) {
14    d.getName().equals("Max"); // true
15    // change d inside of foo() to point to a new Dog instance "Fifi"
16    d = new Dog("Fifi");
17    d.getName().equals("Fifi"); // true
18}
19
Full Screen
copy
1public static void main(String[] args) {
2    Dog aDog = new Dog("Max");
3    Dog oldDog = aDog;
4
5    foo(aDog);
6    // when foo(...) returns, the name of the dog has been changed to "Fifi"
7    aDog.getName().equals("Fifi"); // true
8    // but it is still the same dog:
9    aDog == oldDog; // true
10}
11
12public static void foo(Dog d) {
13    d.getName().equals("Max"); // true
14    // this changes the name of d to be "Fifi"
15    d.setName("Fifi");
16}
17
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)