CS177 Week2: Recitation Primitive data types and Strings with code examples
Questions?
Data Types
int int x; //declaration of variable x x = 23; //assigns the number 23 to x Stored as 1’s and 0’s. 23d = 10111b. d stands for decimal, b for binary
Binary -> Decimal Conversion What does b equal in decimal?
222
Decimal -> Binary Conversion Do the following steps: Start with a decimal number x Set binary number b = 0 Until x = 0 Is x odd? If yes, put a 1 at the end of b If no, put a 0 at the end of b Divide x by 2, rounding down (truncation) Reverse the digits of b
Ex: x = 9
Ex: x = b = 222d, just like before
Positive and Negative Numbers Positive numbers start with a 0 bit Negative numbers start with a 1 bit 2’s complement – way that a negative number is represented
Doubles Rational numbers (can still be an integer, but it is stored differently) 64 bits double y = 1.25; //declares double y and assigns 1.25 to y double z = 3; double a = 2.0; double b = -8.8;
Loosely typed or strongly typed? Java is: Strongly typed double x; x = 3; What is the type of variable x? A double!
Strings Many letters in one type String word1 = “purdue”; String word2 = “boilermakers”; String word3 = “p”; String word4 = “”; //This is legal – called empty String
Chars char – one character. Could be any ASCII character String – many characters together char w = ‘a’; char x = w; char y = ‘w’; String z = “w”; Are the variables w and x equal? Are the variables x and y equal? Are the variables y and z equal?
String - String concatenation /************************************************************************* * Compilation: javac Ruler.java * Execution: java Ruler * * Prints the relative lengths of the subdivisions on a ruler. * * % java Ruler * 1 * * * * * *************************************************************************/
public class Ruler { public static void main(String[] args) { String ruler1 = "1 "; String ruler2 = ruler1 + "2 " + ruler1; String ruler3 = ruler2 + "3 " + ruler2; String ruler4 = ruler3 + "4 " + ruler3; String ruler5 = ruler4 + "5 " + ruler4; System.out.println(ruler1); System.out.println(ruler2); System.out.println(ruler3); System.out.println(ruler4); System.out.println(ruler5); }
Double Operation - Quadratic formula /************************************************************************* * Compilation: javac Quadratic.java * Execution: java Quadatic b c * Given b and c, solves for the roots of x*x + b*x + c. * Assumes both roots are real valued. * % java Quadratic * 2.0 * 1.0 * % java Quadratic * * * * Remark: is the golden ratio. * * % java Quadratic * NaN *************************************************************************/
public class Quadratic { public static void main(String[] args) { double b = Double.parseDouble(args[0]); double c = Double.parseDouble(args[1]); double discriminant = b*b - 4.0*c; double sqroot = Math.sqrt(discriminant); double root1 = (-b + sqroot) / 2.0; double root2 = (-b - sqroot) / 2.0; System.out.println(root1); System.out.println(root2); }
Char Operation /************************************************************************* * Compilation: javac Charactertest.java * Execution: java Charactertest Is this letter lowercase? true Is this letter uppercase? false Is this a letter? true /************************************************************************* public class Charactertest { public static void main(String args[]) { char letter = 'd'; boolean lowerCase = Character.isLowerCase(letter); boolean upperCase = Character.isUpperCase(letter); boolean letterOrdigit = Character.isLetter(letter); System.out.println("Is this letter lowercase? " + lowerCase); System.out.println("Is this letter uppercase? " + upperCase); System.out.println("Is this a letter? " + letterOrdigit); }
Questions?