review… yong choi bpa csub. access modifier public class helloworldapp –more detailes of java...

33
Review… Yong Choi BPA CSUB

Post on 20-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Review…

Yong Choi

BPA

CSUB

Page 2: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Access Modifier

• public class HelloWorldApp– More detailes of Java key (reserved) word– The keyword, public is called an access

modifier and Java code usually begin with an access modifier

– public, indicates that this code can be accessed by all objects and can be extended, or used, as a basis for another class. (opposite to private)

– If you omit the keyword, public, you limit the access to this class - private

Page 3: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Class Name

• The access modifier is followed by the word, class, and the class name.

• The class name (“HelloWorldApp”) is assigned by a programmer.

• It should be a user-friendly word that is not on the list of key words.

• By using understandable and user-friendly word for class, objects, and variables, you can not only avoid confusions but also increase understandability of your program.– class X vs. class GrandTotal

Page 4: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

A Key (reserved) words list

• abstract• boolean• break• byte• case• catch• char• class• const• continue• default• do

• int• interface• long• native• new• package• private• protected• public• return• short• static

• double• else• extends• final• finally• float• for• goto• if• implements• import• instanceof

• strictfp• super• switch• synchronize

d• this• throw• throws• transient • try• void• volatile• while

Page 5: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Requirements for the Class Name

• A class name should begin with a letter of the alphabet.– includes any non-English letter, such as or ,

an underscore, or a dollar sign.

• A class name can contain only letters, digits, underscore, or a dollar signs.

• A class name cannot be a Java programming key words such as public or class.

• A class name cannot be one of following values: true, false, or null.

Page 6: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

More about Class Name

• It is a Java language industry standard to begin class names with an upper case letter and employ uppercase letters as need to improve readability. - ClassName

• The Java compiler expects the file name to match the same class name that you assigned at the beginning of your program.

• Java is case sensitive. – The compiler considers differently.

Page 7: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,
Page 8: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,
Page 9: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,
Page 10: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Using Curly Braces

• Programmers enclose the contents of all classes within curly braces ({ and }).

• For every opening curly brace in a java program, there must be a corresponding closing brace.

• The placement of the opening and closing curly braces is not important to the compiler.

Page 11: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Precedence of Arithmetic Operator

Operator Meaning precedence

- unary minus higest

+ unary plus higest

* multiplication middle

/ division middle

% remainder middle

+ addition or concatenation

low

- subtraction low

Page 12: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

More Arithmetic Operators

• NEVER use the lower case 'l' because it is easily confused with a digit '1'.

– 123l (last one is L) vs. 1231

Page 13: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Declaring a Variable

• Java declaration:– Variable-Type Variable-Name

• Example of declaration:– float fltDollarAmt; – int intNum = 23;

• Multiple declarations of the same data type can be made in a single de declaration:– float fltDollarAmt, fltCurrBalance, fltNewTotal;

• Multiple declarations of the different data type can NOT be made in a single de declaration:– float fltDollarAmt, int intTotal; - incorrect

Page 14: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Syntax of Variable Declaration

• Start with lower case letter• Remember: it’s case sensitive!

– TOTAL  and  total  are different names.

• Must start with a letter, dollar sign, or underscore– Do not start with a digit.

• Must contain only letters, dollar signs, underscores, or digits– Use only the characters 'a' through 'z', 'A' through 'Z',

'0' through '9', character '_', and character '$'.

• A name can not contain the space character.

Page 15: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Syntax of Variable Declaration

• A name can be any length.• A name can not be a reserved word. • A name must not already be in use in this part of

the program. – Must not be a reserved word– “Camelback” naming style:

COBOL: Current-BalanceJava: currentBalance

– Good idea to include data type in name:fltCurrentBalance

• An ending semicolon

Page 16: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Assignment Statements

• An assignment statement changes the value that is held in a variable.

public class Example5

{

public static void main ( String[] args )

{

long payAmount; //a declaration without an initial value payAmount = 123; //an assignment statement

System.out.println("The variable contains: " + payAmount );

}

}

Page 17: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Syntax of assignment Statements

• variableName = expression; – The equal sign "=" means "assignment

operator.– variableName is the name of a variable that

has been declared somewhere in the program. – expression is a collection of characters that

calls for a value.– Errors may occur if the lefthand variable is not

the same variable type that the righthand expression evaluates to.

Page 18: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Syntax of assignment Statements

