/** * Description of the program, this program does lots of examples * * Hunter Lloyd */ public class Examples { Examples() { System.out.println("This is a program to show examples of primitive data types"); System.out.println("Plus some other things"); } /* * comment example */ public void start() { charExample(); booleanExample(); doubleExample(); stringEqualityExample(); additionExample(); finalExample(); } /* * Example for showing the difference between char, int and String */ private void charExample() { char letter = 'x'; String word = "char's have single quotes, STrings have double quotes"; System.out.println(word + " " + letter + '4'); //4 is a char not an int in this case } /* * boolean example, using a boolean in an if statement * also first example of using else */ private void booleanExample() { boolean x = true; if(x) { System.out.println("x was true"); } else { System.out.println("x was false"); } x = false; if(x) { System.out.println("x was true"); } else { System.out.println("x was false"); } } /* * difference between a double and an int * int's do not hold decimal values, truncates it off */ private void doubleExample() { int i = 2; double d = 2; System.out.println("i = " + i + " d = " + d); i = 2/3; d = 2/3; System.out.println("i = " + i + " d = " + d); double a = 2; double b = 3; // i = (int)a/b; d = a/b; System.out.println("i = " + i + " d = " + d); a = 7; //i = a/b; d = a/b; System.out.println("i = " + i + " d = " + d); } /* * == with a string is an error, you have to call the equals method */ private void stringEqualityExample() { //difference between assignment and equality int x = 8; int y = 7; System.out.println(x=y); System.out.println(x==y); String a = "hunter"; String b = "hunter"; System.out.println(a=b); System.out.println(a==b); System.out.println(a.equals(b)); } /* * ++x and x++ examples what happens in what order, pay close attention */ private void additionExample() { int x = 5; x += 4; System.out.println("after addtion: " + x); System.out.println("++x " + (++x)); System.out.println("x++ " + (x++)); } private void finalExample() { final int MAX = 5; //MAX = 6; System.out.println("MAX " + MAX); } /* * main method that starts program */ public static void main(String [] args) { Examples e = new Examples(); e.start(); } }