30. Example : this() and super() class GParent { int a,b,c; GParent() { System.out.println("From gparent"); } GParent(int a,int b) { //this(a,b,100); this(); System.out.println("a= "+a+" b = "+ b); } GParent(int a,int b,int c) { this.a=a; this.b=b; this.c=c; System.out.println("a= "+a+" b = "+ b + " c= " +c); } } www.shareittips.com
31. Example : this() and super() class Parent extends GParent { int x,y; Parent() { System.out.println("From parent"); } Parent(int x,int y) { super(x,y); this.x=x; this.y = y; System.out.println("x= "+x+" y = "+ y); } } www.shareittips.com
32. Example : this() and super() class Child extends Parent { Child() { super(23,343); System.out.println("From child"); } } class SuperEx { public static void main(String[] a) { //Parent p = new Parent(12,23); Child d = new Child(); } } www.shareittips.com
33.
34.
35.
36. Static Variable Example class StatEx { int i=10; static int j = 20; public void normalMethod() { System.out.println("Instance var = " + i++); System.out.println("Static var = " + j++); } public static void main(String arg[]) { StatEx s1 = new StatEx(); StatEx s2 = new StatEx(); s1.normalMethod(); s2.normalMethod(); } } www.shareittips.com
37. Static Method Example class StatEx { int i=10; static int j = 20; public static void staticMethod() { //System.out.println("Instance var = " + i++); //illegal System.out.println("Static var = " + j++); } public static void main(String arg[]) { staticMethod(); staticMethod(); } } www.shareittips.com
38. Static Initializer Example class StatEx1 { static int counter; //static initializer static { counter=10; System.out.println("Static block invoked "+counter); } public static void sMethod() { System.out.println("Static method" + counter++); } } www.shareittips.com
39. Static Initializer Example class StatEx { public static void main(String arg[]) { System.out.println("from main"); StatEx1.sMethod(); StatEx1.sMethod(); } } www.shareittips.com
40.
41.
42. Primitive Data Types and Corresponding Wrapper Classes All the wrapper classes except Boolean and Character are subclasses of an abstract class called Number , whereas Boolean and Character are derived directly from the Object class. www.shareittips.com Primitive Data Type Wrapper Class Constructor Arguments boolean Boolean boolean or String byte Byte byte or String char Character char short Short short or String int Integer int or String long Long long or String float Float double or float or String double Double double or String
43.
44.
45. Methods to Extract the Wrapped Values www.shareittips.com Method Class public boolean booleanValue() Boolean public char charValue() Character public byte byteValue() Byte, Short, Integer, Long, Float, Double public short shortValue() Byte, Short, Integer, Long, Float, Double public int intValue() Byte, Short, Integer, Long, Float, Double public long longValue() Byte, Short, Integer, Long, Float, Double public float floatValue() Byte, Short, Integer, Long, Float, Double public double doubleValue() Byte, Short, Integer, Long, Float, Double
46. Methods to Convert Strings to Primitive Types www.shareittips.com Wrapper Class Method Signature Method Arguments Boolean static boolean parseBoolean(…) String Character Not available Byte static byte parseByte(…) String, or String and radix Short static short parseShort(..) String, or String and radix Integer static int parseInt(…) String, or String and radix Long static long parseLong(…) String, or String and radix Float static float parseFloat(…) String Double static double parseDouble(…) double or String
47.
48.
49.
50.
51.
52.
53. Enum Example1 enum Edge { TOP,BOTTOM,LEFT,RIGHT }; class MyClass { public static void main(String[] a) { Edge e = Edge.TOP; int i = e.ordinal(); System.out.println(e); System.out.println(i); } } www.shareittips.com
54. Enum Example2 enum Edge { TOP,BOTTOM,LEFT,RIGHT; public static void main(String[] a) { Edge e = Edge.TOP; int i = e.ordinal(); System.out.println(e); System.out.println(i); } } www.shareittips.com
55. Enum Example3 public enum Day { MONDAY(8,true), TUESDAY(8,true), WEDNESDAY(8,true), THURSDAY(8,true), FRIDAY(8,true), SATURDAY(4,false), SUNDAY(0,false); private int hours; private boolean weekday; www.shareittips.com
56. Enum Example3 Day(int whours,boolean wday) { hours=whours; wday=weekday; } public int getHours() { return hours; } public boolean isWeekDay() { return weekday; } www.shareittips.com
57. Enum Example3 public static void showDay(Day d) { if(d.isWeekDay()) { System.out.println(d +" is a weekday and has "+ d.getHours() +" hours working hours"); } else { System.out.println(d +" is a not weekday and has "+ d.getHours() +" hours working hours"); } } www.shareittips.com
58. Enum Example3 public static void main(String[] ar) { Day day; day = Day.SUNDAY; showDay(day); } } www.shareittips.com
59.
60. Example for Regular InnerClass class MyOuter { int x =7; class MyInner { public void InnerMethod() { System.out.println("x == " + x); } } public void OuterMethod() { MyInner inn = new MyInner(); inn.InnerMethod(); } www.shareittips.com
61. Example for Regular InnerClass public static void main(String[] a) { MyOuter mo = new MyOuter(); MyOuter.MyInner mi = mo.new MyInner(); mi.InnerMethod(); mo.OuterMethod(); //mi.OuterMethod(); illegal //mo.InnerMethod(); illegal } } www.shareittips.com
62.
63. Method-local inner class class MouterClass { int x =10; public void OuterMethod() { final int j=90; class MinnerClass { public void minnerMethod() { System.out.println("Hello ..." + x + j); } } MinnerClass mic = new MinnerClass(); mic.minnerMethod(); } public static void main(String[] a) { MouterClass moc = new MouterClass(); moc.OuterMethod(); } } www.shareittips.com
64.
65. Static nested class class OuterClass { static int i =10; public void method() { System.out.println("i == " + ++i); } static class InnerClass { public void display() { System.out.println("i == " + i); } } www.shareittips.com
66. Static nested class public static void main(String[] a) { OuterClass.InnerClass ic = new OuterClass.InnerClass(); ic.display(); OuterClass oc = new OuterClass(); oc.method(); } } www.shareittips.com
67.
68. Anonymous Inner Classes import java.awt.*; import java.awt.event.*; class FrameExample { private Frame f; public FrameExample() { f = new Frame("Hello .....!"); } public void launchFrame() { f.setSize(170,170); f.setBackground(Color.blue); f.setVisible(true); www.shareittips.com
69. Anonymous Inner Classes // Add a window listener f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent evt) { System.exit(0); } }); //Anonymous Inner Classes } public static void main(String args[]) { FrameExample f = new FrameExample(); f.launchFrame(); } } www.shareittips.com
70.
71.
72.
73.
74. The Exception Class Hierarchy Object Throwable Exception Error RuntimeException Others… Others… Others… www.shareittips.com
75.
76.
77. Example 1 class PrintStack { public static void main(String args[]) { int Num1= 30 , Num2 = 0; try { int Num3=Num1/Num2; } catch(ArithmeticException obj) { System.out.println("Exception"+obj); obj.printStackTrace(); } } } www.shareittips.com
78.
79.
80. Throws clause class UncheckedThrows { public void show() throws ArithmeticException { System.out.println("Hai I am not handled"); } public static void main(String[] arg) { new UncheckedThrows().show(); } } www.shareittips.com
81.
82.
83. Example 2 import java.io.*; class MyException extends Exception { MyException() { System.out.println("UserDefined Error occured"); } public String toString() { return "MyException thrown"; } } www.shareittips.com
84. Example 2 cont… class UserExceptions { public void valid() { try { String str1,str2; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter Login id"); str1=br.readLine(); System.out.println("Enter password"); str2=br.readLine(); if(str1.equals(str2)) System.out.println("Hai welcome"); else throw new MyException(); } www.shareittips.com
85. Example 2 cont … catch(MyException e) { System.out.println("Sorry U r not a valid user" + e); valid(); } catch(IOException ioe){} } public static void main(String[] arg) throws IOException { UserExceptions e1=new UserExceptions(); e1.valid(); } } www.shareittips.com
105. List import java.util.*; class ListExample { public static void main(String[] args) { List list = new ArrayList(); list.add("one"); list.add("second"); list.add("3rd"); list.add(new Integer(4)); list.add(new Float(5.0F)); list.add("second"); // duplicate, is added list.add(new Integer(4)); // duplicate, is added System.out.println(list); } } www.shareittips.com
106. Set import java.util.*; class SetExample { public static void main(String[] args) { Set set = new HashSet(); set.add("one"); set.add("second"); set.add("3rd"); set.add(new Integer(4)); set.add(new Float(5.0F)); set.add("second"); // duplicate, not added set.add(new Integer(4)); // duplicate, not added System.out.println(set); } } www.shareittips.com
107.
108.
109.
110. www.shareittips.com Class Interface Duplicates Allowed? Ordered/ Sorted Synchronized ArrayList List Yes Ordered by index Not sorted No LinkedList List Yes Ordered by index Not sorted No Vector List Yes Ordered by index Not sorted Yes HashSet Set No Not ordered Not sorted No LinkedHashSet Set No Ordered by insertion Not sorted No TreeSet Set No Sorted either by natural order or by your comparison rules No
111. www.shareittips.com Class Interface Duplicates Allowed? Ordered/ Sorted Synchronized HashMap Map No Not ordered Not sorted No LinkedHashMap Map No Ordered No Hashtable Map No Not ordered Not sorted Yes TreeMap Map No Sorted either by natural order or by your comparison rules No
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123. Thread States A thread can in one of several possible states: 1.Running Currently running In control of CPU 2.Ready to run Can run but not yet given the chance 3.Resumed Ready to run after being suspended or block 4.Suspended Voluntarily allowed other threads to run 5.Blocked Waiting for some resource or event to occur www.shareittips.com
124. Thread Priorities Why priorities? Determine which thread receives CPU control and gets to be executed first Definition: – Integer value ranging from 1 to 10 – Higher the thread priority -> larger chance of being executed first – Example: ● Two threads are ready to run ● First thread: priority of 5, already running ● Second thread = priority of 10, comes in while first thread is running www.shareittips.com
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135. keyboard monitor terminal console standard input stream standardoutput stream Streams How does information travel across? www.shareittips.com MEM CPU HDD
136. keyboard monitor terminal console standard input stream standardoutput stream file input stream LOAD READ file output stream SAVE WRITE Streams files How does information travel across? www.shareittips.com MEM CPU HDD
140. Binary vs. TextFiles www.shareittips.com pro con Binary (input &output stream) Efficient in terms of time and space Preinformation about data needed to understand content Text(reader and writer) Human readable, contains redundant information Not efficient
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161. Structure of Java5 Compiler class C { @NonNull Object field; C( @NonNull Object p) { field = p; } @NonNull Object get() { return field; } } Annotation Checker Plugins Error Error Source File Program with annotations Class File www.shareittips.com Parser Annotation Checker Class File Writer Type Checker