• An assignment statement asks for the computer to perform two steps, in order: 1.Evaluate the expression (i.e., calculate a value.)

2.Store the value in the variable.

• For example, the assignment statement:

• sum = 32 + 8 ; asks for two actions: 1.Evaluate the Expression — 32 + 8 is calculated,

yielding 40.

2.Puts the value (40) in the variable, which is sum .

Page 19: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

The Assignment Operator

• We’ve already used this operator to initialize variables. – float fltCurrBalance = 1000.0395F;– fltNewTotal = fltCurrBalance;

• It can also be used to change the value of an existing variable.

Page 20: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Expressions

• An expression is a combination of literals, operators, variables, and parentheses used to calculate a value. This (slightly incomplete) definition needs some explanation: – literal — characters that directly mean a value, like: 3.456 – operator — a symbol like plus ("+") or times ("*") that asks

for doing arithmetic. – variable — a section of memory containing a value. – parentheses — "(" and ")".

• When the expression on the right gets complicated you need to know the two steps to figure out what happens.

Page 21: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Expressions (con’t)

• This might sound awful. Actually, this is stuff that you know from algebra, like:

• (32 - y) / ( x + 5 ) , the character "/" means "division." Not just any mess will work (of course). The following:

• 32 - y) / ( x 5 + ) is not a syntactically correct expression. There are rules for this, but the best rule is that an expression must look OK as algebra.

Page 22: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Casting

• What happens when a numeric value is assigned into a numeric variable of unlike type?

Double d

int i

i = 45; - OK because int to int

d = i; - Ok because int to double (automatic conversion – see next slide). The 45.0 is stored in d.

i = d; - Java will refuse to compile an assignment statement requiring narrowing conversion. - so need Casting

Page 23: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Casting

• Casting is the process of performing a deliberate change of data type.

• Java will automatically perform widening conversion. – fltCurrBalance = intLastBalance;– The integer will automatically be converted

to floating point.

Page 24: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Casting

• One data type can be explicitly converted to another by a programmer.

Double d

int i

i = (int) 3.14; - i equals 3

• Must be careful to use!!

Page 25: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Casting

Order of widening conversion:

byte

short

int

long

float

double

Page 26: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Increment Operator

• The increment operator ++ adds one to a variable.– counter = counter + 1 ; // add one to

counter – Counter ++

• Usually the variable is an integer type (byte, short, int, or long) but it can be a floating point type (float or double.)

• The two plus signs must not be separated. • Usually they are written immediately

adjacent to the variable.

Page 27: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

How to Use Increment Operator

• The increment operator can be used as part of an arithmetic expression, as in the following:

int sum = 0; int counter = 10; sum = counter++ ; System.out.println("sum: "+ sum " + counter: " + counter );

Page 28: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Example of Increment Operator

• The expression on the right of the = can be more complicated, as in the following fragment:

int value = 10 ;

int result = 0 ;

result = value++ * 2 ;

System.out.println("value: " + value + " result: " + result );

Page 29: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Example of Without Increment Operator

•The following is same as previous example.

int value = 10 ; int result = 0 ; result = value * 2 ; value = value + 1 ; System.out.println("value: " + value + " result: " + result );

Page 30: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Expression of Increment Operator

• The increment operator must be applied to a variable. It cannot be applied to a larger arithmetic expression. The following is incorrect:

int x = 15;

int result;

result = (x * 3 + 2)++ ; // Wrong!

Page 31: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Prefix Increment Operator

• The increment operator ++ can be put in front of a variable.

• When it is put in front of a variable (as in ++counter) it is called a prefix operator.

• When it is put behind a variable (as in counter++) it is called a postfix operator. Both ways increment the variable. However: – ++countermeans increment before using.– counter++means increment after using.

Page 32: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Decrement Operator

• The operator -- is a postfix and a prefix decrement operator. The postfix operator decrements a variable after using its value; the prefix operator increments a variable before using its value.

Expression Operation Example Result

x-- use the value, then subtract 1

int x = 10;int y;y = x-- ;

x is 9; y is 10

--x subtract 1, then use the value

int x = 10;int y;y = --x ;

x is 9; y is 9

Page 33: Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Example of Decrement Operator

int x = 99; int y = 10; y = --x ; System.out.println("x: " + x + " y: " + y );

• Advice for using Prefix and Postfix Increments and Decrements – Don’t use them always. – Sometimes they look too confusing!