0% found this document useful (0 votes)
14 views

Swapping Using Temporary or Third Variable: Java - Util.scanner

The document discusses different methods for swapping two integer variables in Java. It shows code samples that use a temporary variable, as well as a method that swaps the variables without using a temporary variable by adding and subtracting the two variables from each other. It also provides an example output showing the values before and after swapping. The document then discusses defining a custom method, using an enhanced for loop, and includes code samples and questions about method overloading and output.

Uploaded by

takne_007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Swapping Using Temporary or Third Variable: Java - Util.scanner

The document discusses different methods for swapping two integer variables in Java. It shows code samples that use a temporary variable, as well as a method that swaps the variables without using a temporary variable by adding and subtracting the two variables from each other. It also provides an example output showing the values before and after swapping. The document then discusses defining a custom method, using an enhanced for loop, and includes code samples and questions about method overloading and output.

Uploaded by

takne_007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Swapping using temporary or third variable

import java.util.Scanner;
class SwapNumbers
{
public static void main(String args[])
{
int x, y, temp;
System.out.println("Enter x and y");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
System.out.println("Before Swapping\nx = "+x+"\ny = "+y);
temp = x;
x = y;
y = temp;
System.out.println("After Swapping\nx = "+x+"\ny = "+y);
}

Swapping without temporary variable


import java.util.Scanner;
class SwapNumbers
{
public static void main(String args[])
{
int x, y;
System.out.println("Enter x and y");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
System.out.println("Before Swapping\nx = "+x+"\ny = "+y);
x = x + y;
y = x - y;
x = x - y;
System.out.println("After Swapping\nx = "+x+"\ny = "+y);
}

OUTPUT IS AS FOLLOWS.
Before Swapping
X=4
Y=5
After swapping : X = 5, Y = 4.

Defining our own method,

public class deepak{


static void myMethod(String name){
System.out.println("vegetable"+name);
}
public static void main(String[] args){
myMethod("brinjal");
}
}
Enhanced for loop:
int[ ] primes = {2, 3, 5, 7};
for (int t: primes) {
System.out.println(t);
}
ex:2
What is the output of this code?
int result = 0;
for (int i = 0; i < 5; i++) {
if (i == 3) {
result += 10;
} else {
result += i;
}
}
System.out.println(result);

class A {
private void print() {
System.out.println(''a'');
}

private void print(String str) {


System.out.println(''b'');
}
private void print(int x) {
System.out.println(''c'');
}
public static void main(String[ ] args) {
A object = new A();
object.print(12);
}
}

You might also like