Title
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
System.out.println("First binary?");
String binary = sc.nextLine();
System.out.println(binary);
System.out.println("Second binary?");
String binary2 = sc.nextLine();
System.out.println(binary2);
int num = Integer.parseInt(binary, 2);
int num2 = Integer.parseInt(binary2, 2);
int sum = num + num2;
System.out.println("Total: " + Integer.toBinaryString(sum));
Integer n = 5;
Integer n = 6;
System.out.println(n);
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
System.out.println("First binary?");
String binary = sc.nextLine();
System.out.println(binary);
for (int i = 0; i < binary.length(); i++) {
System.out.println(i);
System.out.println(binary.split)
}
public class IntByValue {
public static void changeInt(int n) {
System.out.println("In changeInt method");
System.out.println("\tBefore n += 10: n = " + n); // prints 5
n = n += 10;
System.out.println("\tAfter n += 10: n = " + n); // prints 10
}
public static void main(String[] args) {
int n = 5;
System.out.println("Main method before changeInt(n): n = " + n); // prints 5
changeInt(n);
System.out.println("Main method after changeInt(n): n = " + n); // still prints 5
}
}
IntByValue.main(null);
public class IntegerByValueOrReference {
public static void changeInteger(Integer n) {
System.out.println("In changeInteger method");
System.out.println("\tBefore change: n = " +
n + // prints 5
" hash code = " +
n.hashCode());
n += 10; // behind the scenes, this is: n = new Integer(n+10)
System.out.println("\tAfter change: n = " +
n + // prints 15
" hash code = " +
n.hashCode());
}
public static void main(String[] args) {
Integer n = 5;
System.out.println("Main method before changeInteger(n): n = " +
n + // prints 5
" hash code = " +
n.hashCode());
changeInteger(n);
System.out.println("Main method after changeInteger(n): n = " +
n + // now prints 15
" hash code = " +
n.hashCode());
}
}
IntegerByValueOrReference.main(null);
import java.util.concurrent.atomic.AtomicInteger;
public class PassByReference {
public static void changeAtomicInteger(AtomicInteger n) {
System.out.println("In changeAtomicInteger method");
System.out.println("\tBefore change: n = " +
n + // prints 5
" hash code = " +
n.hashCode());
n.set(n.get() + 10); // at this point, we are clearly working with reference data type
System.out.println("\tAfter change: n = " +
n + // prints 15
" hash code = " +
n.hashCode());
}
public static void main(String[] args) {
AtomicInteger n = new AtomicInteger(5); // unlike conventional wrapper class, this requires new
System.out.println("Main method before changeAtomicInteger(n): n = " +
n + // prints 5
" hash code = " +
n.hashCode());
changeAtomicInteger(n);
System.out.println("Main method after changeAtomicInteger(n): n = " +
n + // now prints 15
" hash code = " +
n.hashCode());
}
}
PassByReference.main(null);
public class IntByReference {
private int value;
public IntByReference(Integer value) {
this.value = value;
}
public String toString() {
return (String.format("%d", this.value));
}
public void swapToLowHighOrder(IntByReference i) {
if (this.value > i.value) {
int tmp = this.value;
this.value = i.value;
i.value = tmp;
}
}
public static void swapper(int n0, int n1) {
IntByReference a = new IntByReference(n0);
IntByReference b = new IntByReference(n1);
System.out.println("Before: " + a + " " + b);
a.swapToLowHighOrder(b); // conditionally build swap method to change values of a, b
System.out.println("After: " + a + " " + b);
System.out.println();
}
public static void main(String[] ags) {
IntByReference.swapper(21, 16);
IntByReference.swapper(16, 21);
IntByReference.swapper(16, -1);
}
}
IntByReference.main(null);