260 likes | 280 Views
Lec.2: Java Basics. Jiang (Jen) ZHENG May 9 th , 2005. Outline. Quick Review Lexical Elements Operators Data Types Predefined Methods Programming Style Program Examples Analysis Topics for next lecture. Quick Review. Java has Application and Applet Java is Object-Oriented
E N D
Lec.2:Java Basics Jiang (Jen) ZHENG May 9th, 2005
Outline • Quick Review • Lexical Elements • Operators • Data Types • Predefined Methods • Programming Style • Program Examples Analysis • Topics for next lecture CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Quick Review • Java has Application and Applet • Java is Object-Oriented • Java is platform independent • Java is Garbage Collected CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Lexical Elements White Space & Comments keywords Tokenizing Source Code Further Analysis identifiers literals operators Early Stage of Compiling CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
White Space & Comments • White Space: • space between tokens and newline character after each line • Separate adjacent tokens and make program more readable • Comments • A single-line comment begins with // • A multi-line comments between /* and */ • Comments between /** and */ can be recognized by javadoc CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Keywords • 47 keywords in Java • Reserved, has special meaning to the Java compiler, can’t be used for any other purposes. • abstract boolean break byte case catch char class const continue default do double else extends final finally float for goto if implements import instanceof int interface long native new package private protected public return short static super switch synchronized this throw throws transient try void volatile while CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Identifiers • Define class, method or variable names. • Can be any sequence of Java letters and digits, the first must be Java letter • Java Letters: English language uppercase and lowercase letters, and $, _ • Java Digits: 0 - 9 • Can’t be keywords, true, false and null. CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Identifiers Naming Conventions • Classes: start with upper case, then start each words with an upper case latter • For example: HelloWorld, StringBuffer, BufferReader … • Methods: start with lower case, then start each word with an upper case letter • For example: compareTo, drawString, moreTokens() • Although legal, we should not use $ • Most of Java programmers are following this convention. CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Some Legal Identifiers • data // variable name • HelloWorld //class name • getTime //method name • itcanbeverylong • i • _123 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Some Illegal Identifiers • 3 • x+y • some**name • no Space • 1floor • class • public CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Literals • Representations of values for the primitive types. • 8 primitive types in Java Java Type Examples byte … int 123, 0, -999, short … long … float 3.1415, -0.05, 0.0 double … boolean true, false char ‘s’, ‘t’, ‘w’ CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Operators & Punctuations • Operators • +, -, *, /, % Ex: 3+5=8, 8%5=3 • =, +=, -=, *=, /=, %= (assignment operator) Ex: i+=j+k is equivalent to i=i+(j+k) • ++, -- (increment & decrement operators) Ex: i=i+1/i=i-1 is equivalent to i++/i-- • i++ (assignment first) and ++i (store value incremented first) Ex: int a, b, c=0; a = ++c; b = c++; System.out.println(“a=” + a + “; b=” + b + “; c=”+ c ); • Punctuations: • ; {} () CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Lexical Elements Example /* Your first Java Application */ class HelloWorldApp { public static void main(String[] args) { System.out.println(“Hello World!”); }} Tokenizing White Space & Comments /* Your first Java Application */ keywords class public static void identifiers HelloWorldApp args println String main System out literals “Hello World!” operators CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Precedence and Associativity of Operators • Precedence: The order that operations are evaluated • Associativity: The order that operators of equal precedence are to be evaluated. CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Operator Associativity ( ) ++(postfix) --(postfix) Left to right +(unary) -(unary) ++(prefix) --(prefix) Right to left new (type)expr Right to left * / % Left to right + - Left to right = += -= *= /= %= etc. Right to left Precedence and Associativity of Operators • Examples: Given a=1, b=2, c=3, d=4, what are the values of following expressions. Expression Equivalent Expression Value a * b / c ( a * b ) / c 0 a * b % c + 1 ( ( a * b ) % c ) + 1 3 ++a * b – c-- ( ( ++a ) * b ) – (c --) 1 7 - - b * ++d 7 – ( (-b) * (++d) ) 17 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Data Types • Java is strongly typed languages. Each data item has a declared type. • Primitive types: • int, double, char … • Created by using literals: a= 5; • Can apply on built-in operators: 2+3 • Class types -- Objects: • Standard Java classes: String, Button, Graphics … 1500 classes • Programmer can define new classes: HelloWorld • Created by using new keyword: new Button(“Start”); String is special, String a = “hello”; • Can have operations/methods. Ex: a.length(); CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Variables & Initialization • Example: • boolean flag = true; • int j=0, k=100; • Button exitButton = new Button(“exit”); • flag, j, k, exitButton are variables. • Variables are identifiers used to refer to data values that are stored in computer’s memory. • Can be initialized during declaration. CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
More On Data Types:Integer Types & Char Type • byte, short, int, long are all integer types to represent integers. Difference is number of bits and the value range. • char is to represent characters and is also integer type. For example, ‘a’=97, ‘A’ =65 • P35 table provides all of the non-printing and hard to print character. \\ , \t, \n, \’’, \’ CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Floating Types • a sign + a magnitude + an exponentEx: 2.17e-27 ( 2.17×10-27) • float (32 bits) and double (64 bits) • Number of bits, value range and approximate precision are different. CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Arithmetic Expressions • int, long, float, double are the four types recognized by Java for arithmetic operations. • Rules for converting operands: • If one is double, all converted to double. • Otherwise If one is float, all converted to float. • Otherwise If one is long, all converted to long. • Otherwise, allare converted to int. • For example: 6 / 4 = 1; 6 / (-4) = -1 ; 6.0 / 4=1.5; CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Type Conversions • Widening primitive conversions automatically. • Automatically during the mixed mode arithmetic • Convert operators of the left-hand-side to right-hand-side type. • Narrowing primitive conversions by cast. • Cast: (type) expression int someInt; float someFloat=3.14159; someInt = (int) someFloat; System.out.print (someInt ); Output is: 3 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Predefined Methods • A method is a group of instructions having a name. • For example: main, drawLine, length, concat, print, println … • Predefined methods: Java provided, can be used directly by calling objectName.methodName(parameters) • For example: System.out.print(x); System.out.println(x); Math.min(x, y); Math.sqrt(x); string1.concat(string2); string1.length(); • Many more predefined methods. • (Appendix B, Chap. 8-10, 13) CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Programming Style • Clear, consistent • Good documentation • Good for you to read later • Good for other programmers to read • Follow the conventions by the large party of the programming community. • Class name convention • Method name convention • Variable name convention CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Program Example 1 import java.io.*; class HelloWorld2 { public static void main( String[] args) { String word1; String word2, sentence; word1 = “Hello, ”; word2 = “world !”; sentence = word1.concat(word2); System.out.println ( sentence ); } } CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Program Example 2 /** some comments here. */ import java.awt.*; import java.swing.*; Public class OneApplet extends JApplet { public void paint( Graphics g) { /* some comments here */ g.drawLine(0, 0, 250, 100); g.drawOval(250, 100, 200, 100); } } CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2
Topics for next lecture • Reading Assignment: Chapter 3 You don’t need to know the details but you should know what are those when I talk about it. • How to do User Input / Standard Input CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 2