How to use Point class of org.openqa.selenium package

Best Selenium code snippet using org.openqa.selenium.Point

Run Selenium automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * See the NOTICE file distributed with this work for additional
5 * information regarding copyright ownership.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.mengge.events;
18
19
20import com.google.common.collect.ImmutableList;
21
22import com.mengge.events.api.Listener;
23import org.aspectj.lang.JoinPoint;
24import org.aspectj.lang.ProceedingJoinPoint;
25import org.aspectj.lang.annotation.After;
26import org.aspectj.lang.annotation.Around;
27import org.aspectj.lang.annotation.Aspect;
28import org.aspectj.lang.annotation.Before;
29import org.openqa.selenium.Alert;
30import org.openqa.selenium.By;
31import org.openqa.selenium.ContextAware;
32import org.openqa.selenium.Dimension;
33import org.openqa.selenium.Point;
34import org.openqa.selenium.ScreenOrientation;
35import org.openqa.selenium.WebDriver;
36import org.openqa.selenium.WebElement;
37import org.openqa.selenium.security.Credentials;
38import org.springframework.context.support.AbstractApplicationContext;
39
40import java.lang.reflect.InvocationTargetException;
41import java.util.ArrayList;
42import java.util.Collection;
43import java.util.List;
44
45@Aspect
46class DefaultAspect {
47
48    private static final List<Class<?>> listenable = ImmutableList.of(WebDriver.class,
49        WebElement.class, WebDriver.Navigation.class, WebDriver.TargetLocator.class,
50        ContextAware.class, Alert.class, WebDriver.Options.class, WebDriver.Window.class);
51
52    private static final String EXECUTION_NAVIGATION_TO = "execution(* org.openqa.selenium.WebDriver."
53        + "Navigation.get(..))  || "
54        + "execution(* org.openqa.selenium.WebDriver.Navigation.to(..)) || "
55        + "execution(* org.openqa.selenium.WebDriver.get(..))";
56    private static final String EXECUTION_NAVIGATION_BACK = "execution(* org.openqa.selenium.WebDriver."
57        + "Navigation.back(..))";
58    private static final String EXECUTION_NAVIGATION_FORWARD = "execution(* org.openqa.selenium.WebDriver."
59        + "Navigation.forward(..))";
60    private static final String EXECUTION_NAVIGATION_REFRESH = "execution(* org.openqa.selenium.WebDriver."
61        + "Navigation.refresh(..))";
62    private static final String EXECUTION_SEARCH = "execution(* org.openqa.selenium.SearchContext."
63        + "findElement(..)) || "
64        + "execution(* org.openqa.selenium.SearchContext.findElements(..))";
65    private static final String EXECUTION_CLICK = "execution(* org.openqa.selenium.WebElement.click(..))";
66    private static final String EXECUTION_CHANGE_VALUE = "execution(* org.openqa.selenium.WebElement."
67        + "sendKeys(..)) || "
68        + "execution(* org.openqa.selenium.WebElement.clear(..))  || "
69        + "execution(* AndroidElement.replaceValue(..))  || "
70        + "execution(* MobileElement.setValue(..))";
71    private static final String EXECUTION_SCRIPT = "execution(* org.openqa.selenium.JavascriptExecutor."
72        + "executeScript(..)) || "
73        + "execution(* org.openqa.selenium.JavascriptExecutor.executeAsyncScript(..))";
74    private static final String EXECUTION_ALERT_ACCEPT = "execution(* org.openqa.selenium.Alert."
75        + "accept(..))";
76    private static final String EXECUTION_ALERT_DISMISS = "execution(* org.openqa.selenium.Alert."
77        + "dismiss(..))";
78    private static final String EXECUTION_ALERT_SEND_KEYS = "execution(* org.openqa.selenium.Alert."
79        + "sendKeys(..))";
80    private static final String EXECUTION_ALERT_AUTHENTICATION = "execution(* org.openqa.selenium."
81        + "Alert.setCredentials(..)) || "
82        + "execution(* org.openqa.selenium.Alert.authenticateUsing(..))";
83    private static final String EXECUTION_WINDOW_SET_SIZE = "execution(* org.openqa.selenium."
84        + "WebDriver.Window.setSize(..))";
85    private static final String EXECUTION_WINDOW_SET_POSITION = "execution(* org.openqa.selenium.WebDriver."
86        + "Window.setPosition(..))";
87    private static final String EXECUTION_WINDOW_MAXIMIZE = "execution(* org.openqa.selenium.WebDriver."
88        + "Window.maximize(..))";
89    private static final String EXECUTION_ROTATE = "execution(* org.openqa.selenium.Rotatable"
90        + ".rotate(..))";
91    private static final String EXECUTION_CONTEXT = "execution(* org.openqa.selenium.ContextAware."
92        + "context(..))";
93    private static final String AROUND = "execution(* org.openqa.selenium.WebDriver.*(..)) || "
94        + "execution(* org.openqa.selenium.WebElement.*(..)) || "
95        + "execution(* org.openqa.selenium.WebDriver.Navigation.*(..)) || "
96        + "execution(* org.openqa.selenium.WebDriver.Options.*(..)) || "
97        + "execution(* org.openqa.selenium.WebDriver.TargetLocator.*(..)) || "
98        + "execution(* org.openqa.selenium.WebDriver.TargetLocator.*(..)) || "
99        + "execution(* org.openqa.selenium.JavascriptExecutor.*(..)) || "
100        + "execution(* org.openqa.selenium.ContextAware.*(..)) || "
101        + "execution(* FindsByAccessibilityId.*(..)) || "
102        + "execution(* FindsByAndroidUIAutomator.*(..)) || "
103        + "execution(* FindsByIosUIAutomation.*(..)) || "
104        + "execution(* org.openqa.selenium.internal.FindsByClassName.*(..)) || "
105        + "execution(* org.openqa.selenium.internal.FindsByCssSelector.*(..)) || "
106        + "execution(* org.openqa.selenium.internal.FindsById.*(..)) || "
107        + "execution(* org.openqa.selenium.internal.FindsByLinkText.*(..)) || "
108        + "execution(* org.openqa.selenium.internal.FindsByName.*(..)) || "
109        + "execution(* org.openqa.selenium.internal.FindsByTagName.*(..)) || "
110        + "execution(* org.openqa.selenium.internal.FindsByXPath.*(..)) || "
111        + "execution(* org.openqa.selenium.WebDriver.Window.*(..)) || "
112        + "execution(* AndroidElement.*(..)) || "
113        + "execution(* IOSElement.*(..)) || "
114        + "execution(* AndroidDriver.*(..)) || "
115        + "execution(* IOSDriver.*(..)) || "
116        + "execution(* AppiumDriver.*(..)) || "
117        + "execution(* MobileElement.*(..)) || "
118        + "execution(* org.openqa.selenium.remote.RemoteWebDriver.*(..)) || "
119        + "execution(* org.openqa.selenium.remote.RemoteWebElement.*(..)) || "
120        + "execution(* org.openqa.selenium.Alert.*(..))";
121
122    private final AbstractApplicationContext context;
123    private final WebDriver driver;
124    private final DefaultListener listener = new DefaultListener();
125
126    private static Throwable getRootCause(Throwable thrown) {
127        Class<? extends Throwable> throwableClass = thrown.getClass();
128
129        if (!InvocationTargetException.class.equals(throwableClass) && !RuntimeException.class.equals(throwableClass)) {
130            return thrown;
131        }
132        if (thrown.getCause() != null) {
133            return getRootCause(thrown.getCause());
134        }
135        return thrown;
136    }
137
138    private static Class<?> getClassForProxy(Class<?> classOfObject) {
139        Class<?> returnStatement = null;
140        for (Class<?> c : listenable) {
141            if (!c.isAssignableFrom(classOfObject)) {
142                continue;
143            }
144            returnStatement =  c;
145        }
146        return returnStatement;
147    }
148
149    DefaultAspect(AbstractApplicationContext context, WebDriver driver) {
150        this.context = context;
151        this.driver = driver;
152    }
153
154    private Object transformToListenable(Object toBeTransformed) {
155        if (toBeTransformed == null) {
156            return null;
157        }
158
159        Object result = null;
160        if (getClassForProxy(toBeTransformed.getClass()) != null) {
161            result = context.getBean(DefaultBeanConfiguration.COMPONENT_BEAN, toBeTransformed);
162        }
163        return result;
164    }
165
166    private List<Object> returnProxyList(List<Object> originalList) throws Exception {
167        try {
168            List<Object> proxyList = new ArrayList<>();
169            for (Object o : originalList) {
170                if (getClassForProxy(o.getClass()) == null) {
171                    proxyList.add(o);
172                } else {
173                    proxyList.add(context.getBean(DefaultBeanConfiguration.COMPONENT_BEAN, o));
174                }
175            }
176            return proxyList;
177        } catch (Exception e) {
178            throw e;
179        }
180
181    }
182
183    public void add(Collection<Listener> listeners) {
184        listener.add(listeners);
185    }
186
187    @Before(EXECUTION_NAVIGATION_TO)
188    public void beforeNavigateTo(JoinPoint joinPoint) throws Throwable {
189        try {
190            listener.beforeNavigateTo(String.valueOf(joinPoint.getArgs()[0]), driver);
191        } catch (Throwable t) {
192            throw getRootCause(t);
193        }
194    }
195
196    @After(EXECUTION_NAVIGATION_TO)
197    public void afterNavigateTo(JoinPoint joinPoint)  throws Throwable {
198        try {
199            listener.afterNavigateTo(String.valueOf(joinPoint.getArgs()[0]), driver);
200        } catch (Throwable t) {
201            throw getRootCause(t);
202        }
203    }
204
205    @Before(EXECUTION_NAVIGATION_BACK)
206    public void beforeNavigateBack(JoinPoint joinPoint) throws Throwable {
207        try {
208            listener.beforeNavigateBack(driver);
209        } catch (Throwable t) {
210            throw getRootCause(t);
211        }
212    }
213
214    @After(EXECUTION_NAVIGATION_BACK)
215    public void afterNavigateBack(JoinPoint joinPoint) throws Throwable {
216        try {
217            listener.afterNavigateBack(driver);
218        } catch (Throwable t) {
219            throw getRootCause(t);
220        }
221    }
222
223    @Before(EXECUTION_NAVIGATION_FORWARD)
224    public void beforeNavigateForward(JoinPoint joinPoint)  throws Throwable {
225        try {
226            listener.beforeNavigateForward(driver);
227        } catch (Throwable t) {
228            throw getRootCause(t);
229        }
230    }
231
232    @After(EXECUTION_NAVIGATION_FORWARD)
233    public void afterNavigateForward(JoinPoint joinPoint) throws Throwable {
234        try {
235            listener.afterNavigateForward(driver);
236        } catch (Throwable t) {
237            throw getRootCause(t);
238        }
239    }
240
241    @Before(EXECUTION_NAVIGATION_REFRESH)
242    public void beforeNavigateRefresh(JoinPoint joinPoint)  throws Throwable {
243        try {
244            listener.beforeNavigateRefresh(driver);
245        } catch (Throwable t) {
246            throw getRootCause(t);
247        }
248    }
249
250    @After(EXECUTION_NAVIGATION_REFRESH)
251    public void afterNavigateRefresh(JoinPoint joinPoint) throws Throwable {
252        try {
253            listener.afterNavigateRefresh(driver);
254        } catch (Throwable t) {
255            throw getRootCause(t);
256        }
257    }
258
259    @SuppressWarnings("unchecked")
260    private <T extends Object> T castArgument(JoinPoint joinPoint, int argIndex) {
261        return (T) joinPoint.getArgs()[argIndex];
262    }
263
264    @SuppressWarnings("unchecked")
265    private <T extends Object> T castTarget(JoinPoint joinPoint) {
266        return (T) joinPoint.getTarget();
267    }
268
269    @Before(EXECUTION_SEARCH)
270    public void beforeFindBy(JoinPoint joinPoint) throws Throwable {
271        try {
272            Object target =  joinPoint.getTarget();
273            if (!WebElement.class.isAssignableFrom(target.getClass())) {
274                listener.beforeFindBy((By) castArgument(joinPoint, 0), null, driver);
275            } else {
276                listener.beforeFindBy((By) castArgument(joinPoint, 0),
277                    (WebElement) castTarget(joinPoint), driver);
278            }
279        } catch (Throwable t) {
280            throw getRootCause(t);
281        }
282    }
283
284    @After(EXECUTION_SEARCH)
285    public void afterFindBy(JoinPoint joinPoint) throws Throwable {
286        try {
287            Object target =  joinPoint.getTarget();
288            if (!WebElement.class.isAssignableFrom(target.getClass())) {
289                listener.afterFindBy((By) castArgument(joinPoint, 0), null, driver);
290            } else {
291                listener.afterFindBy((By) castArgument(joinPoint, 0),
292                    (WebElement) castTarget(joinPoint), driver);
293            }
294        } catch (Throwable t) {
295            throw getRootCause(t);
296        }
297    }
298
299    @Before(EXECUTION_CLICK)
300    public void beforeClickOn(JoinPoint joinPoint) throws Throwable {
301        try {
302            listener.beforeClickOn((WebElement) castTarget(joinPoint), driver);
303        } catch (Throwable t) {
304            throw getRootCause(t);
305        }
306    }
307
308    @After(EXECUTION_CLICK)
309    public void afterClickOn(JoinPoint joinPoint) throws Throwable {
310        try {
311            listener.afterClickOn((WebElement) castTarget(joinPoint), driver);
312        } catch (Throwable t) {
313            throw getRootCause(t);
314        }
315    }
316
317    @Before(EXECUTION_CHANGE_VALUE)
318    public void beforeChangeValueOf(JoinPoint joinPoint) throws Throwable {
319        try {
320            listener.beforeChangeValueOf((WebElement) castTarget(joinPoint), driver);
321        } catch (Throwable t) {
322            throw getRootCause(t);
323        }
324    }
325
326    @After(EXECUTION_CHANGE_VALUE)
327    public void afterChangeValueOf(JoinPoint joinPoint) throws Throwable {
328        try {
329            listener.afterChangeValueOf((WebElement) castTarget(joinPoint), driver);
330        } catch (Throwable t) {
331            throw getRootCause(t);
332        }
333    }
334
335    @Before(EXECUTION_SCRIPT)
336    public void beforeScript(JoinPoint joinPoint) throws Throwable {
337        try {
338            listener.beforeScript(String.valueOf(joinPoint.getArgs()[0]), driver);
339        } catch (Throwable t) {
340            throw getRootCause(t);
341        }
342    }
343
344    @After(EXECUTION_SCRIPT)
345    public void afterScript(JoinPoint joinPoint) throws Throwable {
346        try {
347            listener.afterScript(String.valueOf(joinPoint.getArgs()[0]), driver);
348        } catch (Throwable t) {
349            throw getRootCause(t);
350        }
351    }
352
353    @Before(EXECUTION_ALERT_ACCEPT)
354    public void beforeAlertAccept(JoinPoint joinPoint) throws Throwable {
355        try {
356            listener.beforeAlertAccept(driver, (Alert) castTarget(joinPoint));
357        } catch (Throwable t) {
358            throw getRootCause(t);
359        }
360    }
361
362    @After(EXECUTION_ALERT_ACCEPT)
363    public void afterAlertAccept(JoinPoint joinPoint) throws Throwable {
364        try {
365            listener.afterAlertAccept(driver, (Alert) castTarget(joinPoint));
366        } catch (Throwable t) {
367            throw getRootCause(t);
368        }
369    }
370
371    @Before(EXECUTION_ALERT_DISMISS)
372    public void beforeAlertDismiss(JoinPoint joinPoint) throws Throwable {
373        try {
374            listener.beforeAlertDismiss(driver, (Alert) castTarget(joinPoint));
375        } catch (Throwable t) {
376            throw getRootCause(t);
377        }
378    }
379
380    @After(EXECUTION_ALERT_DISMISS)
381    public void afterAlertDismiss(JoinPoint joinPoint) throws Throwable {
382        try {
383            listener.afterAlertDismiss(driver, (Alert) castTarget(joinPoint));
384        } catch (Throwable t) {
385            throw getRootCause(t);
386        }
387    }
388
389    @Before(EXECUTION_ALERT_SEND_KEYS)
390    public void beforeAlertSendKeys(JoinPoint joinPoint) throws Throwable {
391        try {
392            listener.beforeAlertSendKeys(driver, (Alert) castTarget(joinPoint),
393                String.valueOf(joinPoint.getArgs()[0]));
394        } catch (Throwable t) {
395            throw getRootCause(t);
396        }
397    }
398
399    @After(EXECUTION_ALERT_SEND_KEYS)
400    public void afterAlertSendKeys(JoinPoint joinPoint) throws Throwable {
401        try {
402            listener.afterAlertSendKeys(driver, (Alert) castTarget(joinPoint),
403                String.valueOf(joinPoint.getArgs()[0]));
404        } catch (Throwable t) {
405            throw getRootCause(t);
406        }
407    }
408
409    @Before(EXECUTION_ALERT_AUTHENTICATION)
410    public void beforeAlertAuthentication(JoinPoint joinPoint) throws Throwable {
411        try {
412            listener.beforeAuthentication(driver,
413                (Alert) castTarget(joinPoint), (Credentials) castArgument(joinPoint, 0));
414        } catch (Throwable t) {
415            throw getRootCause(t);
416        }
417    }
418
419    @After(EXECUTION_ALERT_AUTHENTICATION)
420    public void afterAlertAuthentication(JoinPoint joinPoint) throws Throwable {
421        try {
422            listener.afterAuthentication(driver, (Alert) castTarget(joinPoint),
423                (Credentials) castArgument(joinPoint, 0));
424        } catch (Throwable t) {
425            throw getRootCause(t);
426        }
427    }
428
429    @Before(EXECUTION_WINDOW_SET_SIZE)
430    public void beforeWindowIsResized(JoinPoint joinPoint) throws Throwable {
431        try {
432            listener.beforeWindowChangeSize(driver,
433                (WebDriver.Window) castTarget(joinPoint), (Dimension) castArgument(joinPoint, 0));
434        } catch (Throwable t) {
435            throw getRootCause(t);
436        }
437    }
438
439    @After(EXECUTION_WINDOW_SET_SIZE)
440    public void afterWindowIsResized(JoinPoint joinPoint) throws Throwable {
441        try {
442            listener.afterWindowChangeSize(driver, (WebDriver.Window) castTarget(joinPoint),
443                (Dimension) castArgument(joinPoint, 0));
444        } catch (Throwable t) {
445            throw getRootCause(t);
446        }
447    }
448
449    @Before(EXECUTION_WINDOW_SET_POSITION)
450    public void beforeWindowIsMoved(JoinPoint joinPoint) throws Throwable {
451        try {
452            listener.beforeWindowIsMoved(driver, (WebDriver.Window) castTarget(joinPoint),
453                (Point) castArgument(joinPoint, 0));
454        } catch (Throwable t) {
455            throw getRootCause(t);
456        }
457    }
458
459    @After(EXECUTION_WINDOW_SET_POSITION)
460    public void afterWindowIsMoved(JoinPoint joinPoint) throws Throwable {
461        try {
462            listener.afterWindowIsMoved(driver, (WebDriver.Window) castTarget(joinPoint),
463                (Point) castArgument(joinPoint, 0));
464        } catch (Throwable t) {
465            throw getRootCause(t);
466        }
467    }
468
469    @Before(EXECUTION_WINDOW_MAXIMIZE)
470    public void beforeMaximization(JoinPoint joinPoint) throws Throwable {
471        try {
472            listener.beforeWindowIsMaximized(driver, (WebDriver.Window) castTarget(joinPoint));
473        } catch (Throwable t) {
474            throw getRootCause(t);
475        }
476    }
477
478    @After(EXECUTION_WINDOW_MAXIMIZE)
479    public void afterMaximization(JoinPoint joinPoint) throws Throwable {
480        try {
481            listener.afterWindowIsMaximized(driver, (WebDriver.Window) castTarget(joinPoint));
482        } catch (Throwable t) {
483            throw getRootCause(t);
484        }
485    }
486
487    @Before(EXECUTION_ROTATE)
488    public void beforeRotation(JoinPoint joinPoint) throws Throwable {
489        try {
490            listener.beforeRotation(driver, (ScreenOrientation) castArgument(joinPoint, 0));
491        } catch (Throwable t) {
492            throw getRootCause(t);
493        }
494
495    }
496
497    @After(EXECUTION_ROTATE)
498    public void afterRotation(JoinPoint joinPoint) throws Throwable {
499        try {
500            listener.afterRotation(driver, (ScreenOrientation) castArgument(joinPoint, 0));
501        } catch (Throwable t) {
502            throw getRootCause(t);
503        }
504    }
505
506    @Before(EXECUTION_CONTEXT)
507    public void beforeSwitchingToContext(JoinPoint joinPoint) throws Throwable {
508        try {
509            listener.beforeSwitchingToContext(driver, String.valueOf(joinPoint.getArgs()[0]));
510        } catch (Throwable t) {
511            throw getRootCause(t);
512        }
513
514    }
515
516    @After(EXECUTION_CONTEXT)
517    public void afterSwitchingToContextn(JoinPoint joinPoint) throws Throwable {
518        try {
519            listener.afterSwitchingToContext(driver, String.valueOf(joinPoint.getArgs()[0]));
520        } catch (Throwable t) {
521            throw getRootCause(t);
522        }
523    }
524
525    @Around(AROUND)
526    public Object doAround(ProceedingJoinPoint point) throws Throwable {
527        Throwable t = null;
528        Object result = null;
529        try {
530            result = point.proceed();
531        } catch (Throwable e) {
532            t = e;
533        }
534        if (t != null) {
535            Throwable rootCause = getRootCause(t);
536            listener.onException(rootCause, driver);
537            throw rootCause;
538        }
539
540        if (result == null) { // maybe it was "void"
541            return result;
542        }
543        if (List.class.isAssignableFrom(result.getClass())) {
544            return returnProxyList((List<Object>) result);
545        }
546
547        return transformToListenable(result);
548    }
549}
550
Full Screen
copy
1package Test;
2
3import java.awt.Image;
4import java.awt.MouseInfo;
5import java.awt.Robot;
6import java.awt.Toolkit;
7import java.awt.datatransfer.Clipboard;
8import java.awt.datatransfer.DataFlavor;
9import java.awt.datatransfer.Transferable;
10import java.awt.event.InputEvent;
11import java.util.Set;
12
13import org.openqa.selenium.By;
14import org.openqa.selenium.Keys;
15import org.openqa.selenium.Point;
16import org.openqa.selenium.WebDriver;
17import org.openqa.selenium.WebElement;
18import org.openqa.selenium.chrome.ChromeDriver;
19import org.openqa.selenium.chrome.ChromeOptions;
20import org.openqa.selenium.interactions.Actions;
21import org.openqa.selenium.remote.DesiredCapabilities;
22import org.openqa.selenium.remote.RemoteWebDriver;
23
24public class TestOpenTab {
25	public static void main(String[] args) throws Exception {
26		String chromedriver = "C:\\SoftwareDevelopmentTest\\WorkPlaceMAVEN\\testMavenWeb\\webDriver\\chromedriver.exe";
27//		ChromeOptions options = new ChromeOptions();
28//		options.addArguments("--disable-popup-blocking");
29//		DesiredCapabilities capabilities = DesiredCapabilities.chrome();
30//		capabilities.setCapability(ChromeOptions.CAPABILITY, options);
31		
32		
33		System.setProperty("webdriver.chrome.driver", chromedriver);
34//		WebDriver driver = new ChromeDriver(capabilities);
35		WebDriver driver = new ChromeDriver();
36//		driver.get("http://www.w3school.com.cn/tiy/t.asp?f=html5_draganddrop");
37		driver.get("http://www.baidu.com");
38		driver.manage().window().maximize();
39//		driver.switchTo().frame("i");
40		WebElement e = driver.findElement(By.id("kw"));
41		e.sendKeys("av000000000000000000");
42		Thread.sleep(2000);
43//		WebElement e = driver.findElement(By.id("drag1"));
44		Point point = e.getLocation();
45		System.out.println(point);
46//		WebElement e2 = driver.findElement(By.id("div1"));
47//		Point point2 = e2.getLocation();
48//		System.out.println(point);		
49//		clidkAndMove(point,point2);
50		clidkAndMove(point);
51		
52//		String oleHandle = driver.getWindowHandle();
53//		 Actions action = new Actions(driver);
54////		 action.sendKeys(Keys.TAB);
55////		 action.keyDown(Keys.ALT).sendKeys(Keys.F4).keyUp(Keys.ALT).perform();
56//		 action.keyDown(Keys.CONTROL).sendKeys("t").perform();
57////		 action.keyDown(Keys.CONTROL).perform();
58////		 action.sendKeys("t").perform();
59////		 action.sendKeys(Keys.NULL);
60////		 Thread.sleep(10000);
61//		 action.release();
62////		 action.sendKeys(Keys.CONTROL, "t");
63////		openTab();
64//		Set<String> handles = driver.getWindowHandles();
65//		for (String handle : handles) {
66//			if (handle != oleHandle) {
67//				driver.switchTo().window(handle);
68//			}
69//		}
70//		driver.get("http://www.douyutv.com");
71//		driver.switchTo().window(oleHandle);
72//		driver.get("http://www.pandatv.cc");
73////		driver.findElement(By.id("panda_header_all_lives")).click();
74	}
75
76	/**
77	 * 
78	 * @Description: Control+t打开一个新的标签页
79	 */
80	public static void openTab() throws Exception {
81		// 构建 Robot 对象,用来操作键盘
82		Robot robot = new Robot();
83		robot.keyPress(java.awt.event.KeyEvent.VK_CONTROL);
84		robot.keyPress(java.awt.event.KeyEvent.VK_T);
85		// 释放键盘动作
86		robot.keyRelease(java.awt.event.KeyEvent.VK_CONTROL);
87		robot.keyRelease(java.awt.event.KeyEvent.VK_T);
88	}
89	
90	/**
91	 * 
92	 * @Description: 模拟鼠标左击按下右移
93	 */
94	public static void clidkAndMove(Point point) throws Exception {
95		// 构建 Robot 对象,用来操作键盘
96		Robot robot = new Robot();
97		robot.mouseMove(point.x, point.y+70);
98//		java.awt.Point point2 = MouseInfo.getPointerInfo().getLocation();
99//		System.out.println(point2);	
100		robot.delay(1000);
101		robot.mousePress(InputEvent.BUTTON1_MASK);
102		robot.delay(1000);
103		robot.mouseMove(point.x+200, point.y+70);//移动鼠标到(100,200)点
104	    robot.mouseRelease(InputEvent.BUTTON1_MASK);//释放左键
105	}
106}
107
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
1String[] phrases = new String[10];
2String keyPhrase = "Bird";
3for(String phrase : phrases) {
4    System.out.println(phrase.equals(keyPhrase));
5}
6
Full Screen
copy
1String[] phrases = new String[] {"The bird", "A bird", "My bird", "Bird"};
2String keyPhrase = "Bird";
3for(String phrase : phrases) {
4    System.out.println(phrase.equals(keyPhrase));
5}
6
Full Screen
copy
1public class Some {
2    private int id;
3    public int getId(){
4        return this.id;
5    }
6    public setId( int newId ) {
7        this.id = newId;
8    }
9}
10
Full Screen
copy
1Some reference = new Some();    // Point to a new object of type Some()
2Some otherReference = null;     // Initiallly this points to NULL
3
4reference.setId( 1 );           // Execute setId method, now private var id is 1
5
6System.out.println( reference.getId() ); // Prints 1 to the console
7
8otherReference = reference      // Now they both point to the only object.
9
10reference = null;               // "reference" now point to null.
11
12// But "otherReference" still point to the "real" object so this print 1 too...
13System.out.println( otherReference.getId() );
14
15// Guess what will happen
16System.out.println( reference.getId() ); // :S Throws NullPointerException because "reference" is pointing to NULL remember...
17
Full Screen
copy
1public class Student {
2
3    private int id;
4
5    public int getId() {
6        return this.id;
7    }
8
9    public setId(int newId) {
10        this.id = newId;
11    }
12}
13
Full Screen
copy
1public class School {
2
3    Student student;
4
5    public School() {
6        try {
7            student.getId();
8        }
9        catch(Exception e) {
10            System.out.println("Null pointer exception");
11        }
12    }
13}
14
Full Screen
copy
1public class School {
2
3    Student student;
4
5    public School() {
6        try {
7            student = new Student();
8            student.setId(12);
9            student.getId();
10        }
11        catch(Exception e) {
12            System.out.println("Null pointer exception");
13        }
14    }
15}
16
Full Screen
copy
1String a = null;
2System.out.println(a.toString()); // NullPointerException will be thrown
3
Full Screen
copy
1public class Test {
2    public static void main(String[] args) {
3        String foo = null;
4        int length = foo.length();   // HERE
5    }
6}
7
Full Screen
copy
1$ javac Test.java 
2$ java Test
3Exception in thread "main" java.lang.NullPointerException
4    at Test.main(Test.java:4)
5$
6
Full Screen
copy
1Exception in thread "main" java.lang.NullPointerException
2
Full Screen
copy
1int length = foo.length(); // HERE
2
Full Screen
copy
1public class Test {
2
3    private static String[] foo = new String[2];
4
5    private static int test(String[] bar, int pos) {
6        return bar[pos].length();
7    }
8
9    public static void main(String[] args) {
10        int length = test(foo, 1);
11    }
12}
13
14$ javac Test.java 
15$ java Test
16Exception in thread "main" java.lang.NullPointerException
17    at Test.test(Test.java:6)
18    at Test.main(Test.java:10)
19$ 
20
Full Screen
copy
1int length = test(foo, 1);
2    
3
copy
1private static String[] foo = new String[2];
2
Full Screen
copy
1public class Printer {
2    private String name;
3
4    public void setName(String name) {
5        this.name = name;
6    }
7
8    public void print() {
9        printString(name);
10    }
11
12    private void printString(String s) {
13        System.out.println(s + " (" + s.length() + ")");
14    }
15
16    public static void main(String[] args) {
17        Printer printer = new Printer();
18        printer.print();
19    }
20}
21
Full Screen
copy
1Exception in thread "main" java.lang.NullPointerException
2    at Printer.printString(Printer.java:13)
3    at Printer.print(Printer.java:9)
4    at Printer.main(Printer.java:19)
5
Full Screen
copy
1printString((name == null) ? "" : name);
2
Full Screen
copy
1public class Printer {
2    private final String name;
3
4    public Printer(String name) {
5        this.name = Objects.requireNonNull(name);
6    }
7
8    public void print() {
9        printString(name);
10    }
11
12    private void printString(String s) {
13        System.out.println(s + " (" + s.length() + ")");
14    }
15
16    public static void main(String[] args) {
17        Printer printer = new Printer("123");
18        printer.print();
19    }
20}
21
Full Screen
copy
1public class Example {
2
3    public static void main(String[] args) {
4        Object obj = null;
5        obj.hashCode();
6    }
7
8}
9
Full Screen
copy
1Integer num;
2num = new Integer(10);
3
Full Screen
copy
1public void doSomething(SomeObject obj) {
2   // Do something to obj, assumes obj is not null
3   obj.myMethod();
4}
5
Full Screen
copy
1// Throws an NPE with a custom error message if obj is null
2Objects.requireNonNull(obj, "obj must not be null");
3
Full Screen
copy
1/**
2  * @param obj An optional foo for ____. May be null, in which case
3  *  the result will be ____.
4  */
5public void doSomething(SomeObject obj) {
6    if(obj == null) {
7       // Do something
8    } else {
9       // Do something else
10    }
11}
12
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