Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment...

35
Apr, 2011 Dating with Java Larry Li

Transcript of Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment...

Page 1: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Apr, 2011

Dating with Java

Larry Li

Page 2: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Objective

Hello world programSetup development environmentData types and variables Operators and ExpressionsControl FlowMethod

Page 3: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Java Tech Structure

Java code will compile to byte code

JVM will run byte code

Write Once, Run Anywhere

Page 4: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Setup Development Environment

Download JDK

Set environment variables(JAVA_HOME , CLASSPATH , PATH)

JAVA_HOME : “c:\Sun\jdk1.6.0_24”PATH : “%JAVA_HOME%\bin”CLASS_PATH:”.;%JAVA_HOME\lib\dt.jar”;%JAVA_HOME\lib\tools.jar”

Page 5: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

public class Hello{

public static void main(String[] args){System.out.println(“Hello World”);

}

}

Our First Program

Page 6: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Compile and Run

Java source file name is “ClassName.java”

Use javac to compile java program (c:\javac Hello.java)

Use java to run java program(c:\java Hello)

Page 7: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Datatypes

Type Size Minimum Maximum Wrapper type Default

boolean - - - Boolean false

char 16bits \u0000 \uffff Character \u0000

byte 8bits -128 127 Byte 0

short 16bits -32768 32767 Short 0

int 32bits -231 231 -1 Integer 0

long 64bits -263 263 -1 Long 0

float 32bits IEEE754 IEEE754 Float 0.0F

double 64bits IEEE754 IEEE754 Double 0.0D

void - - - Void -

reference - - - - null

Page 8: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Declare Variables

int age = 25;

Declaration must before the first statement that uses the local variable.

Must initialize a local variable before using it.

final int height = 179;

Variables declared with modifier final must be assigned before use and cannot be changed once assigned

Page 9: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Legal Identifier

A variable name is a sequence of letters, digits, and underscores.

Must start with a letter or underscore.

Can be as long(or short)as you want.

Must not be a keyword or a literal value

Page 10: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Java Keywords

abstract continue for new switch

assert default goto* package synchronized

boolean do if private this

break double implements protected throw

byte else import public throws

case enum instanceof return transient

catch extends int short try

char final interface static void

class finally long strictfp volatile

const* float native super while

Page 11: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Literals

Boolean literals : ”true” , ”false”.

Null literals : ”null”.

Numeric literals : “10”,”15L”,”1.1F”,”2.0”,”6.5E+1f”,”2.65e-8”

Character literals : ‘A’,’\u0041’.

String literal : “James Gosling”.

Page 12: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Arrays

A variable declared with brackets,[],is an array reference.

Must use new to actually create the array, array is object.

float price[];String[] title, author;price = new new float[44];String[] names = new String[10];names[1] = “James Gosling”;price[0] = 99.95F;int odds = {1,3,5,7,9};

Page 13: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Operators Precedence

!, ~, ++, --, +(positive), –(negative) ,( ) (cast)

*,/,%

+,-

<,<=,>,>=,instanceof

== ,!=

&&

||

Condition ? X : Y

=,+= …

Page 14: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Operators

AssignmentMathematical operatorsIncrement and decrementRelational operatorsLogical operatorsConditional operatorImplicit type conversionsCasting operators

Page 15: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Assignment

Assign a value to a variable.Left operand must be a variable.Assignment expression's value is the value that

was assignedEvaluated right to left

min = 5;5 = x;// illegal!current = (min = 5);max = current = min = 5;

Page 16: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Mathematical

+,-,*,/,%+,-(unary operator)

age+1;daysLeft -3;width * height;totalLines / numPages;curLine%6;-balance

Page 17: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Increment and decrement

++,--Unary operatorOperand must be a variable

--age;weight--;salary++;++numCloth;

Page 18: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Relational

>,>=,<,<=,==,!=.Results a true or false value.

x >= maxValueargs.length != 2

Page 19: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Logical

||,&&,!|,&&& and || operators use short-circuit evaluation.

length < 12 || width > 18height >= 10 && height <= 20!(height >= 10 && height <=20)

Page 20: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Operate-Assign Operators

+=,-=,etc.Correspond to all the basic arithmetic operators

age -= 10;mySalary += hisSalary;

Page 21: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Conditional Operator

op1 ? op2 : op3

char status;int age = 16;Status = age >= 18 ? ‘a’ : ‘m’

Page 22: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Implicit Type Conversions

Operands of different data types in an expression are converted to a common type.

Smaller data types are converted to larger data types.

Conversions that would truncate data will not happen implicitlyfloat fTotal;float f = 6.5F;int i = 5,iTotal;fTotal = f + i;iTotal = f + i;// ERROR

Page 23: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Cast Operator

Conversions can be forced by explicitly casting a value or expression to a new data type;

If the value being cast is too large to fit in the smaller location, the bits of the value will be truncated.

To convert to a String, do not use the cast operator.

float f = 6.5F, g = 7.7F;int total = (int)(f + g);String s = “” + f;// if one operand is String

Page 24: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Control Flow

StatementConditional Statements (if-else, switch)Loop(do-while, while, for)Continue, Break statementLabel in loop

Page 25: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Statement

A single statement is an expression followed by a semicolon.

Curly braces group statements into compound statements, called blocks.

int area;{ area = width * length; System.out.println(“area is ” + area);}

Page 26: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Conditional (if) Statement

if, if-else if, else

if (age < 12 && age > 3){ //do stuff}else if ( age >= 16 ){ //do stuff}else{ //else do stuff}

Page 27: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Conditional (switch) Statement

Use a switch statement when you are testing the same expression for several possible literal values.

Only compare expression of type byte, short, int, char, enum.switch (choice){ case ‘a’:

// do somethingbreak;

case ‘b’ : // do something

break; default :

// do default break;}

Page 28: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Loops

do-while, while, fordo{ // do something}while (hasNext);while(hasNext){ // do something}for(int i=0; i<5 ;i++){ // do something}int odds[] = {1,3,5,7,9};for(int n : odds){ System.out.println(n);}

Page 29: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Continue/break statement

int i=0;while(true){ if (i % 2 != 0){ cotinue; } System.out.println(i); if (i > 10){ break; }}

Page 30: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Continue/break statement

int i=0;while(true){ if (i % 2 != 0){ cotinue; } System.out.println(i); if (i > 10){ break; }}

Page 31: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Label in Loop

label:while(true){ while(true){ break; continue; continue label; break label; }}

Page 32: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Methods

//defingfloat getRectangleArea(float width,float height){ float area = width * height; return area;}//callingint rectangleArea = getRectangleArea(thisWidth,thisHeight);

Calling methodsDefining methodsMethod Parameters

Page 33: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Finished Today

Q&A

Page 34: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Homework

1. Setup development environment2. A program print your name3. Compile and run this program4. A program print your dog’s information , including :

name ,weight ,length of tail , friendly or not5. Write a program with a big integer, big = 2147483647

and a bigger , bigger = big+1,print the tow number and explain the result

6. A program can convert Fahrenheit to Celsius (C = F minus 32 multiply by five ninth)

Page 35: Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Thank you

[email protected]

Java Bench