apr, 2011 dating with java larry li. objective hello world program setup development environment...

Post on 14-Jan-2016

215 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Apr, 2011

Dating with Java

Larry Li

Objective

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

Java Tech Structure

Java code will compile to byte code

JVM will run byte code

Write Once, Run Anywhere

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”

public class Hello{

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

}

}

Our First Program

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)

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

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

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

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

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”.

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};

Operators Precedence

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

*,/,%

+,-

<,<=,>,>=,instanceof

== ,!=

&&

||

Condition ? X : Y

=,+= …

Operators

AssignmentMathematical operatorsIncrement and decrementRelational operatorsLogical operatorsConditional operatorImplicit type conversionsCasting operators

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;

Mathematical

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

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

Increment and decrement

++,--Unary operatorOperand must be a variable

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

Relational

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

x >= maxValueargs.length != 2

Logical

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

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

Operate-Assign Operators

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

age -= 10;mySalary += hisSalary;

Conditional Operator

op1 ? op2 : op3

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

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

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

Control Flow

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

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);}

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}

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;}

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);}

Continue/break statement

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

Continue/break statement

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

Label in Loop

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

Methods

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

Calling methodsDefining methodsMethod Parameters

Finished Today

Q&A

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)

Thank you

Larry.li@bleum.com

Java Bench

top related