Lec 3 Char, String Object, Math
Lec 3 Char, String Object, Math
Computer Programming
Lecture 3: char, String object, Math
class, Scanner
Java's primitive types
• primitive types: there are 8 simple types for numbers, text, etc.
Type Description Size
byte The type describing a single byte, with range -128 . . . 127 1 byte
short The short integer type, with range -32768 . . . 32767 2 bytes
• Java also has object types (e.g. Strings), which we'll talk about later
Type char
char data type
• char : A primitive data type representing single characters
of text (e.g., 'a', 'b', '@', ' ', etc.).
Output:
students
char vs. int
• Each char is mapped to an integer value internally
– Called an ASCII value
x = 'a' + 3;
System.out.println(x);
}
Output:
e
100
Objects
• object: An entity that contains data and behavior.
– data: variables inside the object
– behavior: methods inside the object
• You interact with the methods;
the data is hidden in the object.
• Example:
public static void main(String[] args) {
…
}
Strings
• string: An object storing a sequence of text characters.
– Unlike most other objects, a String is not created with new.
– Examples:
String name = "Marla Singer";
int x = 3;
int y = 5;
String point = "(" + x + ", " + y + ")";
String objects
A variable of type String is different from the other (primitive) data
types we’ve seen so far
It is actually a reference to a String object .
Examples:
String str = "hello there!";
int len = str.length();
String first = str.substring(0, 1);
Indexes
• Characters of a string are numbered with 0-based
indexes:
String name = "R. Kelly";
index 0 1 2 3 4 5 6 7
character R . K e l l y
Output:
c is for cookie
char vs. String
• "h" is a String, but 'h' is a char (they are different)
• A String is an object; it contains methods.
String s = "h";
s = s.toUpperCase(); // "H"
int len = s.length(); // 1
char first = s.charAt(0); // 'H'
– Example:
Scanner console = new Scanner(System.in);
Scanner methods
Method Description
nextInt() reads an int from the user and returns it
nextDouble() reads a double from the user
next() reads a one-word String from the user
next().charAt(0) reads one char from the user
nextLine() reads a one-line String from the user
• Examples:
double squareRoot = Math.sqrt(121.0);
System.out.println(squareRoot); // 11.0
int absoluteValue = Math.abs(-50);
System.out.println(absoluteValue); // 50
System.out.println(Math.min(3, 7) + 2); // 5
double exp = Math.pow(2, 4);
System.out.println(exp); // 16.0
-42 Math.abs(-42)
42
main
2.71
3
Math.round(2.71)
Remember: Type casting
• type cast: A conversion from one type to another.
– To promote an int into a double to get exact division from /
– To truncate a double from a real number to an integer
• Syntax:
(type) expression
Examples:
double result = (double) 19 / 5; // 3.8
int result2 = (int) result; // 3
int x = (int) Math.pow(10, 3); // 1000
Math questions
• Evaluate the following expressions:
– Math.abs(-1.23)
– Math.pow(3, 2)
– Math.pow(10, -2)
– Math.sqrt(121.0) – Math.sqrt(256.0)
– Math.round(Math.PI) +
Math.round(Math.E)
– Math.ceil(6.022) + Math.floor(15.9994)
– Math.abs(Math.min(-3, -5))
Generate a Random Variable
• Generate a random number between low and high (inclusive)