270 likes | 428 Views
Center For Software Development Core Java Lecture 2-3 [07/09/211]. By : Pankaj Vyas. What We have Covered. What is an Object? What is Class ? How to Compile and Execute Your First Java Program? Displaying o/p on System.out Using System.out.println() & System.out.print()
E N D
Center For Software Development Core JavaLecture 2-3 [07/09/211] By : Pankaj Vyas
What We have Covered • What is an Object? • What is Class ? • How to Compile and Execute Your First Java Program? • Displaying o/p on System.out Using System.out.println() & System.out.print() • Java Primitive Types
Topics to be Covered Today • Identifiers and Variables • Type Conversion and Casting • Automatic Type Promotion • Operators • Control Statements • Command Line Arguments • Arrays [Only Introduction]
Identifiers • Usage – class name, method-names, variable names • Java is case sensitive. [Upper case and lower-case identifiers are not same] • Name of Identifiers may use A-Z, a-z, 0-9, _ (underscore) & $. • Rule to form Identifier name Should not start with a numeral & should not be a Java keyword • Valid Identifiers : name, age, id_number, name_of_student, $name, • Invalid : 1age, #age, id-no, class, short, try
Java Primitive Assignment Rules • byte, short and char type variables can be assigned int type value provided it is with in range. • char type value can be assigned an int type value [0 to 65,536] • boolean type can only be assigned two possible values true or false. • Java allows one primitive type value to another if and only if there is no loss of information [ Auto type conversion- Widening Conversion]
Example 1 // File Name Type.java class _Test { public static void main(String args[]) { double d1 = 10.65f; // float type to double type [Automatic Conversion] byte b1 = 12; int a1 = b1; // byte type value assigned to int type [Automatic Conversion] char x = 'a'; char x1 = 400; System.out.println("Value of x: "+x); System.out.println("Value of x1: "+x1); }// End of main() Method }// End of class Test C:\Program Files\Java\jdk1.7.0\bin>java _Test Value of x: a Value of x1: ?
Example 2 // File Name Type.java class _Test { public static void main(String args[]) { long time = 10.25f; }// End of main() Method }// End of class Test C:\Program Files\Java\jdk1.7.0\bin>javac Test.java Test.java:6: error: possible loss of precision long time = 10.25f; ^ required: long found: float 1 error
Example 3 // File Name Type.java class _Test { public static void main(String args[]) { float f1 = 10L; float f2 = 122222222222222222L; System.out.println("f1="+f1); System.out.println("f2="+f2); }// End of main() Method }// End of class Test C:\Program Files\Java\jdk1.7.0\bin>java _Test f1=10.0 f2=1.22222219E17
Example 4 // File Name Type.java class _Test { public static void main(String args[]) { short s = 10; char a = s; /* byte b = 10; char x = b; */ }// End of main() Method }// End of class Test C:\Program Files\Java\jdk1.7.0\bin>javac Test.java Test.java:8: error: possible loss of precision char a = s; ^ required: char found: short 1 error
Type casting • Higher Type Converted to Lower Type (double float long int short byte) • Syntax (target_type) value; • Narrowing Conversion
Example 1 [Type Casting] // File Name Type.java class _Test { public static void main(String args[]) { int a = 10; byte b = (byte) a; // byte b = 10; System.out.println(b); int a1 = 400; byte b1 = (byte) a; System.out.println(b1); byte b2 = (byte) 800; System.out.println(b2); byte b3 = (byte) -200; System.out.println(b3); }// End of main() Method }// End of class Test 10 10 32 56
Example 2 [Type Casting] // File Name Type.java class _Test { public static void main(String args[]) { double d1 = 12.5677777777777777777777777777; System.out.println(d1); float f1 = (float) d1; System.out.println(f1); }// End of main() Method }// End of class Test C:\Program Files\Java\jdk1.7.0\bin>java _Test 12.567777777777778 12.567778
Example 3 [Type Casting] // File Name Type.java class _Test { public static void main(String args[]) { double d1 = 12.5; System.out.println(d1); float f1 = (float) d1; System.out.println(f1); long l1 = (long) 12.25f; System.out.println(l1); }// End of main() Method }// End of class Test C:\Program Files\Java\jdk1.7.0\bin>java _Test 12.5 12.5 12
Example 4 [Type Casting] // File Name Type.java class _Test { public static void main(String args[]) { char x = 'a'; byte b = (byte) x; System.out.println(b); }// End of main() Method }// End of class Test C:\Program Files\Java\jdk1.7.0\bin>java _Test 97
Type Promotions • Consider the expression x op y [where op can be such as +, -, * ] • byte, short and char types are promoted locally to int type. • If one of the types is long and other is any of the( byte, short, char or int) types then other will be promoted to long and final result is long type. • If one of the types is float and other is any of the( byte, short, char, int or long) types then other will be promoted to long and final result is float type • If one the types is double then other will be promoted to double and the final result will be of double type.
Example 1 // File Name Type.java class _Test { public static void main(String args[]) { byte b = 10; b = b + 1; System.out.println(b); short s = 1; s = s + 1; char c = 65; c = c + 1; }// End of main() Method }// End of class Test Test.java:7: error: possible loss of precision b = b + 1; ^ required: byte found: int Test.java:11: error: possible loss of precision s = s + 1; ^ required: short found: int Test.java:14: error: possible loss of precision c = c + 1; ^ required: char found: int 3 errors
Operators • Arithmetic Operators
Operators cont…. • Bitwise Operators
Operators • Relational Operators
Operators • Logical Operators
Example 1(% Modulus Operator) // File Name Type.java class _Test { public static void main(String args[]) { System.out.println(10 % 3); System.out.println(8 % 10); System.out.println(10.5 % 3); System.out.println(8.2 % 10.5f); System.out.println(10.5 % 3.4); System.out.println(8.2 % 10.5); }// End of main() Method }// End of class Test 1 8 1.5 8.2 0.30000000000000027 8.2
Example 2[/ Division ] // File Name Type.java class _Test { public static void main(String args[]) { System.out.println(10 / 3); System.out.println(8 / 10); System.out.println(10.5 / 3); System.out.println(8.2 / 10.5f); System.out.println(10.5 / 3.4); System.out.println(8.2 / 10.5); }// End of main() Method }// End of class Test 3 0 3.5 0.7809523809523808 3.088235294117647 0.7809523809523808
Increment / Decrement Example 1 // File Name Type.java class _Test { public static void main(String args[]) { byte b = 10; System.out.println(b++); short s = 12; System.out.println(s++); char c= 65; System.out.println(++c); }// End of main() Method }// End of class Test 10 12 B
Increment / Decrement Example 2 // File Name Type.java class _Test { public static void main(String args[]) { byte b = 127; System.out.println(++b); byte b1 = -128; System.out.println(++b1); }// End of main() Method }// End of class Test -128 -127
Short hand Operators[+=,*=,/=,%=] • Syntax var <op>= expr; Result var = var <op> (expr);
Short hand OperatorExample 1 // File Name Type.java class _Test { public static void main(String args[]) { int a = 10; int b = 2; byte b1 = 10; b1 *= (a+b); System.out.println(b1); }// End of main() Method }// End of class Test 120
Short hand OperatorExample 2 // File Name Type.java class _Test { public static void main(String args[]) { byte b1 = 20; b1 *= b1; System.out.println(b1); }// End of main() Method }// End of class Test -112