Java Static Variables and Operators, Slides of Java Programming

The concept of static variables in java, their memory allocation, and access methods. It also covers the use of static methods and the rules for binary operations and casting. Examples and explanations of identifiers, literals, and operators.

Typology: Slides

2011/2012

Uploaded on 07/13/2012

aim.high
aim.high 🇮🇳

4.2

(6)

48 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Java Static Variables and Operators and more Slides Java Programming in PDF only on Docsity!

(^1) docsity.com

^ Static^

Variables

(class^ data

members)

^ Non‐^ static

Variables

(class^ instance

data

members

(^2) docsity.com

4

The pi data exists and we can access it

even

when no instance

of MyMathStuff is created. We can access the data directly using the nameof the class, as in:double x = 2.0 * MyMathStuff.pi;

docsity.com

^ We^ could

also^ define

static^ methods

that^ we

can^ call^ without

instantiating

the^ class.

For

example,

5

public class MyMathStuff{ public static double pi = 3.14;public static double timesPI (double x){ return x * pi;} }

docsity.com

7

If a class property is also declaredclass MyMathStuff{ public final static double PI = 3.14;}

final^ it is referred to as a

constant^ since it cannot be altered:

docsity.com

^ An^ identifier

is^ a^ sequence

of^ characters

that^ consist

of

letters,^ digits,

underscores

(_),^ and^ dollar

signs^ ($).

^ An^ identifier

must^ start

with^ a^ letter,

an^ underscore

(_),

or^ a^ dollar

sign^ ($).^ It

cannot^ start

with^ a^ digit.

^ An^ identifier

cannot^ be^ a

reserved^ word. ^ An^ identifier

cannot^ be

true,^ false

,^ or

null.  An^ identifier

can^ be^ of^

any^ length.

(^8) docsity.com

final^ datatype

CONSTANTNAME

=^ VALUE;

final^ double

PI^ =^ 3.14159; final^ int

SIZE^ =^

3;

(^10) docsity.com

11

Name^

Range^

Storage Size

byte^

(^7) –2(-128) to 2

7 –1 (127)^

8-bit signed

short^

(^15) –2(-32768) to 2

15 –1 (32767)^

16-bit signed

int^

(^31) –2(-2147483648) to 2

31 –1 (2147483647)

32-bit signed

long^

(^63 63) –2to 2–

64-bit signed

(i.e., -9223372036854775808to^ 9223372036854775807) float^

Negative range:

32-bit IEEE 754

-3.4028235E+38 to -1.4E-45Positive range:1.4E-45 to 3.4028235E+ double^

Negative range:

64-bit IEEE 754

-1.7976931348623157E+308 to-4.9E-324Positive range:4.9E-324 to 1.7976931348623157E+

docsity.com

Calculations

involving

floating‐

point^ numbers

are

approximated

because^

these^ numbers

are^ not^ stored

with^ complete

accuracy.

For^ example,

System.out.println(1.

‐^ 0.1^ ‐^ 0.

‐^ 0.1^ ‐^ 0.

‐^ 0.1);

displays 0.5000000000000001, not 0.5, andSystem.out.println(1.

‐^ 0.9);

displays^ 0.09999999999999998,

not^ 0.1.^ Integers

are

stored^ precisely.

Therefore,

calculations

with^ integers

yield^ a^ precise

integer^ result.

(^13) docsity.com

A^ literal^ is

a^ constant

value^ that

appears^ directly

in^ the^ program.

For^ example,

34,^ 1000000,

and

5.0^ are^ literals

in^ the^ following

statements:

int i = 34;long x = 1000000;double d = 5.0;

(^14) docsity.com

16

Operator^ Name

Description

++var^ preincrement

The expression (++var) increments var by 1 and evaluatesto the

new^ value in var^

after^ the increment.

var++^ postincrement

The expression (var++) evaluates to the

original^ value in var and increments var by 1. --var^ predecrement

The expression (--var) decrements var by 1 and evaluatesto the^

new^ value in var^ after

the decrement.

var--^ postdecrement

The expression (var--) evaluates to the

original^ value in var and decrements var by 1.

docsity.com

17

int^ i^ =^ 10; int^ newNum^ =

10 *^ i++ ;^

int^ newNum^ =

10 ^ i Same effect asi^ =^ i^ +^ 1; int^ i^ =^ 10; int^ newNum^ =^10 _^ ( **++i_** );^

i^ =^ i^ +^ 1;int^ newNum^

;= 10 * i Same effect as

docsity.com

Implicit

castingdouble d^ =^ 3;^

(type^ widening) Explicit

castingint i =^ (int)3.0;

(type^ narrowing) int^ i^ =^

(int)3.9;

(Fraction

part^ is

truncated) What is wrong?

int x = 5 / 2.0;

19

byte, short, int, long, float, double

range increases

docsity.com

char letter = 'A'; (ASCII)char numChar = '4'; (ASCII)char^ letter

=^ '\u0041';

(Unicode)

char^ numChar

=^ '\u0034';

(Unicode)

20

Four hexadecimal digits.

NOTE: The increment and decrement operators can also be usedon char variables to get the next or preceding Unicode character.For example, the following statements display character b.char ch = 'a';System.out.println(++ch);

docsity.com