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

Post on 20-Dec-2015

217 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Review…

Yong Choi

BPA

CSUB

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

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

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

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.

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.

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.

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

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

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

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.

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

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

}

}

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.

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 .

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.

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.

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.

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

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.

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!!

Casting

Order of widening conversion:

byte

short

int

long

float

double

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.

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

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

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

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!

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.

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

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!

top related