Data Types
import java.util.Scanner;
public class BinaryAdd {
public static void main(String[] args)
{
//Two variables
int b = 0, c = 0;
//This is to hold the output binary number
int[] add = new int[10];
int b1 = 1;
int b2 = 1;
//while the ints are not 0, code uses mod
//carry moves the 1+1 over and outputs 10
while (b1 != 0 || b2 != 0)
{
add[b++] = (int)((b1 % 2 + b2 % 2 + c) % 2);
c = (int)((b1 % 2 + b2 % 2 + c) / 2);
b1 = b1 / 2;
b2 = b2 / 2;
}
if (c != 0) {
add[b++] = c;
}
--b;
System.out.print("1 + 1 = ");
while (b >= 0) {
System.out.print(add[b--]);
}
}
}
BinaryAdd.main(null);
//pass by value
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);
//by value - wrapper class
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);
// int example
import java.util.Random;
public class IntEx {
public static void main(String[] args) {
// int example
int[] numbers = {1, 2, 3};
int product = numbers[0];
for (int i = 0; i < numbers.length; i++) {
product = product * numbers[i];
}
System.out.println("Product of numbers: " + product);
}
}
IntEx.main(null);
// double example
import java.util.Random;
public class DoubEx {
public static void main(String[] args) {
double pi = 3.141592653589793;
System.out.println("The value of pi: " + pi);
}
}
DoubEx.main(null);
// boolean example
import java.util.Random;
public class BoolEx {
public static void main(String[] args) {
int product = 11;
boolean isEven = (product % 2 == 0);
System.out.println("Is the product even? " + isEven);
}
}
BoolEx.main(null);
// char example
import java.util.Random;
public class CharEx {
public static void main(String[] args) {
String word = "compsci";
char firstLetter = word.charAt(0);
System.out.println("The first letter of the word " + word + " is " + firstLetter);
}
}
CharEx.main(null);
// Integer example
import java.util.Random;
public class IntWrap {
public static void main(String[] args) {
Integer[] numbers = {1, 2, 3};
Integer product = numbers[0];
for (int i = 0; i < numbers.length; i++) {
product = product * numbers[i];
}
System.out.println("Product of numbers: " + product);
}
}
IntWrap.main(null);
// Double example
import java.util.Random;
public class DoubEx {
public static void main(String[] args) {
Double pi = 3.141592653589793;
System.out.println("The value of pi: " + pi);
}
}
DoubEx.main(null);
// Boolean example
import java.util.Random;
public class BoolEx {
public static void main(String[] args) {
int product = 11;
Boolean isEven = (sum % 2 == 0);
System.out.println("Is the sum even? " + isEven);
}
}
BoolEx.main(null);
// Character example
import java.util.Random;
public class CharEx {
public static void main(String[] args) {
String word = "hello";
Character firstLetter = word.charAt(0);
System.out.println("The first letter of the word " + word + " is " + firstLetter);
}
}
CharEx.main(null);