Download presentation
1
JAVA PROGRAMMING PART II
2
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
3
Integer Constants Integer -(231+1) to 231-1
Decimal Oct Hex Long Integer -(263+1) to Example , 356, 0 Example , 0356, 00 Example x89A, 0x356, 0x0 Example L,23145l,-13L
4
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
5
Integer Constants 1942892530 12345678901234 //LongConstant.java
//Represent Normal Integer and Long Integer Public class LongConstant { public static void main (String args[]) { System.out.println( ); System.out.println( L); }
6
Real Constants Float -1.4 x 1045 and +3.4 x 1038
Double x 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
7
Boolean Constant 2 value true false
Note: all character in word is small.
8
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’ ‘β’
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>”); }
10
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”.
11
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
12
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
13
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
14
Type variables Declaration form
<Type> <identifier>[=<value>][,<identifier>[=<value>]…] Example int x; float a,b; double half=0.5;
15
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); }
16
Operators Assignment Operators Bitwise Operators Ralational Operators
Arithmatic Operators (Integer&Float) Arithmatic Assignment Operators Increment and Decrement Operators Bitwise Operators Ralational Operators Logical Operators
17
Arithmatic Operators (Integer&Float)
Opt Integer Float + 1+2 3 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
18
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
19
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
20
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); }
21
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
22
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
23
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)
24
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’)
25
Statement Simple Statement Condition Statement Loop Statement
Assignment Condition Statement If Switch Loop Statement While for
26
Assignment Statement Assignment expression
<variable> = <expression> Example X = ( a * a * b ) / 2.0;
27
Condition Statement If Statement formula
if (<boolean expression>) <statements>; [else <statements>;] If (<expression>) <statement1>; <statement2>; If (<expression>) <statement1>; else <statement2>; <statement3>;
28
Condition Statement Switch Statement formula
Switch (<integer expression>){ case <value> : <statements>; default: <statements>; }
29
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); }
30
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); }
31
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); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.