JAVA PROGRAMMING PART II. CONSTANT Integer constant Real constant Boolean constant Character...

Post on 11-Jan-2016

216 views 1 download

Transcript of JAVA PROGRAMMING PART II. CONSTANT Integer constant Real constant Boolean constant Character...

JAVA PROGRAMMING

PART II

CONSTANT

Integer constant Real constant Boolean constant Character constant String constant

Constant is input data for machine get to calculate following our instructions

Integer Constants

Integer -(2-(23131+1) to 2+1) to 23131-1-1 Decimal

Oct

Hex

Long Integer -(2-(26363+1) to 2+1) to 23131-1-1Example

50000000000L,23145l,-13L

Example -897, 356, 0

Example 0700, 0356, 00

Example 0x89A, 0x356, 0x0

Integer Constants

//IntegerLiteral.java//Represent integer : Decimal, Octagonal and Hexagonalpublic class IntegerLiteral {

public static void main(String args[]) { System.out.println(12); //Show Decimal constant System.out.println(012); //Show Octagonal constant System.out.println(0x12);//Show Hexagonal constant }}

121210101818

Integer Constants

//LongConstant.java//Represent Normal Integer and Long IntegerPublic class LongConstant { public static void main (String args[]) { System.out.println(12345678901234); System.out.println(12345678901234L); }}

194289253019428925301234567890123412345678901234

Real Constants

Float -1.4 x 10-1.4 x 104545 and +3.4 x 10 and +3.4 x 103838

Double -4.9 x 10-4.9 x 10-324-324 and 1.7 x 10 and 1.7 x 10308308

Example10.2F, 3.14f, 2.35E5F, -3.14e45F

Example10.2D, -1.89d, 2.35E5D, -1.4e309D

Suffix ‘F’ or ‘f’

Suffix ‘D’ or ‘d’

Boolean Constant

2 value2 valuetruefalse

Note: all character in word is small.

Character Constants

All character represent in Unicode 16 bits represent between ‘’ single quote

Represent special character

Represent in unicode

‘a’ ‘A’ ‘z’ ‘Z’ ‘0’ ‘9’ ‘β’

‘\t’ ‘\b’ ‘\n’ ‘\f’ ‘\r’ ‘\’’ ‘\”’ ‘\\’

\u0061

\u0041

\u007a

\u005a

\u0030

\u0039

\u00a7

‘a’ ‘A’ ‘z’ ‘Z’ ‘0’ ‘9’ ‘β’

Escape sequence

//CtrlChar.javaPublic class CtrlChar { public static void main(String args[]) { System.out.println(“backspace = <\b>”); System.out.println(“new line = <\n>”); System.out.println(“return = <\r>”); System.out.println(“formfeed = <\f>”); System.out.println(“tab = <\t>”); System.out.println(“backslash = <\\>”); System.out.println(“single quote = <\’>”); System.out.println(“double quote = <\”>”); System.out.println(“null = <\0>”); }}

String Constants

It is sequence of characters Represent between “” (double quote)

Example

“Hello World” Hello World

“Good Morning.\n Sir.”

Good Morning.

Sir.

“a and \u0061” a and a

“I said \”yes\”.” I said “yes”.

Variables

type size Default Contains

byte 8 bits 0 Signed integer

short 16 bits 0 Signed integer

int 32 bits 0 Signed integer

long 64 bits 0 Signed integer

float 32 bits 0.0 IEEE754 floating point

double 64 bits 0.0 IEEE754 floating point

boolean 1 bits false (true,false)

char 16 bits \u0000 Unicode Character

Identifiers

Not allow Keywords Blank space

Allow follow symbol Upper Case (‘A’..’Z’) Lower Case (‘a’..’z’) Number (‘0’..’9’) Other symbol ‘$’:dollar-sign ‘_’:underscore

Keywords

abstract, boolean, break, byte, case, catch, abstract, boolean, break, byte, case, catch, char, class, const, continue, default, do, char, class, const, continue, default, do, double, else, extends, false,final, finally, double, else, extends, false,final, finally, float, for, goto, if, implements, import, float, for, goto, if, implements, import, instanceof, int, interface, long, native, instanceof, int, interface, long, native, new, null, package, private, protected, new, null, package, private, protected, public, return, short, static, super, public, return, short, static, super, switch, synchronized, this, throw, throw, switch, synchronized, this, throw, throw, transient, true, try, void, volatile, whiletransient, true, try, void, volatile, while

Type variables

Declaration form

<Type> <identifier>[=<value>][,<identifier>[=<value>]…]

Exampleint x;float a,b;double half=0.5;

Type wrapper classes

Each primitive data type has a corresponding class in package java.lang

public class IntegerConstants { public static void main(String args[]) { System.out.println(Byte.MAX_VALUE); System.out.println(Byte.MIN_VALUE); System.out.println(Short.MAX_VALUE); System.out.println(Short.MIN_VALUE); System.out.println(Integer.MAX_VALUE); System.out.println(Integer.MIN_VALUE); System.out.println(Long.MAX_VALUE); System.out.println(Long.MIN_VALUE); }}

Operators

Assignment Operators Arithmatic Operators (Integer&Float) Arithmatic Assignment Operators Increment and Decrement Operators

Bitwise Operators Ralational Operators Logical Operators

Arithmatic Operators (Integer&Float)

Opt Integer Float

+ 1+2 3 1.0 + 2.0 3.0

- 2 - 3 -1 2.0 – 3.0 -1.0

* 3 * 4 12 3.0 * 4.0 12.0

/ 24/5 4 24.0 / 5.0 4.8

% 16%7 3 16.0 % 7.0 3.0

Arithmatic Assignment Operators

Form Meaning

+= x+=y x = x + y

-= x-=y x = x – y

*= x*=y x = x * y

/= x/=y x = x / y

%= x%=y x = x % y

Increment and Decrement Operators

Increment Operator is ++ Decrement Operator is – x++ and ++x have different in meaning

int x = 10;int y;y = ++x;System.out.println(x);System.out.println(y);

int x = 10;int y;y = x++;System.out.println(x);System.out.println(y);

11111111

11111010

Overflow and Underflow

public class FloatRange { public static void main(String args[]){ float pMax = Float.MAX_VALUE; float pMin = Float.MIN_VALUE; System.out.println(pMin + “ to ” + pMax); System.out.println(“Overflow ” + pMax*10); System.out.println(“Underflow ” + pMin/10); }}

Bitwise Operators

& A & B &= A&=B A = A&B

| A | B |= A|=B A = A|B

^ A ^ B ^= A^=B A = A^B

>> A >> B >>= A>>=B A = A>>B

<< A << B <<= A<<=B A = A<<B

~ ~A

>>> A >>> B >>>= A>>>=B

A = A>>>B

Ralational Operators

Result is either “true” or “false”

Operator Example Meaning

> A > B more than

< A < B Less then

>= A >= B More than and equal

<= A <= B Less than and equal

== A == B Equal

!= A != B Not equal

Logical Operators

Boolean Logical Operator

& b = false & (++i<10) false i = 1

| b = false | (++i<10) true i = 1

^ b = false ^ (++i<10) true i = 1

! b = !(++i<10) false i = 1

Short-circuit logical Operator

&& b = false && (++i<10) false i = 0

|| b = true || (++i<10) true i = 0

int i = 0; boolean b;

Condition Operator

If condition is true do expression 1 If condition is false do expression 2

Conditional Operator(<condition>) ? <expression 1> : <expression 2>

Example System.out.println(x==0?’0’:’1’)

Statement

Simple Statement Assignment

Condition Statement If Switch

Loop Statement While for

Assignment Statement

Assignment expression

<variable> = <expression>

ExampleX = ( a * a + 4.0 * b ) / 2.0;

Condition Statement

If Statement formula

if (<boolean expression>) <statements>;[else <statements>;]

If (<expression>) <statement1>;<statement2>;

If (<expression>) <statement1>;else <statement2>;<statement3>;

Condition Statement

Switch Statement formula

Switch (<integer expression>){ case <value> : <statements>; case <value> : <statements>; . . . . . . . . . . . . . . default: <statements>;}

Loop Statement

While statement formula

while (<boolean expression>) <statements>;

public class LoopWhileFac { public static void main (String args[]) { int n = Integer.parseInt(args[0]); int i = 1, f = 1; while (i++ < n) f *= i; System.out.println(n+”!=”+f); }}

Loop Statement

Do statement formula

do{ (< statements >) }while < boolean expression >;

public class LoopDoFac { public static void main (String args[]) { int n = Integer.parseInt(args[0]); int i = 1, f = 1; do { f *= i; }while (i++ < n) System.out.println(n+”!=”+f); }}

Loop Statement

for statement formulafor (<initial exp>;<condition exp>;<update exp>) (< statements >)

public class LoopForFac { public static void main (String args[]) { int n = Integer.parseInt(args[0]); int i = 1, f = 1; for (i = 1; f = 1; i <= n; i++) f *= i; System.out.println(n+”!=”+f); }}