Chap 2. Identifiers, Keywords, and Types Objectives Comment Identifier Keyword Basic meaning: Class, object, member variable, reference Create a Java class definition Declare variables of class type Constructor Access member variables of an object using dot notation reference variable
Comments Comments in a program are also called inline documentation They should be included to explain the purpose of the program and describe processing steps Java comments can take two forms: // comment runs to the end of the line /* comment runs to terminating symbol, even across line breaks */
Semicolon, Blocks, White Space In java, statements are terminated with a semicolon(;) A block is marked by enclosing it in brackets({ }) Spaces, blank lines, and tabs are collectively called white space and are used to separate words and symbols in a program Extra white space is ignored
Identifiers Identifiers are the words a programmer uses in a program Most identifiers have no predefined meaning except as specified by the programmer An identifier can be made up of letters, digits, the underscore character (_), and the dollar sign They cannot begin with a digit Java is case sensitive, therefore Total and total are different identifiers
Identifier examples Valid identifier Invalid identifier identifier userName User_name _sys_var1 $change Invalid identifier 3identifier
Reserved Words(Keywords) Some identifiers, called reserved words, have specific meanings in Java and cannot be used in other ways abstract boolean break byte byvalue case cast catch char class const continue default do double else extends false final finally float for future generic goto if implements import inner instanceof int interface long native new null operator outer package private protected public rest return short static super switch synchronized this throw throws transient true try var void volatile while
Primitive Data Types A data type is defined by a set of values and the operators you can perform on them Each value stored in memory is associated with a particular data type The Java language has several predefined types, called primitive data types The following reserved words represent eight different primitive types: byte, short, int, long, float, double, boolean, char
Integers There are four separate integer primitive data types They differ by the amount of memory used to store them Type byte short int long Storage 8 bits 16 bits 32 bits 64 bits Min Value -128 -32,768 -2,147,483,648 < -9 x 1018 Max Value 127 32,767 2,147,483,647 > 9 x 1018
Characters The ASCII character set is still the basis for many other programming languages ASCII is a subset of Unicode, including: uppercase letters lowercase letters punctuation digits special symbols control characters A, B, C, … a, b, c, … period, semi-colon, … 0, 1, 2, … &, |, \, … carriage return, tab, ...
Floating Point There are two floating point types: The float type stores 7 significant digits The double type stores 15 significant digits Approximate Min Value -3.4 x 1038 -1.7 x 10308 Approximate Max Value 3.4 x 1038 1.7 x 10308 Type float double Storage 32 bits 64 bits
Characters A char value stores a single character from the Unicode character set A character set is an ordered list of characters The Unicode character set uses sixteen bits per character, allowing for 65,536 unique characters It is an international character set, containing symbols and characters from many world languages
Boolean A boolean value represents a true or false condition They can also be used to represent any two states, such as a light bulb being on or off The reserved words true and false are the only valid values for a boolean type
Wrappers For each primitive data type there is a corresponding wrapper class. For example: Wrapper classes are useful in situations where you need an object instead of a primitive type They also contain some useful methods Primitive Type int double char boolean Wrapper Class Integer Double Character Boolean
"To thine own self be true." Literals A literal is an explicit data value used in a program Integer literals: 25 69 -4288 Floating point literals: 3.14159 42.075 -0.5 String literals: "The result is: " "To thine own self be true."
Variables A variable is an identifier that represents a location in memory that holds a particular type of data Variables must be declared before they can be used The syntax of a variable declaration is: data-type variable-name; For example: int total;
Variables Multiple variables can be declared on the same line: int total, count, sum; Variables can be initialized (given an initial value) in the declaration: int total = 0, count = 20; float unit_price = 57.25;
Assignment Statements An assignment statement takes the following form: variable-name = expression; The expression is evaluated and the result is stored in the variable, overwriting the value currently stored in the variable The expression can be a single value or a more complicated calculation
Constants A constant is similar to a variable except that they keep the same value throughout their existence They are specified using the reserved word final in the declaration For example: final double PI = 3.14159; final int STUDENTS = 25;
Aggregate Data Types. public class Date {. int day;. int month; Aggregate Data Types public class Date { int day; int month; int year; } Date mybirth, yourbirth; mybirth.day=26; mybirth.month=11; yourbirth.year=1960;
Java Coding Convention Class name should be noun(첫문자는 대문자) Interface name should be capitalized like class names. Method names should be verb(첫문자는 소문자) Variable: 소문자로 시작, meaningful Use comments to explain code segments Place only a single statement on any line, use 4-space indentation, to make your code readable
Objects An object has: For example, a particular bank account state - descriptive characteristics behaviors - what it can do (or be done to it) For example, a particular bank account has an account number has a current balance can be deposited into can be withdrawn from
Classes A class is a blueprint of an object It is the model or pattern from which objects are created A class defines the methods and types of data associated with an object Creating an object from a class is called instantiation; an object is an instance of a particular class For example, the Date class could describe many bank accounts, but mybirth is a particular bank account with a particular balance
Creating Objects The new operator creates an object from a class: Date mybirth = new Date(); This declaration asserts that mybirth is a variable that refers to an object created from the Date class It is initialized to the object created by the new operator The newly created object is set up by a call to a constructor of the class