250 likes | 370 Views
Lecture 2. Introduction to Java Programming. Java Class Format. public class userDefinedName { public static void main(String[] args){ } }. 2.2 Primitive Data Types. Lexical Elements. Keywords Literals Identifiers Operators Punctuation. Keywords.
E N D
Lecture 2 Introduction to Java Programming
Java Class Format public class userDefinedName {public static void main(String[] args){}}
Lexical Elements • Keywords • Literals • Identifiers • Operators • Punctuation
Keywords abstract default if private this boolean do implements protected throw break double import public throws byte else instanceof return transient case extends int short try catch final interface static void char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized • Can not be used for any other purpose • All lowercase letters
Literal • A literal is the source code representation of a value of a primitive type or the String type Data typeLiteral Examplesint -4501 double 35.271d char ‘b’ boolean true String “Hello World”
variable names class names constants Identifiers • An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter. An identifier cannot have the same spelling as a keyword or boolean literal. Example identifierssumprodroot1isLeapYearQuadraticLeapYearMAX
Punctuation • The following nine ASCII characters are the separators (punctuators): ( ) { } [ ] ; , .
White Space • White space is defined as the ASCII space, horizontal tab, and form feed characters, as well as line terminators
Comments • There are two kinds of comments: • /* text */A traditional comment: all the text from the ASCII characters /* to the ASCII characters */ is ignored by the compiler • // textA end-of-line comment: all the text from the ASCII characters // to the end of the line is ignored by the compiler.
All variables must be defined • Tell the compiler the • name of each variable • type of each variable int x;int y;boolean isLeapYear;boolean isPerflick;double root1;double root2; int x, y;boolean isLeapYear, isPerflick;double root1, root2;
Initializing variables int x = 5;int y; //y need not be initialized y = 2*x + 1;…