Computer Science 15-110, Spring 2010
Class Notes: Data and Expressions (3 of 3)
Data and Expressions (3 of 3)
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