CSC297 2003F (TAO of Java), Class 3: Basic Types Admin: * Any questions yet? * We'll talk about BishopD's stuff at the end of class. * I'm behind on writing for you. You'll get some stuff soon. * Handout: C++ Plus Data Structures * Don't forget to keep the API at hand. Overview: * Declaring variables * Built-in Types * Primitive Types * Non-primitive built-in types Declaring Variables: * All variables must be declared in Java. * TYPE VARIABLE_NAME ; * String name; * int grade; * TYPE VARIABLE_NAME = INITIALIZIATION ; * String name = "Hero Man"; * int grade = -5; * Why require programmers to type and declare variables? * Helps avoid "spelling error" problems. * When a programmer uses a variable in a way incompatible with its type, he or she is usually mistaken. Built-In Types * Every object-oriented language needs built-in types * Language design decision: Are built-in types objects or "something else"? * In Java, there are "primitive" types that are not objects. (Essentially, they provide an interpretation of the underlying bits.) * boolean: truth values (true, false) (nothing else) * char: basic characters ('a', 'some-chinese-character'), uses Unicode * int: integer with restricted range (approximately -2^31 to 2^31) * short: integer with smaller restricted range (approximately -2^16 to 2^16) * long: integer with bigger restricted range (approximately -2^64 to 2^64) * float: approximated real number * double: better approximated real number * Why three versions of integer? * Memory usage: bigger range numbers require more memory * All of these built-in types have a syntax for direct entry of values * true, false: boolean * 'a': character * 12312: int or variant * 12.4: double * 12.4f: float * There are lots of built-in operations for these values * addition: + * subtraction: - * multiplication: * * division: / * modulus: % * What happens when you give one of these operations "incompatible" arguments (e.g., 3 + 4.5)? * Converts "less general" to the "more general" type. * Every primitive type has a corresponding class that provides some useful functionality and information * E.g., java.lang.Integer * How do you input (from the keyboard or file) a value of one of the primitive types? * Read a string * "Parse" it to get the underlying number. * Some built-in types are actually classes * String is the most common example * String student = "Alex";