1 / 27

Center For Software Development Core Java Lecture 2-3 [07/09/211]

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()

caleb-mays
Download Presentation

Center For Software Development Core Java Lecture 2-3 [07/09/211]

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Center For Software Development Core JavaLecture 2-3 [07/09/211] By : Pankaj Vyas

  2. 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

  3. Topics to be Covered Today • Identifiers and Variables • Type Conversion and Casting • Automatic Type Promotion • Operators • Control Statements • Command Line Arguments • Arrays [Only Introduction]

  4. 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

  5. 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]

  6. 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: ?

  7. 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

  8. 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

  9. 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

  10. Type casting • Higher Type Converted to Lower Type (double float long int short byte) • Syntax (target_type) value; • Narrowing Conversion

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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.

  16. 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

  17. Operators • Arithmetic Operators

  18. Operators cont…. • Bitwise Operators

  19. Operators • Relational Operators

  20. Operators • Logical Operators

  21. 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

  22. 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

  23. 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

  24. 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

  25. Short hand Operators[+=,*=,/=,%=] • Syntax var <op>= expr; Result var = var <op> (expr);

  26. 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

  27. 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

More Related