JAVA PROGRAMMING PART II
CONSTANT Integer constant Real constant Boolean constant Constant is input data for machine get to calculate following our instructions Integer constant Real constant Boolean constant Character constant String constant
Integer Constants Integer -(231+1) to 231-1 Decimal Oct Hex Long Integer -(263+1) to 231-1 Example -897, 356, 0 Example 0700, 0356, 00 Example 0x89A, 0x356, 0x0 Example 50000000000L,23145l,-13L
Integer Constants 12 10 18 //IntegerLiteral.java //Represent integer : Decimal, Octagonal and Hexagonal public 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 } 12 10 18
Integer Constants 1942892530 12345678901234 //LongConstant.java //Represent Normal Integer and Long Integer Public class LongConstant { public static void main (String args[]) { System.out.println(12345678901234); System.out.println(12345678901234L); } 1942892530 12345678901234
Real Constants Float -1.4 x 1045 and +3.4 x 1038 Double -4.9 x 10-324 and 1.7 x 10308 Suffix ‘F’ or ‘f’ Example 10.2F, 3.14f, 2.35E5F, -3.14e45F Suffix ‘D’ or ‘d’ Example 10.2D, -1.89d, 2.35E5D, -1.4e309D
Boolean Constant 2 value true false 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.java Public 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 Signed integer short Signed integer short 16 bits int 32 bits long 64 bits float 0.0 IEEE754 floating point double boolean 1 bits false (true,false) char \u0000 Unicode Character
Identifiers Not allow Allow follow symbol Keywords Blank space Upper Case (‘A’..’Z’) Lower Case (‘a’..’z’) Number (‘0’..’9’) Other symbol ‘$’:dollar-sign ‘_’:underscore
Keywords abstract, boolean, break, byte, case, catch, char, class, const, continue, default, do, double, else, extends, false,final, finally, float, for, goto, if, implements, import, instanceof, int, interface, long, native, new, null, package, private, protected, public, return, short, static, super, switch, synchronized, this, throw, throw, transient, true, try, void, volatile, while
Type variables Declaration form <Type> <identifier>[=<value>][,<identifier>[=<value>]…] Example int 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 Bitwise Operators Ralational 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 16.0 % 7.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); 11 11 10
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 = 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) int i = 0; boolean b; Boolean Logical Operator & b = false & (++i<10) false i = 1 | b = false | (++i<10) true ^ b = false ^ (++i<10) ! b = !(++i<10) Short-circuit logical Operator && b = false && (++i<10) i = 0 || b = true || (++i<10)
Condition Operator If condition is true do expression 1 Conditional Operator (<condition>) ? <expression 1> : <expression 2> If condition is true do expression 1 If condition is false do expression 2 Example System.out.println(x==0?’0’:’1’)
Statement Simple Statement Condition Statement Loop Statement Assignment Condition Statement If Switch Loop Statement While for
Assignment Statement Assignment expression <variable> = <expression> Example X = ( 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>; . . . . . . . . . . . . . . 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 formula for (<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); }