Computer Science 15-100, Spring 2009
Class Notes: Data and Expressions
Data and Expressions
class MyCode {
public static void main(String[] args) {
int x = 3;
System.out.println(x);
}
}
Same, but with a nicer UI
class MyCode {
public static void main(String[] args) {
int x = 3;
System.out.println("x = " + x);
}
}class MyCode {
public static void main(String[] args) {
System.out.println("x = " + x); // ERROR! No such variable as x
}
}class MyCode {
public static void main(String[] args) {
int x; // declare the variable
x = 5; // initialize it (assign it its first value)
System.out.println("x = " + x);
}
}class MyCode {
public static void main(String[] args) {
int x;
x = 1;
System.out.println("x = " + x); // prints "x = 1"
x = 2;
System.out.println("x = " + x); // prints "x = 2"
}
}class MyCode {
public static void main(String[] args) {
int x; // declared, but not assigned a value
System.out.println("x = " + x); // ERROR! x is not initialized
}
}class MyCode {
public static void main(String[] args) {
int x = 1;
int y = 2;
System.out.println("x = " + x);
System.out.println("y = " + y);
}
}class MyCode {
public static void main(String[] args) {
int x = 1, y = 2;
System.out.println("x = " + x);
System.out.println("y = " + y);
}
}class MyCode {
public static void main(String[] args) {
int x = 1, y = 2;
System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("x + y = " + (x + y));
}
}
Same, but with a nicer UI
class MyCode {
public static void main(String[] args) {
int x = 1, y = 2;
System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("x + y = "
+ x + " + " + y
+ " = " + (x + y));
}
}class MyCode {
public static void main(String[] args) {
System.out.println(2+3*4); // prints 14, not 20
}
}class MyCode {
public static void main(String[] args) {
int x = 1*1000*1000*1000; // 1 billion
int y = 2*1000*1000*1000; // 2 billion
System.out.println(x + y); // Prints -1294967296
}
}class MyCode {
public static void main(String[] args) {
System.out.println("20/3 = " + (20/3));
System.out.println(" 6/3 = " + ( 6/3));
System.out.println(" 5/3 = " + ( 5/3));
System.out.println(" 2/3 = " + ( 2/3));
}
}class MyCode {
public static void main(String[] args) {
System.out.println(" 7%3 = " + (7%3));
System.out.println(" 6%3 = " + (6%3));
System.out.println(" 5%3 = " + (5%3));
System.out.println(" 3%3 = " + (3%3));
System.out.println(" 2%3 = " + (2%3));
System.out.println(" 0%3 = " + (0%3));
System.out.println(" 3%0 = " + (3%0));
}
}class MyCode {
public static void main(String[] args) {
int x = 5;
System.out.println(x); // 5
x++;
System.out.println(x); // 6
++x;
System.out.println(x); // 7
x--;
System.out.println(x); // 6
--x;
System.out.println(x); // 5
}
}
class MyCode {
public static void main(String[] args) {
int x = 5;
System.out.println(x); // 5
x += 2;
System.out.println(x); // 7
x *= 2;
System.out.println(x); // 14
x %= 9;
System.out.println(x); // 5
x /= 2;
System.out.println(x); // 2
x -= 5;
System.out.println(x); // -3
}
}
class MyCode {
public static void main(String[] args) {
System.out.println(Math.abs(4)); // 4
System.out.println(Math.abs(-4)); // 4
System.out.println(Math.min(3, 2)); // 2
System.out.println(Math.min(2, 3)); // 2
System.out.println(Math.max(3, 2)); // 3
System.out.println(Math.max(2, 3)); // 3
// We can use these in expressions, too:
int x = Math.min(5, -13);
int y = Math.max(x, x/2);
int z = Math.abs(x) + Math.abs(y);
System.out.println(x);
System.out.println(y);
System.out.println(z);
}
}
class MyCode {
public static void main(String[] args) {
boolean b = true;
System.out.println(b); // prints true
System.out.println(false); // prints false
}
}class MyCode {
public static void main(String[] args) {
System.out.println(5 == 4);
System.out.println(5 == 5);
System.out.println(5 != 4);
System.out.println(5 != 5);
}
}class MyCode {
public static void main(String[] args) {
System.out.println(5 < 4);
System.out.println(5 > 4);
System.out.println(5 < 5);
System.out.println(5 <= 5);
System.out.println(5 >= 5);
System.out.println(5 > 5);
}
}class MyCode {
public static void main(String[] args) {
System.out.println("&& (AND)");
System.out.println(true && true );
System.out.println(true && false);
System.out.println(false && true );
System.out.println(false && false);
System.out.println("|| (OR)");
System.out.println(true || true );
System.out.println(true || false);
System.out.println(false || true );
System.out.println(false || false);
System.out.println("! (NOT)");
System.out.println( !true );
System.out.println( !false );
}
}class MyCode {
public static void main(String[] args) {
int x = 0, y = 0;
System.out.println((y != 0) && ((x/y) != 0)); // Works!
System.out.println(((x/y) != 0) && (y != 0)); // Crashes!
}
}
Once again, using the "or" operator ( || )
class MyCode {
public static void main(String[] args) {
int x = 0, y = 0;
System.out.println((y == 0) || ((x/y) == 0)); // Works!
System.out.println(((x/y) == 0) || (y == 0)); // Crashes!
}
}class MyCode {
public static void main(String[] args) {
System.out.println(1.2);
}
}class MyCode {
public static void main(String[] args) {
double x = 1.2;
System.out.println(2*x);
}
}class MyCode {
public static void main(String[] args) {
int i = 10;
double d = 10.0;
System.out.println( 10 / 3 );
System.out.println( i / 3 );
System.out.println( d / 3 );
System.out.println( 10 / 3.0 );
System.out.println( i / 3.0 );
}
}class MyCode {
public static void main(String[] args) {
double d1 = (29.0 / 7.0) * 7.0;
double d2 = 29.0;
System.out.println(d1 == d2); // false!
System.out.println(d2 - d1); // -3.552713678800501E-15 (tiny!)
}
}class MyCode {
public static void main(String[] args) {
double d1 = (29.0 / 7.0) * 7.0;
double d2 = 29.0;
System.out.println(d1 == d2); // still false (of course)
// now compare if their difference is very small...
double epsilon = 0.000001;
System.out.println(Math.abs(d2 - d1) < epsilon); // true!
}
}class MyCode {
public static void main(String[] args) {
System.out.println(Math.abs(4.2)); // 4.2
System.out.println(Math.abs(-4.2)); // 4.2
System.out.println(Math.min(3.5, 2.5)); // 2.5
System.out.println(Math.min(2.5, 3.5)); // 2.5
System.out.println(Math.max(3.5, 2.5)); // 3.5
System.out.println(Math.max(2.5, 3.5)); // 3.5
// We can use these in expressions, too:
double x = Math.min(5, -13);
double y = Math.max(x, x/2);
double z = Math.abs(x) + Math.abs(y);
System.out.println(x);
System.out.println(y);
System.out.println(z);
}
}
Some Math methods (like pow and sqrt) do not have int versions:
class MyCode {
public static void main(String[] args) {
System.out.println(Math.pow(2, 3)); // 8.0 (and not int 8), which is 2^3
System.out.println(Math.sqrt(9)); // 3.0 which is the square root of 9
System.out.println(Math.pow(9, 0.5)); // ditto (why?)
}
}class MyCode {
public static void main(String[] args) {
String s = "A"; // use double-quotes, not single-quotes
System.out.println(s);
s = ""; // Empty strings (of zero length) are allowed!
System.out.println(s);
s = 'b'; // ERROR! Must use double-quotes!
System.out.println(s);
}
}class MyCode {
public static void main(String[] args) {
String s = "a";
System.out.println(s.length());
s = "ab";
System.out.println(s.length());
s = "";
System.out.println(s.length());
System.out.println(s.length() == 0);
}
}class MyCode {
public static void main(String[] args) {
String s = ""; // empty string
System.out.println(s == null);
System.out.println(s.length());
s = null; // null string
System.out.println(s == null);
System.out.println(s.length()); // Runtime Error!
}
}class MyCode {
public static void main(String[] args) {
System.out.println("a" + "b");
System.out.println("a" + 1 );
System.out.println("a" + 1 + "2");
System.out.println("a" + 1 + 2 );
System.out.println( 1 + 2 + "a");
}
}class MyCode {
public static void main(String[] args) {
System.out.println("Double-quote: \"");
System.out.println("Backslash: \\");
System.out.println("Newline (in brackets): [\n]");
System.out.println("Tab (in brackets): [\t]");
System.out.println("These items are tab-delimited, 3-per-line:");
System.out.println("abc\tdef\tg\nhi\tj\\\tk\n---");
}
}class MyCode {
public static void main(String[] args) {
char c = 'A'; // use single-quotes, not double-quotes
System.out.println(c);
c = '\\'; // Escape sequences work here, too!
System.out.println(c);
c = "b"; // ERROR! Must use single-quotes!
System.out.println(c);
c = 'ab'; // ERROR! Only one character allowed!
System.out.println(c);
c = ''; // ERROR! No empty char literals allowed!
System.out.println(c);
c = null; // ERROR! No null char allowed!
System.out.println(c);
}
}class MyCode {
public static void main(String[] args) {
System.out.println('D' >= 'A');
System.out.println('D' <= 'Z');
System.out.println('D' >= 'a');
System.out.println('D' <= 'z');
System.out.println("Testing for uppercase:");
char c = 'D';
System.out.println((c >= 'A') && (c <= 'Z'));
c = 'd';
System.out.println((c >= 'A') && (c <= 'Z'));
}
}| to String | to int | to double | to char | to boolean | |
| from String |
n/a |
// use "parse"
method int i = Integer.parseInt(s); |
// use "parse"
method double d = Double.parseDouble(s); |
// use "charAt"
method char c = s.charAt(0); |
// use "parse"
method boolean b = Boolean.parseBoolean(s); |
| from int |
// String
concatenation String s = "" + i; |
n/a |
// Widening, so
assign double d = i; |
// Narrowing, so
cast char c = (char) i; |
n/a |
| from double |
// String
concatenation String s = "" + d; |
// Narrowing, so
cast int i = (int) d; |
n/a | n/a | n/a |
| from char |
// String
concatenation String s = "" + c; |
// Widening, so
assign int i = c; |
// Widening, so
assign double d = c; |
n/a | n/a |
| from boolean |
// String
concatenation String s = "" + b; |
n/a | n/a | n/a | n/a |
Same table, with more details:
| to String | to int | to double | to char | to boolean | |
| from String |
n/a | // From String to int: // use "parse" method String s = "42"; int i = Integer.parseInt(s); System.out.print(i); // prints 42 |
// From String to double: // use "parse" method String s = "42.0"; double d = Double.parseDouble(s); System.out.print(d); // prints 42.0 |
// From String to char: // use "charAt" method String s = "ABCD"; char c = s.charAt(0); System.out.print(c); // prints A |
// From String to boolean: // use "parse" method String s = "true"; boolean b = Boolean.parseBoolean(s); System.out.print(b); // prints true |
| from int |
// From int to String: // String concatenation int i = 65; String s = "" + i; System.out.print(s); // prints 65 (as a String) |
n/a |
// From int to double: // Widening, so assign int i = 3; double d = i; System.out.print(d); // prints 3.0 |
// From int to char: // Narrowing, so cast int i = 65; char c = (char) i; System.out.print(c); // prints A (UNICODE 65) |
n/a |
| from double |
// From double to String: // String concatenation double d = 3.7; String s = "" + d; System.out.print(s); // prints 3.7 (as a String) |
// From double to int: // Narrowing, so cast double d = 3.7; int i = (int) d; System.out.print(i); // prints 3 |
n/a | n/a | n/a |
| from char |
// From char to String: // String concatenation char c = 'A'; String s = "" + c; System.out.print(s); // prints A (as a String) |
// From char to int: // Widening, so assign char c = 'A'; int i = c; System.out.print(i); // prints 65 (UNICODE 'A') |
// From char to double: // Widening, so assign char c = 'A'; double d = c; System.out.print(d); // prints 65.0 // UNICODE 'A' as a double |
n/a | n/a |
| from boolean |
// From boolean to String: // String concatenation boolean b = true; String s = "" + b; System.out.print(s); // prints true (as a String) |
n/a | n/a | n/a | n/a |
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem