150 likes | 348 Views
Topics To be Covered Today. Compiling and Executing Java Programs Java Type System Differences Between C & Java Arrays in C and Java (Only Introduction) Strings in C and Java Object class in java. Java Type System.
E N D
Topics To be Covered Today • Compiling and Executing Java Programs • Java Type System • Differences Between C & Java • Arrays in C and Java (Only Introduction) • Strings in C and Java • Object class in java
Java Type System • Type specifies set values + set of operations that you can perform with those values • Java is strongly Typed Language • Every Type in Java is one of the following: (i) Primitive Types( int, short, byte, long, char, float, double, boolean) (ii) A class Type (iii) An Interface Type (iv) An array Type (v) null type [void is not type in java]
Converts String to int if String contains an integer in String form Print prime numbers from the list of numbers passed as command Line arguments class commandPrime { public static void main(String x[]) { for(int i=0; i<x.length;i++) { int number = Integer.parseInt(x[i]); boolean flag = true; for (int j=2; j< number/2 ;j++) { if( number % j ==0) { flag = false; break; } } // End of inner for if(flag) System.out.println(" "+number); } // End of outer for } // End of main } // End of CommandPrime String[ ] x
Running of CommandPrime D:\java\bin>java commandPrime 10 20 30 13 13 D:\java\bin>java commandPrime x y 30 13 Exception in thread "main" java.lang.NumberFormatException: For input string: "x “ at java.lang.NumberFormatException. forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:447) at java.lang.Integer.parseInt(Integer.java:497) at commandPrime.main(prime.java:9) D:\java\bin>java commandPrime 30 13 x y 13 Exception in thread "main" java.lang.NumberFormatException: For input string: "x"at java.lang.NumberFormatException.forInputString (NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:447) at java.lang.Integer.parseInt(Integer.java:497) at commandPrime.main(prime.java:9)
C vs Java • Does not have statements like goto, sizeof, typedef • Does not support data types as struct,union • No Explicit Pointer Type • No auto, extern, register, signed, unsigned • Java requires that function with no return type should be explicitly declared as void void show() ; void print(); • Java adds a new operator instanceof and >>> (unsigned right shift) • Java adds a labelled break and continue statements • Return type of all conditional, logical or relational expressions is boolen in java not as integer in C.
Example D:\java\bin>javac test1.java test1.java:6: possible loss of precision found : int required: byte byte b1 = 678; ^ test1.java:8: possible loss of precision found : int required: char char y = 70000; ^ test1.java:9: possible loss of precision found : int required: char char y1 = -25; ^ test1.java:10: possible loss of precision found : int required: short short x1 = 238999; ^ test1.java:11: possible loss of precision found : double required: float float f = 678.45; ^ 5 errors class test1 { public static void main(String args[]) { byte b = 24; byte b1 = 678; char x = 45; char y = 70000; char y1 = -25; short x1 = 238999; float f = 678.45; double f1 = 56.67; } }
Example 2 In C In Java int a=10; if(10) printf("Hello"); else printf("Hi"); } int a=10; if(10) S.O.P("Hello"); else S.O.P("Hi"); } OUTPUT D:\java\bin>javac test100.java test100.java:6: incompatible types found : int required: boolean if(a) ^ 1 error OUTPUT Hello
Example 3% Operator class test101 { public static void main(String args[]) { int a=100, b=90; System.out.println(a%b); double a1= 10.56, b1 =4.67; System.out.println(a1%b1); } } D:\java\bin>java test101 10 1.2200000000000006
Example 4 >>,<<,>>> class test103 { public static void main(String args[]) { int x = -1024; System.out.println(x>>2); System.out.println(x<<2); System.out.println(x>>>2); } } D:\java\bin>java test103 -256 -4096 1073741568
Arrays in Java [Introduction] • Arrays in Java are instances of class Arrays defined in java.util.* package • Unlike in C, Java follows strict type and bounds checking • Arrays are only created dynamically. • Bounds checking is strict unlike in C. • ArrayIndexOutOfBoundsException will be thrown if bounds are violated • Every Array has an attribute named length which gives the length of array. Usage <arrayname>.length
Examples Arrays • int[ ] data = new int[40]; OR int data[ ] = new int[40]; • data is an integer array with size 40 • LB =0 UB =39 • data.length = 40 • char[ ] name =new char[40] OR • char name[ ] = new char[40]; • double[ ] values = new double[100]; • BOX[ ] boxes = new BOX[40];
Strings in Java(Introduction) • Strings in java are implemented as objects of class String( a final class in java lang package) and StringBuffer class • String objects are immutable objects i.e once created you can not change the contents of the strings. • Whenever an attempt is made to change the contents of a String reference another instance will be created with modified values • Examples : • 1. String name = “Java World”; • 2. String s1 = new String(“Objective Java”); • 3. String course =“Object Oriented Programming”; • 4. String s2 = s1; • 5. String s2 = new String(s2);
Object class in Java • Super most class in java. Super class for all the classes in java • If a class does not inherit/extends any class then it extends Object • Provides Common functionalties that can be applied to every class. boolean isEqual(Object other) String toString(Object other)
Sample Tutorial class Tut10 { public static void main(String args[]) { int a=1; System.out.println(a>>31); System.out.println(a<<31); System.out.println(a>>>31); int a1=-1; System.out.println(a1>>31); System.out.println(a1<<31); System.out.println(a1>>>31); } } D:\java\bin>java Tut10 0 -2147483648 0 -1 -2147483648 1 D:\java\bin>