├── Encapsulation └── file.txt ├── FileIOPakages ├── Source2.txt ├── Destination2.txt ├── Source1.txt ├── Destination.txt ├── BufferedInOutSource1.txt └── BufferedInOutDestination1.txt ├── InnerClasses ├── StaticInnerClass │ ├── file.txt │ ├── Example14.java │ ├── Example17.java │ ├── Example15.java │ ├── Example16.java │ └── Example18.java └── NonStaticInnerClass │ ├── NormalInnerClass │ ├── file.txt │ ├── Example9.java │ ├── Example3.java │ ├── Example1.java │ ├── Example4.java │ ├── Example6.java │ ├── Example5.java │ ├── Example2.java │ └── Example7.java │ ├── AnonymousInnerClass │ ├── file.txt │ ├── Example20.java │ ├── Example22.java │ ├── Example21.java │ └── Example19.java │ └── MethodLocalInnerClass │ ├── file.txt │ └── Example12.java ├── Scanner_Classes ├── Scanner2TextFile.txt ├── Scanner4.java ├── ScannerInfo.txt ├── Scanner1.java ├── Scanner2.java ├── Scanner3.java └── Scanner5.java ├── Collections ├── TestClass.java ├── Emp.java ├── Stud.java ├── Employee.java ├── Amazon.java ├── Flipkart.java ├── TreeMap4EmployeeComparator.java ├── Stack1.java ├── TreeMap1.java ├── TreeSet1.java ├── TreeMap2.java ├── LinkedHashMap1.java ├── HashMap1.java ├── HashMap3.java ├── ArrayList4.java ├── Collections.txt ├── HashMap2.java ├── HashSet4.java ├── ArrayList1.java ├── TreeMap3.java ├── LinkedHashSet1.java ├── LinkedHashSet3.java ├── SwiggytemNameComparable.java ├── ArrayList25.java ├── ArrayList8.java ├── Stack3.java ├── ArrayList12.java ├── HashSet2.java ├── LinkedList1.java ├── ArrayList14.java ├── ArrayList23.java ├── ArrayList5.java ├── TreeMap5.java ├── ArrayList2.java ├── ArrayList13.java ├── HashSet1.java ├── HashMap6.java ├── HashMap5.java ├── HashMap4.java ├── TreeMap6.java ├── ArrayList11.java ├── LinkedList3.java ├── SwiggyItemIdComparable.java ├── ArrayList15.java ├── ArrayList6.java ├── ArrayList7.java ├── LinkedList2.java ├── ArrayList20.java ├── HashSet3.java ├── ArrayList18.java ├── ArrayList28.java ├── LinkedHashMap3.java ├── ArrayList24.java ├── ArrayList22.java ├── TreeMap4.java ├── LinkedHashSet2.java ├── ArrayList16.java └── ArrayList17.java ├── UnicodeValueOfChar.class ├── SerializationAndDeserialization ├── StudentData.txt ├── Student.java ├── Concept.txt └── CollegeSerial.java ├── Arrays ├── Emp.java ├── Student.java ├── Example7.java ├── Example2.java ├── Example1.java ├── Example6.java ├── Example4.java ├── Example3.java ├── Example21.java ├── Example13.java ├── Example20.java ├── Example12.java ├── Example14.java ├── Example8.java ├── Example15.java ├── Example19.java ├── Example16.java ├── Example17.java ├── Example22.java ├── Example11.java ├── Example10.java └── Example9.java ├── ExceptionHandling ├── Exceptions.txt ├── ExceptionHandling.txt ├── Example35.java ├── Example26.java ├── Example5.java ├── Example21.java ├── Example37.java ├── Example2.java ├── Example4.java ├── Example6.java ├── Example17.java ├── Example22.java ├── Example25.java ├── Example1.java ├── Example8.java ├── Example20.java ├── Example32.java ├── Example27.java ├── Example9.java ├── Example12.java ├── Example34.java ├── Example29.java ├── Example28.java ├── Example30.java ├── CustomeCheckException1.java ├── Example18.java ├── CustomeUnCheckException1.java ├── Example24.java ├── Example31.java ├── CustomeCheckException2.java ├── Example10.java ├── CustomeUnCheckException2.java ├── Example19.java ├── Shadi1.java └── Shadi2.java ├── Assignent1.java ├── GreaterSmallNum.java ├── Substraction.java ├── Subtraction.java ├── Multiplication.java ├── Abstraction ├── Example8.java ├── Example10.java ├── Example3.java ├── Example11.java ├── Example12.java ├── Example1.java ├── Example13.java ├── Example2.java ├── Example4.java ├── Example14.java └── Example5.java ├── ForLoopWithoutInti.java ├── WrapperClasses ├── Example2.java ├── Example9.java ├── Example8.java ├── Example1.java ├── Example3.java ├── Example5.java ├── Example7.java ├── WrapperClass.txt ├── Example6.java └── Example4.java ├── Addition.java ├── StringClasses ├── Example16.java ├── Example23.java ├── Example24.java ├── Example14.java ├── Example1.java ├── Example20.java ├── Example26.java ├── Example17.java ├── Example21.java ├── Example6.java ├── Example25.java ├── Example27.java ├── Example4.java ├── Example15.java ├── Example8.java ├── Example11.java ├── Example22.java ├── Example12.java ├── Example28.java ├── Example2.java ├── Example18.java ├── Example7.java ├── Example5.java └── Example3.java ├── NumFromOneToFiveForLoop.java ├── StaticVarMultiClass.java ├── Constructor └── Constructor.txt ├── Non_Static_Blocks ├── Example1.java ├── Example3.java ├── Example2.java ├── Example5.java ├── Example6.java ├── Example7.java ├── Example4.java ├── Example8.java ├── Customer.java ├── Example9.java └── Example10.java ├── NumberPattern.java ├── StaticAndNonStatic ├── StaticVarMultiClass.java ├── NonStaticVMInSClass.java ├── NStaVMInNStaArea.java ├── NonStaticVMInMClass.java ├── StaticvarInNStaticArea.java ├── StaticVarMulClassAdd.java ├── StaticVMInStaticAreaInMClass.java └── StaticVarMethInStaticArea.java ├── NumFromFiveToOne.java ├── Byte1.java ├── FactorialNumUsingWhile.java ├── EvenOdd.java ├── StaticVarAssignValue.java ├── Identifier.java ├── IfElse1.java ├── IfElse2.java ├── StaticethodPassByValue.java ├── Static_Blocks ├── Example1.java ├── Example3.java ├── Example2.java ├── Assignment2.java ├── Example6.java ├── Example4.java └── Example5.java ├── StaticMethodPassByValue1.java ├── NumIsExitingBtw.java ├── StaticMethod1.java ├── StaticMPByVAdd.java ├── Inheritance ├── Example11.java ├── Example8.java ├── Example10.java ├── Example9.java ├── Example15.java ├── Example27.java ├── Example6.java ├── Example16.java ├── Example23.java ├── Example26.java ├── Example2.java ├── Example14.java ├── Example28.java ├── Example12.java ├── Example17.java ├── Example24.java └── Example1.java ├── Polymorphism ├── Compile Time Poly │ ├── Example5.java │ ├── Example6.java │ ├── Example9.java │ ├── Example1.java │ ├── Example2.java │ ├── Example10.java │ ├── Example8.java │ ├── Example7.java │ ├── Example3.java │ └── Example4.java └── RunTime Poly │ ├── Example33.java │ ├── Example24.java │ ├── Example29.java │ ├── Example30.java │ ├── Example18.java │ ├── Example17.java │ ├── Example26.java │ ├── Example25.java │ ├── Example22.java │ ├── Example21.java │ ├── Example16.java │ ├── Example11.java │ ├── Example15.java │ ├── Example12.java │ └── Example32.java ├── StaticMethod2.java ├── FinalKeywordInSwitch.java ├── Interfaces ├── Example27.java ├── Example21.java ├── Example25.java ├── Example22.java ├── Example24.java ├── Example26.java ├── Example28.java ├── Example15.java ├── Example30.java ├── Example29.java └── Example20.java ├── StarPattern3.java ├── StarPattern5.java ├── StaticVarAdd.java ├── StarPattern6.java ├── StarPattern.java ├── SignalSimulationSwitch.java ├── StarPattern4.java ├── ContinueStatement.java ├── AgeCheckUsingIfElseIf.java ├── ForloopCheck.java ├── TypesOfAccessVar.java ├── SwitchMatchesUnicodeV.java ├── StarPatternOpposite.java ├── StarPattern7.java ├── MaleFemaleAgeCheck.java ├── StaticVarMulClassAdd.java ├── StarPattern8.java ├── PrimeNumber.java ├── StarPattern10.java ├── StarPattern9.java └── SwapNumber.java /Encapsulation/file.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /FileIOPakages/Source2.txt: -------------------------------------------------------------------------------- 1 | This is the FileREader class. -------------------------------------------------------------------------------- /FileIOPakages/Destination2.txt: -------------------------------------------------------------------------------- 1 | This is the FileREader class. -------------------------------------------------------------------------------- /InnerClasses/StaticInnerClass/file.txt: -------------------------------------------------------------------------------- 1 | These is the Static Inner class -------------------------------------------------------------------------------- /FileIOPakages/Source1.txt: -------------------------------------------------------------------------------- 1 | This is the source1 file of program fileInOutStream. -------------------------------------------------------------------------------- /FileIOPakages/Destination.txt: -------------------------------------------------------------------------------- 1 | This is the source1 file of program fileInOutStream. -------------------------------------------------------------------------------- /FileIOPakages/BufferedInOutSource1.txt: -------------------------------------------------------------------------------- 1 | This is the text file of BufferedInOutSource1. -------------------------------------------------------------------------------- /Scanner_Classes/Scanner2TextFile.txt: -------------------------------------------------------------------------------- 1 | This is , the text, file, for the, Scanner, class2. -------------------------------------------------------------------------------- /FileIOPakages/BufferedInOutDestination1.txt: -------------------------------------------------------------------------------- 1 | This is the text file of BufferedInOutSource1. -------------------------------------------------------------------------------- /Collections/TestClass.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | public class TestClass { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /InnerClasses/NonStaticInnerClass/NormalInnerClass/file.txt: -------------------------------------------------------------------------------- 1 | These is the type of Non Static Inner class -------------------------------------------------------------------------------- /InnerClasses/NonStaticInnerClass/AnonymousInnerClass/file.txt: -------------------------------------------------------------------------------- 1 | These is the type of Non Static Inner class -------------------------------------------------------------------------------- /InnerClasses/NonStaticInnerClass/MethodLocalInnerClass/file.txt: -------------------------------------------------------------------------------- 1 | These is the type of Non Static Inner class -------------------------------------------------------------------------------- /UnicodeValueOfChar.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FarhaKousar1601/daily_java_programs/main/UnicodeValueOfChar.class -------------------------------------------------------------------------------- /SerializationAndDeserialization/StudentData.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FarhaKousar1601/daily_java_programs/main/SerializationAndDeserialization/StudentData.txt -------------------------------------------------------------------------------- /Arrays/Emp.java: -------------------------------------------------------------------------------- 1 | class Emp 2 | { 3 | int rollno; 4 | String name; 5 | 6 | Emp(int rollno , String name) 7 | { 8 | this.rollno = rollno; 9 | this.name=name; 10 | } 11 | } -------------------------------------------------------------------------------- /ExceptionHandling/Exceptions.txt: -------------------------------------------------------------------------------- 1 | An Exception can be defined as during the compilation or execution 2 | there will be an occurance of an unexpected events this behavior is called as Exception. 3 | 4 | -------------------------------------------------------------------------------- /Arrays/Student.java: -------------------------------------------------------------------------------- 1 | class Student 2 | { 3 | int studRollNo; 4 | String studName; 5 | Student(int studRollNo,String studName) 6 | { 7 | this.studRollNo=studRollNo; 8 | this.studName=studName; 9 | } 10 | } -------------------------------------------------------------------------------- /Assignent1.java: -------------------------------------------------------------------------------- 1 | class Assignent1 2 | { 3 | public static void main(String args[]) 4 | { 5 | float z='a'; 6 | System.out.println(z); 7 | 8 | char y=97.0f; 9 | System.out.println(y); 10 | } 11 | 12 | } -------------------------------------------------------------------------------- /Collections/Emp.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | public class Emp { 4 | int empId; 5 | String empName; 6 | Emp(int empId , String empName) 7 | { 8 | this.empId=empId; 9 | this.empName=empName; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /ExceptionHandling/ExceptionHandling.txt: -------------------------------------------------------------------------------- 1 | Exceptions : Whenever they are raised it will lead to an abnormal termination 2 | and remaining statements will not get executed hence to overcome this we need to 3 | handle the exception. 4 | 5 | -------------------------------------------------------------------------------- /GreaterSmallNum.java: -------------------------------------------------------------------------------- 1 | class GreaterSmallNum 2 | { 3 | public static void main(String args[]) 4 | { 5 | int number=7; 6 | if(number>0) 7 | { 8 | System.out.println("Number is greater : "+ number); 9 | } 10 | } 11 | } -------------------------------------------------------------------------------- /Substraction.java: -------------------------------------------------------------------------------- 1 | class Substraction 2 | { 3 | public static void main(String args[]) 4 | { 5 | int number1=12; 6 | int number2=10; 7 | int result; 8 | result=number1-number2; 9 | System.out.println(result); 10 | } 11 | } -------------------------------------------------------------------------------- /Subtraction.java: -------------------------------------------------------------------------------- 1 | class Subtraction 2 | { 3 | public static void main(String args[]) 4 | { 5 | int number1=12; 6 | int number2=10; 7 | int result; 8 | result=number1-number2; 9 | System.out.println(result); 10 | } 11 | } -------------------------------------------------------------------------------- /Multiplication.java: -------------------------------------------------------------------------------- 1 | class Multiplication 2 | { 3 | public static void main(String args[]) 4 | { 5 | int number1=12; 6 | int number2=10; 7 | int result; 8 | result=number1*number2; 9 | System.out.println(result); 10 | } 11 | } -------------------------------------------------------------------------------- /Abstraction/Example8.java: -------------------------------------------------------------------------------- 1 | abstract class Example8 2 | { 3 | public static void main(String[] args) 4 | { 5 | // we can create main method or static method within the abstract class 6 | System.out.println("These is the Abstract class"); 7 | } 8 | } -------------------------------------------------------------------------------- /ForLoopWithoutInti.java: -------------------------------------------------------------------------------- 1 | class ForLoopWithoutInti 2 | { 3 | public static void main(String args[]) 4 | { 5 | int i = 1; 6 | for( ; i<=5; i++) 7 | { 8 | System.out.println(i); 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Collections/Stud.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | public class Stud 4 | { 5 | int studId; 6 | String studName; 7 | Stud(int studId ,String studName ) 8 | { 9 | this.studId=studId; 10 | this.studName=studName; 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /WrapperClasses/Example2.java: -------------------------------------------------------------------------------- 1 | class Example2 2 | { 3 | public static void main(String[] args) 4 | { 5 | Integer i=new Integer("100"); 6 | System.out.println(i); 7 | Integer ii=new Integer("Hundread"); 8 | System.out.println(ii); 9 | } 10 | } -------------------------------------------------------------------------------- /Addition.java: -------------------------------------------------------------------------------- 1 | class Addition 2 | { 3 | public static void main(String args[]) 4 | { 5 | int number1=12; 6 | int number2=10; 7 | int result; 8 | result=number1+number2; 9 | System.out.println("The result is : "+result); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /StringClasses/Example16.java: -------------------------------------------------------------------------------- 1 | class Example16 2 | { 3 | // Write a program to calculate length of string 4 | // length() 5 | public static void main(String[] args) 6 | { 7 | String str="Sahil"; 8 | System.out.println(str.length()); 9 | } 10 | } -------------------------------------------------------------------------------- /StringClasses/Example23.java: -------------------------------------------------------------------------------- 1 | class Example23 2 | { 3 | public static void main(String[] args) 4 | { 5 | int[] array={100,200,300,400}; 6 | 7 | for (int i=0;i=1;i--) // number from 5 to 1 9 | { 10 | System.out.println(i); // print number 11 | } 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /Byte1.java: -------------------------------------------------------------------------------- 1 | class Byte1 2 | { 3 | public static void main(String [] args) 4 | { 5 | byte var1=10; 6 | System.out.println(var1); 7 | 8 | //byte var2=128; Generate error, ANS : When assign a value in integral data type then by default it is integer. 9 | //System.out.println(var2); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Collections/Flipkart.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | public class Flipkart 4 | { 5 | 6 | int productId; 7 | String productName; 8 | public Flipkart(int productId, String productName) { 9 | super(); 10 | this.productId = productId; 11 | this.productName = productName; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /FactorialNumUsingWhile.java: -------------------------------------------------------------------------------- 1 | class FactorialNumUsingWhile 2 | { 3 | public static void main(String args[]) 4 | { 5 | int number=1; 6 | int factorial=1; 7 | while(number<=8) 8 | { 9 | 10 | factorial=factorial*number; 11 | number++; 12 | } 13 | System.out.println(factorial); 14 | } 15 | } -------------------------------------------------------------------------------- /WrapperClasses/Example9.java: -------------------------------------------------------------------------------- 1 | class Example9 2 | { 3 | public static void main(String[] args) 4 | { 5 | int number=new Integer(1000); 6 | System.out.println(number); 7 | 8 | } 9 | } 10 | /* 11 | Directly converting wrapper object to primitive data is Called UnBoxing. 12 | UnBoxing Operation 13 | */ -------------------------------------------------------------------------------- /EvenOdd.java: -------------------------------------------------------------------------------- 1 | class EvenOdd 2 | { 3 | public static void main(String args[]) 4 | { 5 | int number = 8; 6 | 7 | if(number%2==0) 8 | { 9 | System.out.println("The number is even : "+number); 10 | } 11 | else 12 | { 13 | System.out.println("The number is odd : "+number); 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /SerializationAndDeserialization/Concept.txt: -------------------------------------------------------------------------------- 1 | Serialization is the process to transfer the data from one where to another is called Serialization. 2 | 3 | The process converting java object into input stream format is called Serialization. 4 | 5 | The process reading the object from Network supported format is called Deserialization -------------------------------------------------------------------------------- /StaticVarAssignValue.java: -------------------------------------------------------------------------------- 1 | class StaticVarAssignValue 2 | { 3 | static int number; 4 | 5 | public static void main(String args[]) 6 | { 7 | int number2=11; 8 | 9 | StaticVarAssignValue.number=number2; // 1. Direct access 2. Class_Name using 3. Object_Creation 10 | System.out.println(number); 11 | } 12 | } -------------------------------------------------------------------------------- /Arrays/Example6.java: -------------------------------------------------------------------------------- 1 | class Example6 2 | { 3 | public static void main(String[] args) 4 | { 5 | String[] array=new String[3]; 6 | array[0]="Sahil"; 7 | array[1]="is a"; 8 | array[2]="Programmer"; 9 | for (int i=0;i<=2 ;i++) 10 | { 11 | System.out.println(array[i]); 12 | } 13 | 14 | 15 | } 16 | } -------------------------------------------------------------------------------- /Identifier.java: -------------------------------------------------------------------------------- 1 | class Identifier 2 | { 3 | public static void main(String args[]) 4 | { 5 | int a=1; 6 | int $=2; 7 | // int _=3; error 8 | 9 | System.out.println(a); 10 | System.out.println($); 11 | // System.out.println(_); 12 | float b=143.9; 13 | System.out.println(b); 14 | 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Arrays/Example4.java: -------------------------------------------------------------------------------- 1 | class Example4 2 | { 3 | public static void main(String[] args) 4 | { 5 | int[] array = new int[5]; 6 | System.out.println(array.length); // length is used for to calculate the length of array. 7 | 8 | for (int i=0;i=21) 9 | { 10 | System.out.println("Eligible for marrage..."); 11 | } 12 | else 13 | { 14 | System.out.println("Not Eligible for marrage..."); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /StringClasses/Example1.java: -------------------------------------------------------------------------------- 1 | class Example1 2 | { 3 | public static void main(String[] args) 4 | { 5 | // approach -1 : without new keyword 6 | String first="vdf"; 7 | System.out.println(first); 8 | // approach -2 : with new keyword 9 | String second = new String("College"); 10 | System.out.println(second); 11 | } 12 | } -------------------------------------------------------------------------------- /WrapperClasses/Example8.java: -------------------------------------------------------------------------------- 1 | class Example8 2 | { 3 | public static void main(String[] args) 4 | { 5 | Integer number=1000; 6 | System.out.println(number); 7 | System.out.println(number.toString()); 8 | } 9 | } 10 | /* 11 | Directly converting primitive data to wrapper object is Called Boxing. 12 | Boxing Operation 13 | */ -------------------------------------------------------------------------------- /Collections/TreeMap4EmployeeComparator.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.Comparator; 4 | 5 | public class TreeMap4EmployeeComparator implements Comparator 6 | { 7 | 8 | 9 | public int compare(Employee o1, Employee o2) { 10 | 11 | return o1.empName.compareTo(o2.empName); 12 | } 13 | 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Arrays/Example3.java: -------------------------------------------------------------------------------- 1 | class Example3 2 | { 3 | public static void main(String[] args) 4 | { 5 | int[] array=new int[3]; 6 | array[0]=10; 7 | array[1]=20; 8 | array[2]=true; // boolean cannot be converted to int 9 | System.out.println(array[0]); 10 | System.out.println(array[1]); 11 | System.out.println(array[2]); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /IfElse2.java: -------------------------------------------------------------------------------- 1 | class IfElse2 2 | { 3 | public static void main(String args[]) 4 | { 5 | int age = 24; 6 | char gender = 'M'; 7 | 8 | if(age>=21 && gender == 'M') 9 | { 10 | System.out.println("Eligible for marrage..."); 11 | } 12 | else 13 | { 14 | System.out.println("Not Eligible for marrage..."); 15 | } 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /WrapperClasses/Example1.java: -------------------------------------------------------------------------------- 1 | class Example1 2 | { 3 | public static void main(String[] args) 4 | { 5 | int a=100; 6 | Integer aa=new Integer(a); 7 | System.out.println(aa); 8 | 9 | Integer aaa=new Integer("200"); 10 | System.out.println(aaa); 11 | } 12 | } 13 | // public java.lang.Integer(int) 14 | // java.lang.String toString(); 15 | // -------------------------------------------------------------------------------- /Collections/Stack1.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.Stack; 4 | 5 | public class Stack1 { 6 | 7 | public static void main(String[] args) 8 | { 9 | Stack stack=new Stack(); 10 | stack.push(101); 11 | stack.push("oggy"); 12 | stack.push("entertainment"); 13 | stack.push(102.21); 14 | System.out.println(stack); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /StaticethodPassByValue.java: -------------------------------------------------------------------------------- 1 | class StaticethodPassByValue 2 | { 3 | 4 | static void method1(int number) // void does not return anything 5 | { 6 | System.out.println(number); 7 | } 8 | 9 | public static void main(String args[]) 10 | { 11 | int number =100; // local variable 12 | 13 | method1(100); // Access by pass-by-value 14 | 15 | } 16 | } -------------------------------------------------------------------------------- /WrapperClasses/Example3.java: -------------------------------------------------------------------------------- 1 | class Example3 2 | { 3 | public static void main(String[] args) 4 | { 5 | Float f1=new Float(202.5); 6 | System.out.println(f1); 7 | Float f2=new Float(202.5f); 8 | System.out.println(f1); 9 | Float f3=new Float("202.5"); 10 | System.out.println(f1); 11 | Character c=new Character('A'); 12 | System.out.println(c); 13 | } 14 | } -------------------------------------------------------------------------------- /Static_Blocks/Example1.java: -------------------------------------------------------------------------------- 1 | class Example1 2 | { 3 | /* 4 | Static blocks are executed during class loading time... 5 | Static blocks are executed first then main(). 6 | */ 7 | 8 | static 9 | { 10 | System.out.println("These is static block..."); 11 | } 12 | public static void main(String[] args) 13 | { 14 | System.out.println("These is Main block..."); } 15 | } -------------------------------------------------------------------------------- /StringClasses/Example20.java: -------------------------------------------------------------------------------- 1 | class Example20 2 | { 3 | // Assignment 4 | /* 5 | Use String method 6 | hint--> check the . operator --> .oprator should extact 7 | sop()--> only one extension-->xlsx 8 | sop()--> only the extension --> doc 9 | */ 10 | public static void main(String[] args) 11 | { 12 | String filename="student.xlsx"; //college.doc 13 | 14 | } 15 | } -------------------------------------------------------------------------------- /Scanner_Classes/Scanner4.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class Scanner4 4 | { 5 | public static void main(String[] args) 6 | { 7 | 8 | Scanner scan= new Scanner(System.in); //System.in accept input from console 9 | while (scan.hasNext()) 10 | { 11 | System.out.println(" "+scan.next()); 12 | } 13 | } 14 | } 15 | // public java.io.File(java.lang.String); 16 | -------------------------------------------------------------------------------- /StaticMethodPassByValue1.java: -------------------------------------------------------------------------------- 1 | class StaticMethodPassByValue1 2 | { 3 | 4 | static int method1() // it return the int value so it have the int return type 5 | { 6 | int number=22; 7 | return number; // returns the int value 8 | } 9 | 10 | public static void main(String args[]) 11 | { 12 | int number=method1(); 13 | 14 | System.out.println(number); 15 | } 16 | } -------------------------------------------------------------------------------- /Collections/TreeMap1.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.TreeMap; 4 | 5 | public class TreeMap1 { 6 | 7 | public static void main(String[] args) { 8 | TreeMap tm=new TreeMap(); 9 | tm.put(102, "oggy"); 10 | tm.put(103, "jack"); 11 | tm.put(101, "motu"); 12 | tm.put(104, "patlu"); 13 | System.out.println(tm); // print in sorted order 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Collections/TreeSet1.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.TreeSet; 4 | 5 | public class TreeSet1 { 6 | 7 | public static void main(String[] args) { 8 | // only homogeneous type data store 9 | TreeSet ts=new TreeSet(); 10 | ts.add(101); 11 | ts.add(100); 12 | ts.add(55); 13 | ts.add(150); 14 | System.out.println(ts); // print sorted order 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /StringClasses/Example26.java: -------------------------------------------------------------------------------- 1 | class Example26 2 | { 3 | public static void main(String[] args) 4 | { 5 | // StringBuilder sb="sahil"; String cannot be converted to StringBuilder 6 | StringBuilder sb1=new StringBuilder("sahil"); 7 | System.out.println(sb1); 8 | sb1.append(" shaikh"); // append() --> add another string at last/end of string 9 | System.out.println(sb1); 10 | } 11 | } -------------------------------------------------------------------------------- /NumIsExitingBtw.java: -------------------------------------------------------------------------------- 1 | class NumIsExitingBtw 2 | { 3 | public static void main(String args[]) 4 | { 5 | // check the number 3 is present in between 1 to 5 or not 6 | 7 | for(int i=1;i<=5;i++) 8 | { 9 | System.out.println(i); 10 | if(i==3) 11 | { 12 | System.out.println("Number is present in between 1 to 5 : "+i); 13 | break; 14 | } 15 | } 16 | 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /Arrays/Example21.java: -------------------------------------------------------------------------------- 1 | class Example21 2 | { 3 | public static void main(String[] args) 4 | { 5 | /* 6 | 10 20 30 7 | 40 50 60 8 | */ 9 | // 2nd approach to declare the Multidimensional array 10 | int[][] array={{10,20,30} , 11 | {40,50,60}}; 12 | 13 | System.out.println(array[0][1]); 14 | System.out.println(array[0][2]); 15 | System.out.println(array[1][2]); 16 | } 17 | } -------------------------------------------------------------------------------- /ExceptionHandling/Example26.java: -------------------------------------------------------------------------------- 1 | class Example26 2 | { 3 | public static void main(String[] args) 4 | { 5 | try 6 | { 7 | System.out.println("Inside the try block"); 8 | 9 | } 10 | finally 11 | { 12 | // Finally block will always executed... 13 | System.out.println("Inside the Finally block"); 14 | } 15 | } 16 | } 17 | /* 18 | try-catch as well as try-finally is also valid. 19 | */ -------------------------------------------------------------------------------- /StringClasses/Example17.java: -------------------------------------------------------------------------------- 1 | class Example17 2 | { 3 | 4 | public static void main(String[] args) 5 | { 6 | String str="Sahil"; 7 | //System.out.println(str.length()); 8 | if (str.length()<7) 9 | { 10 | System.out.println("String length is sufficient and username is " +str); 11 | } 12 | else 13 | { 14 | System.out.println("String length is no sufficient"); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /StringClasses/Example21.java: -------------------------------------------------------------------------------- 1 | class Example21 2 | { 3 | public static void main(String[] args) 4 | { 5 | String name="sahilshaikh"; 6 | System.out.println(name.substring(5)); // method extract string from main string 7 | System.out.println(name.substring(0,11)); 8 | } 9 | } 10 | // public java.lang.String substring(int beginIndex); 11 | // public java.lang.String substring(int beginIndex, int endIndex); -------------------------------------------------------------------------------- /Collections/TreeMap2.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.TreeMap; 4 | 5 | public class TreeMap2 { 6 | 7 | public static void main(String[] args) { 8 | 9 | TreeMap tm=new TreeMap(); 10 | tm.put(102, "oggy"); 11 | tm.put(103, "jack"); 12 | tm.put(101, "motu"); 13 | tm.put(null, "patlu"); // null key is not applicable 14 | System.out.println(tm); // Error 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /StaticMethod1.java: -------------------------------------------------------------------------------- 1 | class StaticMethod1 2 | { 3 | static void method1() 4 | { 5 | System.out.println("Inside the method1 , call by main method..."); 6 | } 7 | 8 | public static void main(String args[]) 9 | { 10 | System.out.println("Welcome... First enter in the main..."); 11 | method1(); 12 | //StaticMethod1.method1 -- applicable 13 | System.out.println("Return from method1..."); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Arrays/Example13.java: -------------------------------------------------------------------------------- 1 | class Example13 2 | { 3 | 4 | void method(String[] ss) // ss -- we provides array of String ie. s 5 | { 6 | for(String s:ss) // in s Store the value of ss 7 | { 8 | System.out.println(s); 9 | } 10 | } 11 | 12 | public static void main(String[] args) 13 | { 14 | Example13 ref1=new Example13(); 15 | String[] s={"Sahil","Shaikh","Intern"}; 16 | ref1.method(s); 17 | } 18 | } -------------------------------------------------------------------------------- /Non_Static_Blocks/Example3.java: -------------------------------------------------------------------------------- 1 | class Example3 2 | { 3 | /* 4 | Non Static block OR Instance block will execute based on how 5 | many times objects are created and number of new keywords. 6 | */ 7 | { 8 | System.out.println("Its Non Static block"); 9 | } 10 | 11 | public static void main(String[] args) 12 | { 13 | Example3 refVar1 = new Example3(); 14 | Example3 refVar2 = new Example3(); 15 | } 16 | } -------------------------------------------------------------------------------- /StaticMPByVAdd.java: -------------------------------------------------------------------------------- 1 | class StaticMPByVAdd 2 | { 3 | 4 | static float additionmethod(float number1 , int number2) 5 | { 6 | float result=number1+number2; 7 | 8 | return result; 9 | 10 | } 11 | 12 | public static void main(String args[]) 13 | { 14 | float number1=21f; 15 | int number2=1; 16 | 17 | float result=additionmethod(number1 , number2); 18 | 19 | System.out.println(result); 20 | } 21 | } -------------------------------------------------------------------------------- /ExceptionHandling/Example5.java: -------------------------------------------------------------------------------- 1 | class Example5 2 | { 3 | public static void main(String[] args) 4 | { 5 | try 6 | { 7 | System.out.println("These is the try block..."); // Execute 8 | System.out.println(10/2); // Execute 9 | } 10 | catch (ArithmeticException ae) 11 | { 12 | System.out.println("These is the catch block..."); // No Execute 13 | System.out.println(10/2); // No Execute 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /Arrays/Example20.java: -------------------------------------------------------------------------------- 1 | class Example20 2 | { 3 | public static void main(String[] args) 4 | { 5 | /* 6 | 10 20 30 7 | 40 50 60 8 | */ 9 | // 2nd approach to declare the Multidimensional array 10 | int[][] array={{10,20,30} , 11 | {40,50,60}}; 12 | 13 | System.out.println(array[0][1]); 14 | System.out.println(array[0][2]); 15 | System.out.println(array[1][2]); 16 | 17 | 18 | 19 | 20 | } 21 | } -------------------------------------------------------------------------------- /Abstraction/Example3.java: -------------------------------------------------------------------------------- 1 | abstract class One 2 | { 3 | abstract void method(); 4 | } 5 | class Two 6 | { 7 | void method() 8 | { 9 | System.out.println("These is these the normal class...."); 10 | } 11 | } 12 | class Example3 13 | { 14 | public static void main() 15 | { 16 | One ref1=new One(); // can not create the object of abstract class 17 | Two ref2= new Two(); 18 | Example3 ref3 = new Example3(); 19 | } 20 | } -------------------------------------------------------------------------------- /StringClasses/Example6.java: -------------------------------------------------------------------------------- 1 | class Example6 2 | { 3 | public static void main(String[] args) 4 | { 5 | String str1 = new String("Sahil"); 6 | System.out.println(str1); 7 | str1 =str1.concat("Shaikh"); 8 | System.out.println(str1); 9 | } 10 | } 11 | 12 | //Class does not allow its object data to be change such behavior is called Immutable 13 | // String Class are "Immutable" 14 | // public jaav.lang.String concat(java.lang.String); -------------------------------------------------------------------------------- /ExceptionHandling/Example21.java: -------------------------------------------------------------------------------- 1 | class Example21 2 | { 3 | public static void main(String[] args) 4 | { 5 | try 6 | { 7 | System.out.println("Inside the try block"); 8 | } 9 | catch (ArithmeticException ae) 10 | { 11 | System.out.println("Inside the catch block"); 12 | } 13 | finally 14 | { 15 | // Finally block will always executed... 16 | System.out.println("Inside the Finally block"); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /ExceptionHandling/Example37.java: -------------------------------------------------------------------------------- 1 | class Example37 2 | { 3 | public static void main(String[] args) 4 | { 5 | System.out.println("Array"); 6 | int[] a={10,20,30}; 7 | int[] b=new int[10000000000000]; 8 | System.out.println("Array"); 9 | } 10 | } 11 | /* 12 | Two type of Error Are present ie. 13 | 1. OutOfMemoryError public java.lang.OutOfMemoryError(); 14 | 2. StackOverFlowError public java.lang.StackOverflowError(); 15 | */ -------------------------------------------------------------------------------- /Inheritance/Example11.java: -------------------------------------------------------------------------------- 1 | class A 2 | { 3 | static int rollno= 101; // Static variables are also part of the inheritance 4 | } 5 | class B extends A 6 | { 7 | static char name='S'; // Static variables are also part of the inheritance 8 | } 9 | class Example11 10 | { 11 | public static void main(String args[]) 12 | { 13 | System.out.println(A.rollno); 14 | System.out.println(B.name); 15 | System.out.println(B.rollno); 16 | } 17 | } -------------------------------------------------------------------------------- /ExceptionHandling/Example2.java: -------------------------------------------------------------------------------- 1 | class Example2 2 | { 3 | public static void main(String[] args) 4 | { 5 | int[] a={10,20,30,40}; 6 | System.out.println(a[5]); // index is out of range 7 | } 8 | } 9 | 10 | /* 11 | Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 4 12 | at Example2.main(Example2.java:6) 13 | */ 14 | 15 | //ArrayIndexOutOfBoundsException is also Unchecked Exception -------------------------------------------------------------------------------- /ExceptionHandling/Example4.java: -------------------------------------------------------------------------------- 1 | class Example4 2 | { 3 | public static void main(String[] args) 4 | { 5 | try 6 | { 7 | System.out.println("These is the try block..."); // Execute 8 | System.out.println(10/0); // ArithmeticException NOT Execute 9 | } 10 | catch (ArithmeticException ae) 11 | { 12 | System.out.println("These is the catch block..."); // Execute 13 | System.out.println(10/2); // Execute 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /Polymorphism/Compile Time Poly/Example5.java: -------------------------------------------------------------------------------- 1 | class Example5 2 | { 3 | // Constructor name should remain same but different no. of args 4 | 5 | Example5() 6 | { 7 | System.out.println("These is the 0 args Constructor -- 1"); 8 | } 9 | 10 | Example5(int a) 11 | { 12 | System.out.println("These is the 1 args Constructor -- 2"); 13 | } 14 | public static void main(String[] args) 15 | { 16 | Example5 var = new Example5(5); 17 | } 18 | } -------------------------------------------------------------------------------- /Polymorphism/RunTime Poly/Example33.java: -------------------------------------------------------------------------------- 1 | class P 2 | { 3 | int number1 = 10; 4 | } 5 | class C extends P 6 | { 7 | int number2 = 20; 8 | } 9 | 10 | /* 11 | Variables are not part of polimorphic behavior or neither part of Polymorphism. 12 | But Variables are part of Inheritance 13 | */ 14 | class Example33 15 | { 16 | public static void main(String[] args) 17 | { 18 | P obj=new C(); 19 | System.out.println(obj.number1); 20 | } 21 | } -------------------------------------------------------------------------------- /Collections/LinkedHashMap1.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.LinkedHashMap; 4 | 5 | public class LinkedHashMap1 { 6 | 7 | public static void main(String[] args) 8 | { 9 | LinkedHashMap lhm=new LinkedHashMap(); 10 | lhm.put(101, "oggy"); 11 | lhm.put(102, "jack"); 12 | lhm.put(103, "motu"); 13 | lhm.put(104, "patlu"); 14 | System.out.println(lhm); // insertion order is maintained 15 | 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /StringClasses/Example25.java: -------------------------------------------------------------------------------- 1 | class Example25 2 | { 3 | public static void main(String[] args) 4 | { 5 | String str1="sahil shaikh is a Computer Engineering student"; 6 | System.out.println(str1); 7 | String[] str2=str1.split(" "); // it remove the blank spaces --> (" ") 8 | 9 | for (int i=0;i hm=new HashMap(); 9 | hm.put(101, "bheem"); 10 | hm.put(106, "chutaki"); 11 | hm.put(105, "jaggu"); 12 | hm.put(102, "raju"); 13 | System.out.println(hm); // print sorted order (Insertion Order is not Maintain) 14 | 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Collections/HashMap3.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.HashMap; 4 | 5 | public class HashMap3 { 6 | public static void main(String[] args) { 7 | HashMap hm=new HashMap(); 8 | hm.put(101, "bheem"); 9 | hm.put(106, "bheem"); 10 | hm.put(105, "bheem"); 11 | hm.put(102, "bheem"); // multiple duplicate values are allowed 12 | System.out.println(hm); // print sorted order (Insertion Order is not Maintain) 13 | 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /FinalKeywordInSwitch.java: -------------------------------------------------------------------------------- 1 | class FinalKeywordInSwitch 2 | { 3 | 4 | public static void main(String args[]) 5 | { 6 | int a=2; 7 | final int x=1 , y=2 , z=3; 8 | 9 | switch(a) 10 | { 11 | case x: 12 | System.out.println("Within the case x "); 13 | break; 14 | case y: 15 | System.out.println("Within the case y "); 16 | break; 17 | case z: 18 | System.out.println("Within case z "); 19 | } 20 | 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Interfaces/Example27.java: -------------------------------------------------------------------------------- 1 | interface It2 2 | { 3 | // check if we can create Constructor --> NO 4 | It2() 5 | { 6 | 7 | } 8 | // check if we can create Static block --> NO 9 | static 10 | { 11 | 12 | } 13 | // check if we can create Instance Block / Non Static block --> NO 14 | { 15 | 16 | } 17 | } 18 | // Assignment 2 19 | class Example27 implements It2 20 | { 21 | public static void main(String[] args) 22 | { 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Non_Static_Blocks/Example2.java: -------------------------------------------------------------------------------- 1 | class Example2 2 | { 3 | /* 4 | 5 | We can create multiple non static blocks. 6 | Approach of executing non static block is TOP TO BOTTOM. 7 | 8 | */ 9 | 10 | { 11 | System.out.println("Its first non static block - 1"); 12 | } 13 | 14 | { 15 | System.out.println("Its second non static block - 2"); 16 | } 17 | public static void main(String args[]) 18 | { 19 | Example2 refVar= new Example2(); 20 | } 21 | } -------------------------------------------------------------------------------- /Polymorphism/RunTime Poly/Example24.java: -------------------------------------------------------------------------------- 1 | class Parent 2 | { 3 | void carkey() 4 | { 5 | System.out.println("Give me the Key of Car..."); 6 | } 7 | } 8 | 9 | class Child extends Parent 10 | { 11 | void bikekey() 12 | { 13 | System.out.println("Give me the Key of Bike..."); 14 | } 15 | } 16 | class Example24 17 | { 18 | public static void main(String[] args) 19 | { 20 | // Upcasting 21 | Parent pp=new Child(); 22 | pp.carkey(); 23 | } 24 | } -------------------------------------------------------------------------------- /WrapperClasses/Example5.java: -------------------------------------------------------------------------------- 1 | class Example5 2 | { 3 | public static void main(String[] args) 4 | { 5 | Integer aa=Integer.valueOf(100); 6 | System.out.println(aa); 7 | System.out.println(aa.toString()); 8 | } 9 | } 10 | /* 11 | How many ways are there to convert the primitive data to wrpper object? 12 | 13 | ANS: Two ways: 14 | ie. constructor base approach 15 | valueOf() -- it is static and accept primitive data and return type of object. 16 | */ -------------------------------------------------------------------------------- /Abstraction/Example11.java: -------------------------------------------------------------------------------- 1 | abstract class Demo 2 | { 3 | Demo() 4 | { 5 | // super() by default present 6 | System.out.println("These is the Constructor of Parent class....."); 7 | } 8 | } 9 | class Example11 extends Demo 10 | { 11 | Example11() 12 | { 13 | // super() by default present 14 | System.out.println("These is the Constructor of child class....."); 15 | } 16 | public static void main(String[] args) 17 | { 18 | new Example11(); 19 | } 20 | } -------------------------------------------------------------------------------- /Interfaces/Example21.java: -------------------------------------------------------------------------------- 1 | class Demo1 extends Demo2 // Cyclic inheritance is not allow by java at class level 2 | { 3 | void m1() 4 | { 5 | System.out.println(""); 6 | } 7 | 8 | } 9 | 10 | class Demo2 extends Demo1 // Cyclic inheritance is not allow by java at class level 11 | { 12 | void m1() 13 | { 14 | System.out.println(""); 15 | } 16 | } 17 | 18 | class Example21 19 | { 20 | public static void main(String[] args) 21 | { 22 | 23 | } 24 | } -------------------------------------------------------------------------------- /WrapperClasses/Example7.java: -------------------------------------------------------------------------------- 1 | class Example7 2 | { 3 | public static void main(String[] args) 4 | { 5 | String str1="10"; // string value 6 | String str2="20"; // string value 7 | int num1=Integer.parseInt(str1); // convert string to integer 8 | int num2=Integer.parseInt(str2); // convert string to integer 9 | System.out.println(num1); // int value 10 | System.out.println(num2); // int value 11 | System.out.println(num1+num2); // 30 12 | } 13 | } -------------------------------------------------------------------------------- /Polymorphism/Compile Time Poly/Example6.java: -------------------------------------------------------------------------------- 1 | class Example6 2 | { 3 | // Constructor name should remain same but different no. of args 4 | 5 | Example6(float a) 6 | { 7 | System.out.println("These is the 1 args Constructor typr float -- 1"); 8 | } 9 | 10 | Example6(int a) 11 | { 12 | System.out.println("These is the 1 args Constructor type int -- 2"); 13 | } 14 | public static void main(String[] args) 15 | { 16 | Example6 var = new Example6(5f); 17 | } 18 | } -------------------------------------------------------------------------------- /Interfaces/Example25.java: -------------------------------------------------------------------------------- 1 | class Demo 2 | { 3 | interface It3 4 | { 5 | void method(); // Inner interface is allow 6 | // Interface within the class is allow 7 | } 8 | } 9 | 10 | class Example25 implements Demo.It3 // Access of inner interface 11 | { 12 | public void method() 13 | { 14 | System.out.println("Inside method..."); 15 | } 16 | public static void main(String[] args) 17 | { 18 | Example25 obj = new Example25(); 19 | obj.method(); 20 | } 21 | } -------------------------------------------------------------------------------- /InnerClasses/NonStaticInnerClass/NormalInnerClass/Example9.java: -------------------------------------------------------------------------------- 1 | class OuterClassA 2 | { 3 | void m1() 4 | { 5 | InnerClassB ref1=new InnerClassB(); 6 | System.out.println(ref1.a+ref1.b); 7 | } 8 | class InnerClassB 9 | { 10 | private int a=10; 11 | private float b=11.5f; 12 | 13 | } 14 | } 15 | 16 | 17 | class Example9 18 | { 19 | public static void main(String[] args) 20 | { 21 | 22 | OuterClassA ref2 = new OuterClassA(); 23 | ref2.m1(); 24 | } 25 | } -------------------------------------------------------------------------------- /Collections/ArrayList4.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class ArrayList4 { 6 | 7 | public static void main(String[] args) { 8 | // It can store only homogeneous --> <> is called canonical item 9 | ArrayList al=new ArrayList(); 10 | al.add(100); 11 | al.add(200); 12 | al.add(300); 13 | System.out.println(al); 14 | 15 | //al.add("sahil"); 16 | //System.out.println(al); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Collections/Collections.txt: -------------------------------------------------------------------------------- 1 | The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects. 2 | 3 | Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion. 4 | 5 | Java Collection means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet). -------------------------------------------------------------------------------- /Interfaces/Example22.java: -------------------------------------------------------------------------------- 1 | interface Demo1 extends Demo2 // Cyclic inheritance is not allow by java at interface level 2 | { 3 | void m1() 4 | { 5 | System.out.println(""); 6 | } 7 | 8 | } 9 | 10 | interface Demo2 extends Demo1 // Cyclic inheritance is not allow by java at interface level 11 | { 12 | void m1() 13 | { 14 | System.out.println(""); 15 | } 16 | } 17 | 18 | class Example22 19 | { 20 | public static void main(String[] args) 21 | { 22 | 23 | } 24 | } -------------------------------------------------------------------------------- /StringClasses/Example27.java: -------------------------------------------------------------------------------- 1 | import java.util.StringTokenizer; 2 | 3 | class Example27 4 | { 5 | public static void main(String[] args) 6 | { 7 | StringTokenizer st= new StringTokenizer("sahil shaikh is the computer engineering student"); 8 | while (st.hasMoreTokens()) // checks element is present or not 9 | { 10 | System.out.println(st.nextToken()); //extract the element 11 | } 12 | 13 | } 14 | } 15 | // public boolean hasMoreElements() 16 | // public Object nextElement() 17 | -------------------------------------------------------------------------------- /Arrays/Example14.java: -------------------------------------------------------------------------------- 1 | class A 2 | { 3 | } 4 | 5 | 6 | class Example14 7 | { 8 | public static void main(String[] args) 9 | { 10 | String[] s=new String[3]; // initialization of array but don't declare 11 | for(String ss:s) 12 | { 13 | System.out.println(ss); // we don't create array so it return null. 14 | 15 | } 16 | 17 | System.out.println("-----"); 18 | 19 | A[] a=new A[4]; 20 | for(A aa:a) 21 | { 22 | System.out.println(aa); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /ExceptionHandling/Example6.java: -------------------------------------------------------------------------------- 1 | class Example6 2 | { 3 | public static void main(String[] args) 4 | { 5 | int a[] ={10,20,30,40}; 6 | try 7 | { 8 | System.out.println("These is the try block..."); // Execute 9 | System.out.println(a[10]); // ArrayIndexOutOfBoundsException NO Execute 10 | } 11 | catch (ArrayIndexOutOfBoundsException aiobe) 12 | { 13 | System.out.println("These is the catch block..."); // Execute 14 | System.out.println(a[1]); // Execute 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Inheritance/Example8.java: -------------------------------------------------------------------------------- 1 | final class A // By making class final we can avoid the inheritance 2 | { 3 | void myMethod() 4 | { 5 | System.out.println("Can not be inherites these class and acces method..."); 6 | } 7 | } 8 | 9 | class B 10 | { 11 | void myTest() 12 | { 13 | System.out.println("Child Class Method..."); 14 | } 15 | } 16 | class Example8 17 | { 18 | public static void main(String args[]) 19 | { 20 | B b=new B(); 21 | b.myTest(); 22 | //b.myMethod(); 23 | } 24 | } -------------------------------------------------------------------------------- /Inheritance/Example10.java: -------------------------------------------------------------------------------- 1 | class A 2 | { 3 | static void m1() // Static variables are also part of the inheritance 4 | { 5 | System.out.println("Method m1 -- 1"); 6 | } 7 | 8 | } 9 | class B extends A 10 | { 11 | static void m2() // Static variables are also part of the inheritance 12 | { 13 | System.out.println("Method m2 -- 2"); 14 | } 15 | } 16 | class Example10 17 | { 18 | public static void main(String args[]) 19 | { 20 | A.m1(); 21 | B.m1(); 22 | B.m2(); 23 | } 24 | } -------------------------------------------------------------------------------- /Interfaces/Example24.java: -------------------------------------------------------------------------------- 1 | interface It2 2 | { 3 | interface It3 4 | { 5 | void method(); // Inner interface is allow 6 | // Interface within the interface is called inner interface 7 | } 8 | } 9 | 10 | class Example24 implements It2.It3 // Access of inner interface 11 | { 12 | public void method() 13 | { 14 | System.out.println("Inside method..."); 15 | } 16 | public static void main(String[] args) 17 | { 18 | Example24 obj = new Example24(); 19 | obj.method(); 20 | } 21 | } -------------------------------------------------------------------------------- /Polymorphism/Compile Time Poly/Example9.java: -------------------------------------------------------------------------------- 1 | class Example9 2 | { 3 | /* 4 | Java does not support the operator overloading except + operator. 5 | 6 | The + operator has more than one behavior like Addition and Concatination 7 | */ 8 | public static void main(String args[]) 9 | { 10 | System.out.println(52+65); // Perform Addition 11 | System.out.println("I am "+"Sahil Shaikh"); // Perform Concatination 12 | System.out.println("My roll num is : "+101); // Perform Concatination 13 | } 14 | } -------------------------------------------------------------------------------- /WrapperClasses/WrapperClass.txt: -------------------------------------------------------------------------------- 1 | The main purpose of wrapper class is to convert primitive data to object type. 2 | 3 | What is need for wrpper class or why do we use wrapper class? 4 | Ans: All primitive data by nature are not secure so to apply a level of security we need to wrap the data. 5 | 6 | Auto typecating: 7 | The integer wrapper class will convert primitive data or string data in the format of wrapper object such process is called type casting and as it is internally taken care by java it is called auto typecating. -------------------------------------------------------------------------------- /ExceptionHandling/Example17.java: -------------------------------------------------------------------------------- 1 | class Example17 2 | { 3 | public static void main(String[] args) 4 | { 5 | int[] a = {1,2,3,4,5}; 6 | try 7 | 8 | { 9 | System.out.println("Inside the try block --- 1"); 10 | System.out.println(a[10]); 11 | } 12 | catch (Exception e) 13 | { 14 | System.out.println(e); 15 | } 16 | } 17 | } 18 | 19 | /* 20 | toString() from Object class is Overridden in Throwable class so 21 | its behavior will change and it print exception info not address 22 | */ -------------------------------------------------------------------------------- /ExceptionHandling/Example22.java: -------------------------------------------------------------------------------- 1 | class Example22 2 | { 3 | public static void main(String[] args) 4 | { 5 | try 6 | { 7 | System.out.println("Inside the try block"); 8 | System.out.println(10/0); // Not print ArithmeticException 9 | } 10 | catch (ArithmeticException ae) 11 | { 12 | System.out.println("Inside the catch block"); 13 | } 14 | finally 15 | { 16 | // Finally block will always executed... 17 | System.out.println("Inside the Finally block"); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /ExceptionHandling/Example25.java: -------------------------------------------------------------------------------- 1 | class Example25 2 | { 3 | public static void main(String[] args) 4 | { 5 | try 6 | { 7 | System.out.println("Inside the try block"); 8 | Thread.sleep(3000); 9 | } 10 | catch (InterruptedException ie) 11 | { 12 | System.out.println("Inside the catch block"); 13 | } 14 | finally 15 | { 16 | System.out.println("Inside the finally block"); 17 | } 18 | } 19 | } 20 | /* 21 | finally block is also used for checked and unchecked exceptions. 22 | */ -------------------------------------------------------------------------------- /StringClasses/Example4.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Object 3 | { 4 | public String toString() 5 | { 6 | return "address"; 7 | } 8 | } 9 | */ 10 | 11 | class Example4 // extends Object 12 | { 13 | public String toString() 14 | { 15 | return "Example4 class"; 16 | } 17 | public static void main(String[] args) 18 | { 19 | Example4 ref = new Example4(); 20 | System.out.println(ref); // Implicitly call toString() 21 | System.out.println(ref.toString()); // Explicitly call toString() 22 | } 23 | } -------------------------------------------------------------------------------- /StarPattern3.java: -------------------------------------------------------------------------------- 1 | class StarPattern3 2 | { 3 | 4 | /* 5 | * * * * * 6 | * * * * 7 | * * * 8 | * * 9 | * 10 | */ 11 | 12 | public static void main(String args[]) 13 | { 14 | for(int column=5;column>=1;column--) // column = 5 ,4,3,2,1 15 | { 16 | for(int row=1;row<=column;row++) //Print 5 * in first line and then increase value of row means print then 4 (row=1,2,3,4,5) 17 | { 18 | System.out.print("* "); 19 | } 20 | System.out.println(); 21 | } 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Abstraction/Example12.java: -------------------------------------------------------------------------------- 1 | abstract class Demo 2 | { 3 | int number; 4 | Demo() 5 | { 6 | // super() by default present 7 | System.out.println("The value of Number is : "+number); 8 | number=10; 9 | } 10 | abstract void method(); 11 | } 12 | class Example12 extends Demo 13 | { 14 | void method() 15 | { 16 | System.out.println("The value of Number is : "+number); 17 | } 18 | public static void main(String[] args) 19 | { 20 | // nameless object creation 21 | new Example12().method(); 22 | } 23 | } -------------------------------------------------------------------------------- /Polymorphism/Compile Time Poly/Example1.java: -------------------------------------------------------------------------------- 1 | class Example1 2 | { 3 | 4 | /* 5 | To achieve method overloading the method name should 6 | remain the same but vary in no. of args 7 | */ 8 | 9 | void method() 10 | { 11 | System.out.println("These is the 0 args method - 1"); 12 | } 13 | void method(int a) 14 | { 15 | System.out.println("These is the 1 args method - 2"); 16 | } 17 | public static void main(String args[]) 18 | { 19 | Example1 ref = new Example1(); 20 | ref.method(4); 21 | } 22 | } -------------------------------------------------------------------------------- /Arrays/Example8.java: -------------------------------------------------------------------------------- 1 | class Example8 2 | { 3 | // for each loop is used to extract object. 4 | /* 5 | for(store in a variable based on data / object extracted : mention from where to extract array / contain 6 | 7 | It will extract mention data and store in variable And variable should be of same type based on data object extraction. 8 | */ 9 | public static void main(String[] args) 10 | { 11 | String[] ss={"Sahil","is a ","Programmer"}; 12 | for(String s:ss) 13 | { 14 | System.out.println(s); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Collections/HashMap2.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.HashMap; 4 | 5 | public class HashMap2 { 6 | 7 | public static void main(String[] args) { 8 | HashMap hm=new HashMap(); 9 | hm.put(101, "bheem"); 10 | hm.put(106, "chutaki"); 11 | hm.put(105, "jaggu"); 12 | hm.put(102, "raju"); 13 | hm.put(106, "Indumati"); // key duplicate --> it accept last entry's value 14 | System.out.println(hm); // print sorted order (Insertion Order is not Maintain) 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Inheritance/Example9.java: -------------------------------------------------------------------------------- 1 | class A 2 | { 3 | void myMethod() 4 | { 5 | System.out.println("Can not be inherites these class and acces method..."); 6 | } 7 | } 8 | 9 | final class B extends A // if child class make final there is no effect on the inheritance 10 | { 11 | void myTest() 12 | { 13 | System.out.println("Child Class Method..."); 14 | } 15 | } 16 | class Example9 17 | { 18 | public static void main(String args[]) 19 | { 20 | B b=new B(); 21 | b.myTest(); 22 | b.myMethod(); 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /Polymorphism/Compile Time Poly/Example2.java: -------------------------------------------------------------------------------- 1 | class Example2 2 | { 3 | 4 | /* 5 | To achieve method overloading the method name should 6 | remain the same but vary in no. of args 7 | */ 8 | 9 | void method(float a) 10 | { 11 | System.out.println("These is the 0 args method - 1"); 12 | } 13 | void method(int b) 14 | { 15 | System.out.println("These is the 1 args method - 2"); 16 | } 17 | public static void main(String args[]) 18 | { 19 | Example2 ref = new Example2(); 20 | ref.method(40); 21 | } 22 | } -------------------------------------------------------------------------------- /Polymorphism/RunTime Poly/Example29.java: -------------------------------------------------------------------------------- 1 | class ParentClass 2 | { 3 | private void m1() // Private members are not part of overring 4 | { 5 | System.out.println("Inside Private method -- M1 -- Parent"); 6 | } 7 | } 8 | 9 | class ChildClass extends ParentClass 10 | { 11 | void m1() 12 | { 13 | System.out.println("Inside Private method -- M1 -- Child"); 14 | } 15 | } 16 | class Example29 17 | { 18 | public static void main(String[] args) 19 | { 20 | ParentClass pp = new ChildClass(); 21 | pp.m1(); 22 | } 23 | } -------------------------------------------------------------------------------- /StarPattern5.java: -------------------------------------------------------------------------------- 1 | class StarPattern5 2 | { 3 | 4 | /* 5 | 6 | * 7 | * * 8 | * * * 9 | * * * * 10 | * * * * * 11 | 12 | */ 13 | 14 | public static void main(String args[]) 15 | { 16 | for (int column =5;column>=1 ;column-- ) 17 | { 18 | for (int space=1 ;space=column ;row-- ) 24 | { 25 | System.out.print("* "); 26 | } 27 | System.out.println(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /StaticVarAdd.java: -------------------------------------------------------------------------------- 1 | class StaticVarAdd 2 | { 3 | static int number1=23; 4 | static float number2=12.5f; 5 | 6 | public static void main(String args[]) 7 | { 8 | float result; 9 | 10 | result = number1+number2; // direct access 11 | System.out.println("By using direct access of static variable , the result is : "+result); 12 | 13 | result=StaticVarAdd.number1+StaticVarAdd.number2; // using class name 14 | System.out.println("By using class_name access of static variable , the result is : "+result); 15 | } 16 | } -------------------------------------------------------------------------------- /Polymorphism/Compile Time Poly/Example10.java: -------------------------------------------------------------------------------- 1 | class Example10 2 | { 3 | /* 4 | Java does not support the operator overloading except + operator. 5 | 6 | The + operator has more than one behavior like Addition and Concatination 7 | */ 8 | public static void main(String args[]) 9 | { 10 | System.out.println(20+1); // Perform Addition 11 | System.out.println(101+" I am "+"Sahil Shaikh"); // Perform Concatination 12 | System.out.println("My roll num is : "+101+" in college."); // Perform Concatination 13 | } 14 | } -------------------------------------------------------------------------------- /StarPattern6.java: -------------------------------------------------------------------------------- 1 | class StarPattern6 2 | { 3 | 4 | /* 5 | 6 | 7 | * * * * * 8 | * * * * 9 | * * * 10 | * * 11 | * 12 | 13 | */ 14 | 15 | 16 | public static void main(String args[]) 17 | { 18 | for (int column =5;column >=1 ;column-- ) 19 | { 20 | for (int space=4;space>=column ;space-- ) 21 | { 22 | System.out.print(" "); 23 | } 24 | for (int row=1;row<=column ;row++ ) 25 | { 26 | System.out.print("* "); 27 | } 28 | 29 | System.out.println(); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /Collections/HashSet4.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.HashSet; 4 | 5 | public class HashSet4 { 6 | public static void main(String[] args) { 7 | HashSet hs1=new HashSet(10,0.5f); // load Factor 8 | // default capacity is 0.75 9 | // load factor range 0.0 --> 1.0 so we need to defined the defined load factor 10 | 11 | hs1.add(10); 12 | hs1.add(20); 13 | hs1.add(30); 14 | hs1.add(40); 15 | System.out.println(hs1); // insertion order is not maintain 16 | 17 | 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Static_Blocks/Example3.java: -------------------------------------------------------------------------------- 1 | class Example3 2 | { 3 | /* 4 | Static blocks are executed during class loading time and non static blocks are executed during object creation 5 | Static blocks are execute first. 6 | */ 7 | 8 | static 9 | { 10 | System.out.println("These is static block "); 11 | } 12 | 13 | { 14 | System.out.println("These is non static block "); 15 | } 16 | public static void main(String[] args) 17 | { 18 | System.out.println("These is Main block..."); 19 | Example3 obj1= new Example3(); 20 | } 21 | } -------------------------------------------------------------------------------- /StringClasses/Example15.java: -------------------------------------------------------------------------------- 1 | class Example15 2 | { 3 | public static void main(String[] args) 4 | { 5 | String str1 = "Sahil"; 6 | String str2 = "sahil"; 7 | // CaseSensitive 8 | System.out.println(str1.compareTo(str2)); 9 | System.out.println(str1.equals(str2)); 10 | // InCaseSensitive 11 | System.out.println(str1.compareToIgnoreCase(str2)); 12 | System.out.println(str1.equalsIgnoreCase(str2)); 13 | } 14 | } 15 | // public boolean equalsIgnoreCase(java.lang.String) 16 | // public int compareToIgnoranceCase(java.lang.String) -------------------------------------------------------------------------------- /Collections/ArrayList1.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class ArrayList1 { 6 | 7 | public static void main(String[] args) 8 | { 9 | // Arraylist is a homogeneous as well as hetrogeneous data structure 10 | ArrayList al=new ArrayList(); 11 | al.add(100); 12 | al.add("sahil"); 13 | System.out.println(al); 14 | al.add(201.5f); 15 | al.add('s'); 16 | System.out.println(al); 17 | al.add(1001); 18 | al.add(null); 19 | System.out.println(al); 20 | 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Collections/TreeMap3.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.TreeMap; 4 | 5 | public class TreeMap3 { 6 | public static void main(String[] args) { 7 | TreeMap tm1=new TreeMap(); 8 | tm1.put(102, "oggy"); 9 | tm1.put(103, "jack"); 10 | tm1.put(101, "motu"); 11 | System.out.println(tm1); 12 | 13 | TreeMap tm2=new TreeMap(tm1); // add one TreeMap to another TreeMap 14 | tm2.put(106, "chingum"); 15 | tm2.put(105, "jhatka"); 16 | tm2.put(104, "ghasita"); 17 | System.out.println(tm2); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /InnerClasses/StaticInnerClass/Example14.java: -------------------------------------------------------------------------------- 1 | class OuterClassA 2 | { 3 | /* 4 | Normal Inner Class allow both static and non static member to be 5 | access to outer class members in inner class 6 | */ 7 | int number1 = 100; 8 | static float number2 = 200.5f; 9 | class InnerClassB 10 | { 11 | void m1() 12 | { 13 | System.out.println(number1+number2); 14 | } 15 | } 16 | } 17 | 18 | class Example14 19 | { 20 | public static void main(String[] args) 21 | { 22 | new OuterClassA().new InnerClassB().m1(); 23 | } 24 | } -------------------------------------------------------------------------------- /Collections/LinkedHashSet1.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.LinkedHashSet; 4 | 5 | public class LinkedHashSet1 { 6 | 7 | public static void main(String[] args) { 8 | LinkedHashSet lhs1=new LinkedHashSet(); 9 | lhs1.add(10); 10 | lhs1.add(20); 11 | lhs1.add(30); 12 | lhs1.add(40); 13 | System.out.println(lhs1); // insertion order is maintain 14 | 15 | lhs1.add(40); 16 | lhs1.add(null); // null insertion is allowed 17 | System.out.println(lhs1); // insertion order is maintain 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /ExceptionHandling/Example1.java: -------------------------------------------------------------------------------- 1 | class Example1 2 | { 3 | public static void main(String[] args) 4 | { 5 | System.out.println(10/0); 6 | } 7 | } 8 | 9 | /* 10 | Exception in thread "main" java.lang.ArithmeticException: / by zero 11 | at Example1.main(Example1.java:5) 12 | */ 13 | 14 | /* 15 | This is an Unchecked exception. 16 | The exception raised is of type Arithmetic because operation performed is of Arithmetic. 17 | 18 | It deos not check the exception but JVM checks the exception hence it is called as unchecked exception. 19 | */ -------------------------------------------------------------------------------- /Static_Blocks/Example2.java: -------------------------------------------------------------------------------- 1 | class Example2 2 | { 3 | /* 4 | Static blocks are executed during class loading time... 5 | Static blocks are executed first then main(). 6 | 7 | We create multiple static blocks & EXECUTION order is top to bottom 8 | */ 9 | 10 | static 11 | { 12 | System.out.println("These is static block - 1"); 13 | } 14 | 15 | static 16 | { 17 | System.out.println("These is static block - 2"); 18 | } 19 | public static void main(String[] args) 20 | { 21 | System.out.println("These is Main block..."); } 22 | } -------------------------------------------------------------------------------- /Arrays/Example15.java: -------------------------------------------------------------------------------- 1 | class Example15 2 | { 3 | public static void main(String[] args) 4 | { 5 | Emp[] emp=new Emp[5]; // all index are not give its value. 6 | emp[0]=new Emp(101,"Sahil"); 7 | emp[1]=new Emp(102,"Owais"); 8 | emp[2]=new Emp(103,"Navid"); 9 | 10 | for (Emp e:emp) 11 | { 12 | System.out.println(e.rollno+" "+e.name); 13 | } 14 | } 15 | } 16 | /* 17 | Exception in thread "main" java.lang.NullPointerException: Cannot read field "rollno" because "" is null 18 | at Example15.main(Example15.java:12) 19 | */ -------------------------------------------------------------------------------- /Polymorphism/Compile Time Poly/Example8.java: -------------------------------------------------------------------------------- 1 | class Student 2 | { 3 | Student(String student) 4 | { 5 | System.out.println("College student -- Sahil"); 6 | } 7 | 8 | Student(String student , String College) 9 | { 10 | System.out.println("Student name is sahil and college is -- DBATU"); 11 | } 12 | } 13 | 14 | class Example8 15 | { 16 | // Constructor name should remain same but different no. of args 17 | 18 | 19 | 20 | public static void main(String[] args) 21 | { 22 | Student var = new Student("Sahil" , "DBATU"); 23 | } 24 | } -------------------------------------------------------------------------------- /StarPattern.java: -------------------------------------------------------------------------------- 1 | class StarPattern 2 | { 3 | public static void main(String args[]) 4 | { 5 | for(int column=1;column<=5;column++) // encounter the logic for column where 1st column * star , 2nd column * * , etc 6 | { 7 | for(int row=1;row<=column;row++) // encounter the logic for row where 1st row * star , 2nd row * * , etc 8 | { 9 | System.out.print(" * "); // use only print because print all * in one line 10 | } 11 | System.out.println(); // go to the next line after completing the rows operation 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Abstraction/Example1.java: -------------------------------------------------------------------------------- 1 | abstract class Example1 /* If class contains abstract method then 2 | by default the class by nature it becomes abstract 3 | */ 4 | { 5 | abstract void method(); 6 | 7 | // to make method abstract we use the keyword "abstract" 8 | 9 | } 10 | 11 | //whenever we compile normal class or abstract class it will generates .class file. 12 | 13 | /* 14 | 15 | If class contains incomplete or abstract method such method by nature are abstract or incomplete methods. 16 | Hence class also abstract by nature 17 | 18 | */ -------------------------------------------------------------------------------- /Arrays/Example19.java: -------------------------------------------------------------------------------- 1 | class Example19 2 | { 3 | public static void main(String[] args) 4 | { 5 | /* 6 | 10 20 30 7 | 40 50 60 8 | */ 9 | // 1st approach to declare the Multidimensional array 10 | int[][] array=new int[2][3]; 11 | 12 | array[0][0]=10; 13 | array[0][1]=20; 14 | array[0][2]=30; 15 | array[1][0]=40; 16 | array[1][1]=50; 17 | array[1][2]=60; 18 | 19 | System.out.println(array[0][1]); 20 | System.out.println(array[0][2]); 21 | System.out.println(array[1][1]); 22 | 23 | 24 | 25 | 26 | } 27 | } -------------------------------------------------------------------------------- /SignalSimulationSwitch.java: -------------------------------------------------------------------------------- 1 | class SignalSimulationSwitch 2 | { 3 | public static void main(String args[]) 4 | { 5 | char signal='r'; 6 | 7 | switch(signal) 8 | { 9 | case 'r': 10 | System.out.println("Stop signal is visible..."); 11 | break; 12 | 13 | case 'g': 14 | System.out.println("You can Go Now..."); 15 | break; 16 | 17 | case 'y': 18 | System.out.println("Go slowely..."); 19 | break; 20 | 21 | default: 22 | System.out.println("Invalid signal..."); 23 | break; 24 | } 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /StarPattern4.java: -------------------------------------------------------------------------------- 1 | class StarPattern4 2 | { 3 | public static void main(String args[]) 4 | { 5 | for(int column=5;column>=1;column--) // we descrese the column because in each column * also descrease. 6 | { 7 | for(int space=5;space>=column;space--) // We need space in 2nd(1 space), 3rd(2 space),etc 8 | { 9 | System.out.print(" "); 10 | } 11 | 12 | for(int row=1;row<=column;row++) // We need 5 * in First line 13 | { 14 | System.out.print("*"); 15 | } 16 | 17 | System.out.println(); 18 | } 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /StaticAndNonStatic/NonStaticVMInSClass.java: -------------------------------------------------------------------------------- 1 | class NonStaticVMInSClass 2 | { 3 | int number = 11; 4 | void myMethod() 5 | { 6 | System.out.println("Inside the non Static method..."); 7 | } 8 | 9 | /* 10 | 11 | Non static variables , methods are access in static area in single class 12 | 1. object creation (Only one way to acces) 13 | 14 | */ 15 | 16 | public static void main(String[] args) 17 | { 18 | NonStaticVMInSClass refVar = new NonStaticVMInSClass(); 19 | System.out.println(refVar.number); 20 | refVar.myMethod(); 21 | 22 | } 23 | } -------------------------------------------------------------------------------- /Collections/LinkedHashSet3.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.Iterator; 4 | import java.util.LinkedHashSet; 5 | 6 | public class LinkedHashSet3 { 7 | 8 | public static void main(String[] args) { 9 | LinkedHashSet lhs1=new LinkedHashSet(); 10 | lhs1.add(10); 11 | lhs1.add(20); 12 | lhs1.add(30); 13 | lhs1.add(40); 14 | System.out.println(lhs1); // insertion order is not maintain 15 | 16 | Iterator ii=lhs1.iterator(); 17 | while(ii.hasNext()) 18 | { 19 | System.out.println(ii.next()); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ContinueStatement.java: -------------------------------------------------------------------------------- 1 | class ContinueStatement 2 | { 3 | public static void main(String args[]) 4 | { 5 | // Implementation of the continue statement within the for loop. 6 | 7 | for(int i=1; i<=5; i++) 8 | { 9 | if(i==3) 10 | { 11 | System.out.println("The value of i is : "+i); // print the value of i = 3 12 | continue; // it go to out of for loop for increament the value of i without printing the same value outside the if statement. 13 | } 14 | System.out.println(i); // it print 1,2 and 4,5 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Inheritance/Example15.java: -------------------------------------------------------------------------------- 1 | class Demo 2 | { 3 | Demo() 4 | { 5 | System.out.println("Constructor 0 argument of Demo -- 1"); 6 | } 7 | } 8 | 9 | class Test extends Demo 10 | { 11 | Test() 12 | { 13 | super(); /* the super() [statement or method] is used to call 14 | another constructor within the super class (Another class). 15 | */ 16 | System.out.println("Constructor 0 argument of Test -- 2"); 17 | } 18 | } 19 | class Example15 20 | { 21 | public static void main(String args[]) 22 | { 23 | Test tt = new Test(); 24 | } 25 | } -------------------------------------------------------------------------------- /Arrays/Example16.java: -------------------------------------------------------------------------------- 1 | class Example16 2 | { 3 | public static void main(String[] args) 4 | { 5 | //String str = "Sahil"; --> Yes True 6 | String str=null; // --> No False 7 | 8 | if (str instanceof String) // --> No False 9 | { 10 | System.out.println("Yes str is holding the data of type String"); 11 | } 12 | else 13 | { 14 | System.out.println("No str is not holding the data of type String"); 15 | } 16 | } 17 | } 18 | // instanceof : instanceof keyword is a binary operator used to test if an object (instance) is a subtype of a given Type. -------------------------------------------------------------------------------- /Static_Blocks/Assignment2.java: -------------------------------------------------------------------------------- 1 | class Assignment2 2 | { 3 | { 4 | // check we can create object --> NO 5 | System.out.println("These is the non static block..."); 6 | //Assignment2 obj2 = new Assignment2(); 7 | 8 | } 9 | static 10 | { 11 | //check we can create object --> YES 12 | System.out.println("These is the static block..."); 13 | Assignment2 obj3 = new Assignment2(); 14 | 15 | } 16 | public static void main(String[] args) 17 | { 18 | System.out.println("These is the main area..."); 19 | Assignment2 obj1 = new Assignment2(); 20 | } 21 | } -------------------------------------------------------------------------------- /Collections/SwiggytemNameComparable.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | public class SwiggytemNameComparable implements Comparable 4 | { 5 | 6 | int itemId; 7 | String itemName; 8 | public SwiggytemNameComparable(int itemId , String itemName) 9 | { 10 | this.itemId=itemId; 11 | this.itemName=itemName; 12 | } 13 | 14 | 15 | @Override 16 | public int compareTo(Object o) //swiggy 17 | { 18 | SwiggytemNameComparable s=(SwiggytemNameComparable)o; 19 | return itemName.compareTo(s.itemName); // return int 20 | 21 | } 22 | 23 | 24 | 25 | } 26 | -------------------------------------------------------------------------------- /InnerClasses/NonStaticInnerClass/AnonymousInnerClass/Example20.java: -------------------------------------------------------------------------------- 1 | abstract class Parent 2 | { 3 | abstract void test1(); 4 | abstract void test2(); 5 | } 6 | 7 | class Child extends Parent 8 | { 9 | void test1() 10 | { 11 | System.out.println("These is the test - 1 method..."); 12 | } 13 | void test2() 14 | { 15 | System.out.println("These is the test - 2 method..."); 16 | } 17 | } 18 | 19 | class Example20 20 | { 21 | public static void main(String[] args) 22 | { 23 | Child ref = new Child(); 24 | ref.test1(); 25 | ref.test2(); 26 | } 27 | } -------------------------------------------------------------------------------- /Interfaces/Example26.java: -------------------------------------------------------------------------------- 1 | interface It1 2 | { 3 | int number1=10; // by default it is public static final 4 | } 5 | 6 | // Assignment 1 7 | class Example26 implements It1 8 | { 9 | static void method() 10 | { 11 | System.out.println("Inside the Method and value of number is : "+number1); 12 | //number1=20; by default it is public static final so can not change the value of number1 13 | //System.out.println("Inside the Method and value of number is : "+number1); 14 | } 15 | 16 | public static void main(String[] args) 17 | { 18 | method(); 19 | } 20 | } -------------------------------------------------------------------------------- /AgeCheckUsingIfElseIf.java: -------------------------------------------------------------------------------- 1 | class AgeCheckUsingIfElseIf 2 | { 3 | public static void main(String args[]) 4 | { 5 | int age=21; 6 | char gender='F'; 7 | 8 | if(age>=21) 9 | { 10 | System.out.println("Age is Matching..."); 11 | 12 | if(gender=='M' || gender=='F') 13 | { 14 | System.out.println("Gender is Matching..."); 15 | } 16 | else 17 | { 18 | System.out.println("Gender is not Matching..."); 19 | } 20 | } 21 | else 22 | { 23 | System.out.println("Age is not MAtching...."); 24 | } 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /StringClasses/Example8.java: -------------------------------------------------------------------------------- 1 | class Example8 2 | { 3 | public static void main(String[] args) 4 | { 5 | String str1 = new String("Sahil"); 6 | System.out.println(str1); 7 | str1.concat("Shaikh"); 8 | System.out.println(str1); // String are Immutable 9 | 10 | System.out.println("============"); 11 | 12 | StringBuffer str2 = new StringBuffer("Sahil"); 13 | System.out.println(str2); 14 | str2.append("Shaikh"); // StringBuffer are Mutable 15 | System.out.println(str2); 16 | 17 | } 18 | } 19 | 20 | // public jaav.lang.StringBuffer append(java.lang.String); -------------------------------------------------------------------------------- /Collections/ArrayList25.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | 6 | public class ArrayList25 { 7 | 8 | public static void main(String[] args) 9 | { 10 | ArrayList al= new ArrayList(); 11 | al.add(100); 12 | al.add(50); 13 | al.add(400); 14 | al.add(25); 15 | System.out.println(al); 16 | Collections.sort(al); 17 | System.out.println(al); 18 | 19 | // At any point of time we can sort based on one object at a time 20 | // To overcome thise problem we go for Comparator. 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Collections/ArrayList8.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class ArrayList8 { 6 | 7 | public static void main(String[] args) 8 | { 9 | ArrayList al=new ArrayList(); 10 | 11 | al.add(101); 12 | al.add("Sahil"); 13 | al.add(250.5f); 14 | al.add(true); 15 | System.out.println(al); // Print the ArrayList 16 | 17 | System.out.println(al.contains(al)); // To check object is present or not in the ArrayList 18 | 19 | System.out.println(al.size()); // To check the size of the ArrayList 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Non_Static_Blocks/Example5.java: -------------------------------------------------------------------------------- 1 | class Example5 2 | { 3 | /* 4 | 5 | THere is no effect of construct ie. parametorized constructor or non paramatorised 6 | non static block execute first. 7 | 8 | */ 9 | 10 | { 11 | int number1 =19 , number2 = 2 , result; 12 | result = number1+number2; 13 | System.out.println(result); 14 | } 15 | 16 | 17 | Example5(int number) 18 | { 19 | System.out.println("The value pf number is in the constructor is "+ number); 20 | } 21 | public static void main(String args[]) 22 | { 23 | Example5 obj = new Example5(12); 24 | } 25 | } -------------------------------------------------------------------------------- /Interfaces/Example28.java: -------------------------------------------------------------------------------- 1 | interface It1 2 | { 3 | /* 4 | Java 8 allow to create complete method is in the interface 5 | Hence within interface we can create both static and non static complete methods. 6 | 7 | Static methods is in interface are not allow to be inherited. 8 | */ 9 | static void method() 10 | { 11 | System.out.println("Inside the method of Interface"); 12 | } 13 | } 14 | 15 | class Example28 //implements It1 Static methods is in interface are not allow to be inherited. 16 | { 17 | public static void main(String[] args) 18 | { 19 | It1.method(); 20 | } 21 | } -------------------------------------------------------------------------------- /Interfaces/Example15.java: -------------------------------------------------------------------------------- 1 | interface Demo // interface keyword is used for interfaces class 2 | { 3 | void method(); // All methods in interfaces are incomplete , complete methods can not be allow 4 | } 5 | class Example15 implements Demo // while we compile it , it creates the both .class files 6 | { 7 | public void method() // public because give the acccessibility more to child than parent 8 | { 9 | System.out.println("These is the Interfaces Method....."); 10 | } 11 | public static void main(String[] args) 12 | { 13 | Example15 obj = new Example15(); 14 | obj.method(); 15 | } 16 | } -------------------------------------------------------------------------------- /Non_Static_Blocks/Example6.java: -------------------------------------------------------------------------------- 1 | class Example6 2 | { 3 | 4 | 5 | 6 | 7 | { 8 | int number1 =19 , number2 = 2 , result; 9 | result = number1+number2; 10 | System.out.println("Non static block "+result); 11 | } 12 | 13 | Example6() 14 | { 15 | System.out.println("0 Parameter constructor"); 16 | } 17 | 18 | Example6(int number) 19 | { 20 | System.out.println("The value pf number is in the constructor is "+ number); 21 | } 22 | public static void main(String[] args) 23 | { 24 | Example6 obj1 =new Example6(); 25 | Example6 obj2 =new Example6(2002); 26 | } 27 | } -------------------------------------------------------------------------------- /Abstraction/Example13.java: -------------------------------------------------------------------------------- 1 | abstract class Demo1 2 | { 3 | int number; 4 | { 5 | System.out.println("These is the instance block "+number); 6 | number= 100; 7 | } 8 | Demo1() 9 | { 10 | // super() by default present 11 | System.out.println("The value of Number is : "+number); 12 | number=10; 13 | } 14 | void method() 15 | { 16 | System.out.println("Method And value is : "+number); 17 | } 18 | } 19 | class Example13 extends Demo1 20 | { 21 | 22 | public static void main(String[] args) 23 | { 24 | // nameless object creation 25 | new Example13().method(); 26 | } 27 | } -------------------------------------------------------------------------------- /Arrays/Example17.java: -------------------------------------------------------------------------------- 1 | class Example17 2 | { 3 | public static void main(String[] args) 4 | { 5 | Emp[] emp=new Emp[5]; 6 | emp[0]=new Emp(101,"Sahil"); 7 | emp[1]=new Emp(102,"Owais"); 8 | emp[2]=new Emp(103,"Navid"); 9 | 10 | for (Emp ss:emp) 11 | { 12 | if (ss instanceof Emp) // instanceof : instanceof keyword is a binary operator used to test if an object (instance) is a subtype of a given Type. 13 | { 14 | System.out.println(ss.rollno+" "+ss.name); 15 | } 16 | else 17 | { 18 | System.out.println("Not value present : "+ss); 19 | } 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /Collections/Stack3.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.Stack; 4 | 5 | public class Stack3 6 | { 7 | public static void main(String[] args) { 8 | Stack stack=new Stack(); 9 | stack.push("doggyDon"); 10 | stack.push("cochroches"); 11 | stack.push("bhaiyaa"); 12 | System.out.println(stack); 13 | 14 | //search(); returns the position number of the element in the stack 15 | System.out.println(stack.search("doggyDon")); // 3 16 | System.out.println(stack.search("bhaiyaa")); // 1 17 | System.out.println(stack.search("cochroches")); // 2 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /ForloopCheck.java: -------------------------------------------------------------------------------- 1 | class ForloopCheck 2 | { 3 | 4 | public static void main(String args[]) 5 | { 6 | 7 | int variable = 5; 8 | 9 | System.out.println(variable); // print original value [5] 10 | System.out.println(++variable); // in pre-Increment , it increment value first and print incremented value [6] 11 | System.out.println(variable); // print upcoming value [6] 12 | System.out.println(variable++); // first of all print the previous value and in next statement it print incremented value [6] 13 | System.out.println(variable); // Print Incremented value [7] 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Inheritance/Example27.java: -------------------------------------------------------------------------------- 1 | class Demo1 2 | { 3 | // static block executes first in the top to bottom manner. Precedance is gives to parent class 4 | static 5 | { 6 | System.out.println("This is the Static Block - 1 - Demo1"); 7 | } 8 | // call default constructor and default super() 9 | } 10 | class Test1 extends Demo1 11 | { 12 | static 13 | { 14 | System.out.println("This is the Static Block - 1 - Test1"); 15 | } 16 | } 17 | // call default constructor and default super() 18 | class Example27 19 | { 20 | public static void main(String args[]) 21 | { 22 | new Test1(); 23 | } 24 | } -------------------------------------------------------------------------------- /Inheritance/Example6.java: -------------------------------------------------------------------------------- 1 | // Multiple Inheritance 2 | // 3 | 4 | class A2 5 | { 6 | void m1() 7 | { 8 | System.out.println("Method m1"); 9 | } 10 | } 11 | class B2 12 | { 13 | System.out.println("Method m1"); 14 | } 15 | class C2 extends A2 , B2 // saparated by coma is not supported by java 16 | // Only one CLASS can single java class extends or inherites 17 | { 18 | System.out.println("Child class"); 19 | } 20 | class Example6 21 | { 22 | public static void main(String args[]) 23 | { 24 | System.out.println("Multiple inheritance does not support by java..."); 25 | } 26 | } -------------------------------------------------------------------------------- /StringClasses/Example11.java: -------------------------------------------------------------------------------- 1 | class Example11 2 | { 3 | public static void main(String[] args) 4 | { 5 | // Convert String to StringBuffer --> We can use Constructor base Approach 6 | 7 | String str = "Sahil"; //Immutable 8 | System.out.println(str); 9 | StringBuffer sb1 = new StringBuffer(str); //Immutable 10 | System.out.println(sb1); 11 | sb1.append("Shaikh"); 12 | System.out.println(sb1); 13 | 14 | } 15 | } 16 | /* 17 | String to StringBuffer -- > Constructor 18 | public jaav.lang.StringBuffer(java.lang.String); 19 | public jaav.lang.StringBuffer append(java.lang.String); 20 | */ -------------------------------------------------------------------------------- /TypesOfAccessVar.java: -------------------------------------------------------------------------------- 1 | class TypesOfAccessVar 2 | { 3 | 4 | static int number = 5; 5 | public static void main(String args[]) 6 | { 7 | // There are the 3 types of access of the variable... 8 | // 1.direct access 2.class_name 3.object_creation 9 | 10 | 11 | System.out.println("These is the direct access type , number is : "+number); //1.direct access 12 | 13 | System.out.println("These is the class name type of access , and number is : "+TypesOfAccessVar.number); //2.class_name 14 | 15 | // Object creation we will learn in the class and object section 16 | 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /ExceptionHandling/Example8.java: -------------------------------------------------------------------------------- 1 | class Example8 2 | { 3 | public static void main(String[] args) 4 | { 5 | try 6 | { 7 | System.out.println("These is the try block..."); 8 | System.out.println(10/2); 9 | } 10 | // It is manditory to use the catch block if try block is successfull or not an exception in that. 11 | 12 | } 13 | } 14 | 15 | /* 16 | Example8.java:5: error: 'try' without 'catch', 'finally' or resource declarations 17 | try 18 | ^ 19 | */ 20 | 21 | // It is manditory to use the catch block if try block is successfull or not an exception in that. -------------------------------------------------------------------------------- /InnerClasses/StaticInnerClass/Example17.java: -------------------------------------------------------------------------------- 1 | class OuterClassA 2 | { 3 | /* 4 | Static Inner Class only allow static member to be 5 | access to outer class members in inner class 6 | */ 7 | int number1 = 100; 8 | static float number2 = 200.5f; 9 | static class InnerClassB 10 | { 11 | static void m1() 12 | { 13 | OuterClassA ref2 = new OuterClassA(); 14 | System.out.println(number2); 15 | System.out.println(ref2.number1); 16 | } 17 | } 18 | } 19 | 20 | class Example17 21 | { 22 | public static void main(String[] args) 23 | { 24 | OuterClassA.InnerClassB.m1(); 25 | } 26 | } -------------------------------------------------------------------------------- /Non_Static_Blocks/Example7.java: -------------------------------------------------------------------------------- 1 | class Example7 2 | { 3 | 4 | { 5 | int number1 =19 , number2 = 2 , result; 6 | result = number1+number2; 7 | System.out.println("Non static block "+result); 8 | } 9 | 10 | Example7() 11 | { 12 | 13 | System.out.println("0 Parameter constructor"); 14 | } 15 | 16 | Example7(int number) 17 | { 18 | this(); // transfer the control to another constructor 19 | System.out.println("The value pf number is in the constructor is "+ number); 20 | } 21 | public static void main(String[] args) 22 | { 23 | Example7 obj1 =new Example7(12); 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /Polymorphism/Compile Time Poly/Example7.java: -------------------------------------------------------------------------------- 1 | class Example7 2 | { 3 | // Constructor name should remain same but different no. of args 4 | 5 | Example7(float a) 6 | { 7 | System.out.println("These is the 1 args Constructor typr float -- 1"); 8 | } 9 | 10 | Example7(int a) 11 | { 12 | System.out.println("These is the 1 args Constructor type int -- 2"); 13 | } 14 | Example7(int a , float b) 15 | { 16 | System.out.println("These is the 1 args Constructor type int and float -- 3"); 17 | } 18 | public static void main(String[] args) 19 | { 20 | Example7 var = new Example7(4 , 5f); 21 | } 22 | } -------------------------------------------------------------------------------- /Arrays/Example22.java: -------------------------------------------------------------------------------- 1 | class Example22 2 | { 3 | public static void main(String[] args) 4 | { 5 | int[][] array={{10,20,30} , 6 | {40,50,60}}; 7 | 8 | System.out.println("Length of 1D array is : "+array.length); //2 9 | System.out.println("Length of 2D array is : "+array[0].length); 10 | 11 | for (int i=0;iarray.length 23 | //Length of 2D array is : -->array[0].length -------------------------------------------------------------------------------- /Scanner_Classes/Scanner1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class Scanner1 4 | { 5 | public static void main(String[] args) 6 | { 7 | String str= "My name is Sahil and I'm student of Computer engineering"; 8 | Scanner sc= new Scanner(str); 9 | 10 | //the scanner class break information in the form of tokens and once 11 | //token into Scanner we have to check token is present or not if present 12 | //then we need to extract it. For these we use method hasNext() 13 | 14 | while (sc.hasNext()) 15 | { 16 | System.out.println(sc.next()); // for extract token use method next() 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Scanner_Classes/Scanner2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.io.File; 3 | import java.io.FileNotFoundException; 4 | class Scanner2 5 | { 6 | public static void main(String[] args) throws FileNotFoundException 7 | { 8 | File ff=new File("D:\\Programming\\cybersuccess\\ScannerClass\\Scanner2TextFile.txt"); // read the content from file 9 | Scanner scan= new Scanner(ff); 10 | while (scan.hasNext()) 11 | { 12 | System.out.print(" "+scan.next()); 13 | } 14 | } 15 | } 16 | // public java.io.File(java.lang.String); 17 | // public java.util.Scanner(java.io.File) throws java.io.FileNotFoundException -------------------------------------------------------------------------------- /SwitchMatchesUnicodeV.java: -------------------------------------------------------------------------------- 1 | class SwitchMatchesUnicodeV 2 | { 3 | public static void main(String args[]) 4 | { 5 | int a =97; 6 | 7 | switch(a) 8 | { 9 | case 10: 10 | System.out.println("Inside case 1 "); 11 | break; 12 | 13 | case 'a': 14 | System.out.println("It checks the unicode value of a = 97 and print these case..."); 15 | break; 16 | 17 | case 'b': 18 | System.out.println("It check the unicode value also..."); 19 | break; 20 | default: 21 | System.out.println("Print default statement if none of case match"); 22 | break; 23 | } 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /Collections/ArrayList12.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class ArrayList12 { 6 | 7 | public static void main(String[] args) 8 | { 9 | // generic collection --> no need to upcast and downcast 10 | 11 | ArrayList al=new ArrayList(); 12 | al.add(new Emp(101,"sahil")); 13 | 14 | //1st approach --> for each loop 15 | for(Emp e:al) 16 | { 17 | System.out.println(e.empId+" - "+e.empName); 18 | } 19 | 20 | // 2nd approach --> get(index) 21 | Emp e=al.get(0); 22 | System.out.println(e.empId+" - "+e.empName); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Collections/HashSet2.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.HashSet; 4 | import java.util.Iterator; 5 | 6 | public class HashSet2 { 7 | public static void main(String[] args) { 8 | HashSet hs=new HashSet(); 9 | hs.add(10); 10 | hs.add(20); 11 | hs.add(30); 12 | hs.add(40); 13 | System.out.println(hs); // insertion order is not maintain 14 | 15 | // Only Cursor Iterator is applicable for HashSet 16 | Iterator i=hs.iterator(); // Only Cursor Iterator is applicable for HashSet 17 | while(i.hasNext()) 18 | { 19 | System.out.println(i.next()); 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Collections/LinkedList1.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.LinkedList; 4 | 5 | public class LinkedList1 { 6 | 7 | public static void main(String[] args) 8 | { 9 | LinkedList ll=new LinkedList(); 10 | ll.add(100); 11 | ll.add(244); 12 | ll.add("motu"); 13 | System.out.println(ll); 14 | 15 | ll.add(100); // dupliacte objects are allowed 16 | ll.add("patlu"); 17 | System.out.println(ll); 18 | 19 | ll.add(null); // null insertion is possible 20 | ll.add(null); 21 | System.out.println(ll); 22 | 23 | // maintain the insertion order 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /InnerClasses/StaticInnerClass/Example15.java: -------------------------------------------------------------------------------- 1 | class OuterClassA 2 | { 3 | /* 4 | Static Inner Class only allow static member to be 5 | access to outer class members in inner class 6 | */ 7 | int number1 = 100; 8 | static float number2 = 200.5f; 9 | static class InnerClassB 10 | { 11 | void m1() 12 | { 13 | System.out.println(number2); 14 | //System.out.println(number1); Compilation Error 15 | } 16 | } 17 | } 18 | 19 | class Example15 20 | { 21 | public static void main(String[] args) 22 | { 23 | OuterClassA.InnerClassB ref = new OuterClassA.InnerClassB(); 24 | ref.m1(); 25 | } 26 | } -------------------------------------------------------------------------------- /StaticAndNonStatic/NStaVMInNStaArea.java: -------------------------------------------------------------------------------- 1 | 2 | class Demo 3 | { 4 | float number=100.5f; 5 | void myMethod() 6 | { 7 | System.out.println(); 8 | System.out.println("Inside the static method and access by non static area and then main() in multiple classes..."); 9 | } 10 | } 11 | 12 | 13 | class NStaVMInNStaArea 14 | { 15 | 16 | void accessMethod() 17 | { 18 | Demo obj1 = new Demo(); 19 | System.out.println(obj1.number); 20 | obj1.myMethod(); 21 | } 22 | public static void main(String[] args) 23 | { 24 | NStaVMInNStaArea obj2 = new NStaVMInNStaArea(); 25 | obj2.accessMethod(); 26 | } 27 | } -------------------------------------------------------------------------------- /Polymorphism/Compile Time Poly/Example3.java: -------------------------------------------------------------------------------- 1 | class Example3 2 | { 3 | // To acheive overloading there is sufficient one class 4 | void method1() 5 | { 6 | System.out.println("These is the 0 args method - 1"); 7 | } 8 | void method1(int a) 9 | { 10 | System.out.println("These is the 1 args method - 2"); 11 | } 12 | void method1(float a , int b) 13 | { 14 | System.out.println("These is the 2 args method - 3"); 15 | } 16 | 17 | public static void main(String args[]) 18 | { 19 | Example3 ref = new Example3(); 20 | ref.method1(); 21 | ref.method1(1); 22 | ref.method1(4f , 4); 23 | } 24 | } -------------------------------------------------------------------------------- /Scanner_Classes/Scanner3.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.io.FileInputStream; 3 | import java.io.FileNotFoundException; 4 | 5 | class Scanner3 6 | { 7 | public static void main(String[] args) throws FileNotFoundException 8 | { 9 | FileInputStream ff=new FileInputStream("D:\\Programming\\cybersuccess\\ScannerClass\\Scanner2TextFile.txt"); 10 | Scanner scan= new Scanner(ff); 11 | while (scan.hasNext()) 12 | { 13 | System.out.print(" "+scan.next()); 14 | } 15 | } 16 | } 17 | // public java.io.File(java.lang.String); 18 | // public java.util.Scanner(java.io.File) throws java.io.FileNotFoundException -------------------------------------------------------------------------------- /StringClasses/Example22.java: -------------------------------------------------------------------------------- 1 | class Example22 2 | { 3 | public static void main(String[] args) 4 | { 5 | String str1=" sahil shaikh "; 6 | System.out.println("Original String is : "+str1); 7 | System.out.println("length of string is : "+str1.length()); 8 | String str2=str1.trim(); // remove the blank spaces from left and right side 9 | System.out.println(str2); 10 | System.out.println(str2.length()); //return length of string 11 | 12 | String str3="sahil shaikh"; 13 | System.out.println(str3); 14 | System.out.println(str3.length()); 15 | System.out.println(str3.trim()); 16 | 17 | 18 | 19 | } 20 | } -------------------------------------------------------------------------------- /ExceptionHandling/Example20.java: -------------------------------------------------------------------------------- 1 | class Example20 2 | { 3 | public static void main(String[] args) 4 | { 5 | int[] a = {1,2,3,4,5}; 6 | try 7 | { 8 | System.out.println("Inside the try block --- 1"); 9 | System.out.println(10/0); 10 | System.out.println(a[10]); 11 | } 12 | catch(Throwable e) 13 | { 14 | System.out.println("Inside Throwable Exception..."); 15 | } 16 | 17 | } 18 | } 19 | 20 | /* 21 | Rather than using multiple catch blocks it is always better to use a single catch block 22 | 23 | As a single catch block can either handle exception of type "Throwable" or type exception. 24 | */ -------------------------------------------------------------------------------- /Interfaces/Example30.java: -------------------------------------------------------------------------------- 1 | interface It1 2 | { 3 | int number1=10; 4 | } 5 | 6 | interface It2 7 | { 8 | int number1=20; 9 | } 10 | /* 11 | static members are not part of inheritance with respect to INTERFACE. 12 | 13 | In both variables of an interface having same name then is confusing 14 | hence while accessing both static variables in 2 different interface 15 | will prefer to access them with the help of interface 16 | */ 17 | class Example30 implements It1 , It2 18 | { 19 | public static void main(String[] args) 20 | { 21 | System.out.println(It1.number1); 22 | System.out.println(It2.number1); 23 | } 24 | } -------------------------------------------------------------------------------- /StarPatternOpposite.java: -------------------------------------------------------------------------------- 1 | class StarPatternOpposite 2 | { 3 | 4 | /* 5 | * 6 | ** 7 | *** 8 | **** 9 | ***** 10 | Print the Above pattern 11 | */ 12 | 13 | public static void main(String args[]) 14 | { 15 | for (int column=1;column<=5 ;column++ ) 16 | { 17 | for (int space=4;space >=column;space--) // to print the space before the * 18 | { 19 | System.out.print(" "); 20 | } 21 | 22 | for (int row=1;row<=column ;row++ ) // to print the * at last of space 23 | { 24 | System.out.print("*"); 25 | 26 | } 27 | System.out.println(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Collections/ArrayList14.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | 6 | public class ArrayList14 { 7 | 8 | public static void main(String[] args) { 9 | ArrayList al=new ArrayList(); 10 | al.add("A"); 11 | al.add("B"); 12 | al.add("C"); 13 | System.out.println(al); 14 | 15 | // Iterator is a universal cursor applicable to all class 16 | Iterator i=al.iterator(); // cursor 17 | while(i.hasNext()) // check element present or not 18 | { 19 | String s=(String)i.next(); // extract element 20 | System.out.println(s); 21 | } 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Collections/ArrayList23.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class ArrayList23 { 6 | 7 | public static void main(String[] args) 8 | { 9 | // Convert Collection to array 10 | 11 | ArrayList al= new ArrayList(); 12 | al.add("motu"); 13 | al.add("patlu"); 14 | al.add("jhataka"); 15 | al.add("ghasita"); 16 | System.out.println(al); 17 | 18 | String[] ss=al.toArray(new String[al.size()]); // method 19 | // specify the size of array al.size() 20 | 21 | for(String s:ss) 22 | { 23 | System.out.println(s); 24 | } 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /InnerClasses/NonStaticInnerClass/NormalInnerClass/Example3.java: -------------------------------------------------------------------------------- 1 | class OuterClassA 2 | { 3 | int number1 = 10; 4 | class InnerClassB 5 | { 6 | float number2 = 12.5f; 7 | void method1() 8 | { 9 | System.out.println(number1+number2); 10 | } 11 | } 12 | } 13 | 14 | 15 | 16 | class Example3 17 | { 18 | public static void main(String[] args) 19 | { 20 | // Normal Way object creation 21 | OuterClassA ref1 = new OuterClassA(); 22 | OuterClassA.InnerClassB ref2 = ref1.new InnerClassB(); 23 | ref2.method1(); 24 | 25 | // Standard object creation 26 | new OuterClassA().new InnerClassB().method1(); 27 | } 28 | } -------------------------------------------------------------------------------- /Arrays/Example11.java: -------------------------------------------------------------------------------- 1 | class Example11 2 | { 3 | public static void main(String[] args) 4 | { 5 | Emp[] array= new Emp[3]; // create and array of Emp 6 | array[0]=new Emp(101,"Sahil"); /* constructor of Emp class there is need to create an 7 | non static variables and store the current object address also means (this) keyword is used */ 8 | array[0]=new Emp(102,"Owai"); 9 | array[0]=new Emp(103,"Navid"); 10 | 11 | 12 | 13 | for (Emp ee : array) // Store the array index in variable type class 14 | { 15 | 16 | System.out.println(ee.rollno+"-"+ee.name); // call constructor local variable. 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Collections/ArrayList5.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class ArrayList5 { 6 | 7 | public static void main(String[] args) { 8 | 9 | // By using the generic data we do not need to use instanceof and no need to upcasting and downcating 10 | 11 | ArrayList al=new ArrayList(); 12 | al.add(new Emp(101,"sahil")); 13 | al.add(new Emp(102,"jon")); 14 | al.add(new Emp(103,"Deo")); 15 | 16 | //System.out.println(al); not extract the data it print the address 17 | 18 | for(Emp ee:al) { 19 | System.out.println(ee.empId+" - "+ee.empName); 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Collections/TreeMap5.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.SortedMap; 4 | import java.util.TreeMap; 5 | 6 | public class TreeMap5 { 7 | public static void main(String[] args) 8 | { 9 | TreeMap tm=new TreeMap(); 10 | tm.put(103, "oggy"); 11 | tm.put(102, "jack"); 12 | tm.put(105, "motu"); 13 | tm.put(101, "ghasita"); 14 | tm.put(104, "chingum"); 15 | 16 | System.out.println(tm); 17 | 18 | SortedMap sm=tm.headMap(103); 19 | System.out.println(sm); // print all before 103 20 | 21 | SortedMap sm2=tm.tailMap(103); 22 | System.out.println(sm2); // print all after and with 103 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /InnerClasses/StaticInnerClass/Example16.java: -------------------------------------------------------------------------------- 1 | class OuterClassA 2 | { 3 | /* 4 | Static Inner Class only allow static member to be 5 | access to outer class members in inner class 6 | */ 7 | int number1 = 100; 8 | static float number2 = 200.5f; 9 | static class InnerClassB 10 | { 11 | void m1() 12 | { 13 | OuterClassA ref2 = new OuterClassA(); 14 | System.out.println(number2); 15 | System.out.println(ref2.number1); 16 | } 17 | } 18 | } 19 | 20 | class Example16 21 | { 22 | public static void main(String[] args) 23 | { 24 | OuterClassA.InnerClassB ref = new OuterClassA.InnerClassB(); 25 | ref.m1(); 26 | } 27 | } -------------------------------------------------------------------------------- /WrapperClasses/Example6.java: -------------------------------------------------------------------------------- 1 | class Example6 2 | { 3 | public static void main(String[] args) 4 | { 5 | Float aa=(100f); 6 | Integer bb=(200); 7 | System.out.println(aa); 8 | System.out.println(bb); 9 | int i=aa.intValue(); 10 | int ii=bb.intValue(); 11 | System.out.println(i); 12 | System.out.println(ii); 13 | 14 | 15 | //System.out.println(ii.toString()); //CE but not occur 16 | } 17 | } 18 | /* 19 | xxxxxvalue() ---> fiveX 20 | xxxxx ---> byte , short , int , double , float 21 | 22 | These method is rrsponsible to convert a Wrapper object to primitive data 23 | 24 | intValue() ---> int type(Primitive data) 25 | */ -------------------------------------------------------------------------------- /Collections/ArrayList2.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class ArrayList2 { 6 | 7 | public static void main(String[] args) 8 | { 9 | // creating a ArrayList 10 | ArrayList al=new ArrayList(); 11 | System.out.println(al); 12 | al.add(100); // int --> Integer --> toString() 13 | al.add(100.5f); // float --> Float --> toString() 14 | System.out.println(al); 15 | System.out.println("-------------"); 16 | 17 | 18 | TestClass tc=new TestClass(); 19 | al.add(tc); // tc--> ref --> toString --> Object --> address 20 | System.out.println(al); // address 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /ExceptionHandling/Example32.java: -------------------------------------------------------------------------------- 1 | class Example32 2 | { 3 | public static void main(String[] args) 4 | { 5 | System.out.println(10/0); 6 | try 7 | { 8 | System.out.println("Inside the try block"); 9 | } 10 | catch (ArithmeticException ae) 11 | { 12 | System.out.println("Inside the catch block"); 13 | } 14 | finally 15 | { 16 | System.out.println("Inside the finally block"); 17 | } 18 | } 19 | } 20 | /* 21 | Exception in thread "main" java.lang.ArithmeticException: / by zero 22 | at Example32.main(Example32.java:5) 23 | 24 | If an exception is raised outside try block the then finally block never executed. 25 | */ -------------------------------------------------------------------------------- /StaticAndNonStatic/NonStaticVMInMClass.java: -------------------------------------------------------------------------------- 1 | class AnotherClass 2 | { 3 | int number = 11; 4 | void myMethod() 5 | { 6 | System.out.println("Inside the non Static method and declare/intialize in one class and access by another class..."); 7 | } 8 | } 9 | 10 | 11 | class NonStaticVMInMClass 12 | { 13 | /* 14 | 15 | Non static variables , methods are access in static area in Multiple class 16 | 1. object creation (Only one way to acces) 17 | 18 | */ 19 | 20 | public static void main(String[] args) 21 | { 22 | AnotherClass refVar = new AnotherClass(); 23 | System.out.println(refVar.number); 24 | refVar.myMethod(); 25 | 26 | } 27 | } -------------------------------------------------------------------------------- /WrapperClasses/Example4.java: -------------------------------------------------------------------------------- 1 | class Example4 2 | { 3 | public static void main(String[] args) 4 | { 5 | Integer aa=(100); // in latest java version these is way to declare the Integer Wrapper class 6 | 7 | //Integer aa=new Integer(100); in java 1.8 is perfect 8 | 9 | System.out.println(aa); 10 | System.out.println(aa.toString()); //The toString() from wrapper class is responsible to print the VALUE of wrapper object. 11 | } 12 | } 13 | /* 14 | The toString() is overridden in all Wrapper classes. 15 | The toString() from wrapper class is responsible to print the value of wrapper object. 16 | toString() is called from wrapper object class. 17 | */ -------------------------------------------------------------------------------- /Inheritance/Example16.java: -------------------------------------------------------------------------------- 1 | class Demo 2 | { 3 | Demo() 4 | { 5 | System.out.println("Constructor 0 argument of Demo -- 1"); 6 | } 7 | } 8 | 9 | class Test extends Demo 10 | { 11 | Test() 12 | { 13 | /* the super() [statement or method] is used to call 14 | another constructor within the super class (Another class). 15 | 16 | If we do not add the super() then compiler add it automatically while compiling 17 | */ 18 | System.out.println("Constructor 0 argument of Test -- 2"); 19 | } 20 | } 21 | class Example16 22 | { 23 | public static void main(String args[]) 24 | { 25 | new Test(); // nameless object creation 26 | } 27 | } -------------------------------------------------------------------------------- /ExceptionHandling/Example27.java: -------------------------------------------------------------------------------- 1 | class Example27 2 | { 3 | public static void main(String[] args) 4 | { 5 | try 6 | { 7 | System.out.println("Inside the try block"); 8 | System.out.println(10/0); 9 | 10 | } 11 | finally 12 | { 13 | // Finally block will always executed... 14 | System.out.println("Inside the Finally block"); 15 | } 16 | } 17 | } 18 | /* 19 | try-catch as well as try-finally is also valid. 20 | 21 | try-finally block can not handle the exception it result on abnormal termination. 22 | 23 | Exception in thread "main" java.lang.ArithmeticException: / by zero 24 | at Example27.main(Example27.java:8) 25 | */ -------------------------------------------------------------------------------- /ExceptionHandling/Example9.java: -------------------------------------------------------------------------------- 1 | class Example9 2 | { 3 | public static void main(String[] args) 4 | { 5 | try 6 | { 7 | System.out.println("These is the try block..."); 8 | System.out.println(10/2); 9 | } 10 | catch(NullPointerException npe) // Not execute 11 | { 12 | System.out.println("These is the catch block..."); // Not execute 13 | } 14 | 15 | 16 | } 17 | } 18 | 19 | /* 20 | Java is not particular about the exception type within the catch block. 21 | Whenever there is no Exception generated but Java is particular about the 22 | exception type within catch block whenever an exception raised within try block. 23 | */ 24 | -------------------------------------------------------------------------------- /InnerClasses/NonStaticInnerClass/NormalInnerClass/Example1.java: -------------------------------------------------------------------------------- 1 | class OuterClassA 2 | { 3 | int number1 = 100; 4 | class InnerClassB 5 | { 6 | int number2 = 200; 7 | } 8 | } 9 | 10 | /* 11 | The Outer Class members can access in the Inner class 12 | But Inner Class members directly can not access in Outer class 13 | */ 14 | 15 | class Example1 16 | { 17 | public static void main(String[] args) 18 | { 19 | // Outer Class 20 | OuterClassA ref1 = new OuterClassA(); 21 | System.out.println(ref1.number1); 22 | 23 | // Inner Class 24 | OuterClassA.InnerClassB ref2= ref1.new InnerClassB(); 25 | System.out.println(ref2.number2); 26 | } 27 | } -------------------------------------------------------------------------------- /Non_Static_Blocks/Example4.java: -------------------------------------------------------------------------------- 1 | class Example4 2 | { 3 | 4 | /* 5 | 6 | When object is created then constructor get call , but it check first 7 | if any non static block will present or not and then proceed. 8 | If non static block are present then executed it first and then constructor executed. 9 | 10 | */ 11 | 12 | 13 | { 14 | int number1 =19 , number2 = 2 , result; 15 | result = number1+number2; 16 | System.out.println(result); 17 | } 18 | 19 | Example4() 20 | { 21 | System.out.println("The Constructor is called..."); 22 | } 23 | public static void main(String[] args) 24 | { 25 | Example4 obj = new Example4(); 26 | } 27 | } -------------------------------------------------------------------------------- /Collections/ArrayList13.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.Enumeration; 4 | import java.util.Vector; 5 | 6 | public class ArrayList13 { 7 | 8 | public static void main(String[] args) { 9 | Vector v=new Vector(); 10 | v.add(100); 11 | v.add(200); 12 | v.add(300); 13 | System.out.println(v); 14 | 15 | //Enumeration cursor only perform read operation . It does not perform set or remove methods 16 | 17 | Enumeration e=v.elements(); // Cursor 18 | while(e.hasMoreElements()) // hasMoreElements() check element present or not 19 | { 20 | System.out.println(e.nextElement()); // extract elements. 21 | } 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Collections/HashSet1.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.HashSet; 4 | 5 | public class HashSet1 { 6 | 7 | public static void main(String[] args) 8 | { 9 | HashSet hs=new HashSet(); 10 | hs.add(10); 11 | hs.add(20); 12 | hs.add(30); 13 | hs.add(40); 14 | System.out.println(hs); // insertion order is not maintain 15 | 16 | hs.add(30); 17 | hs.add(10); 18 | System.out.println(hs); // HashSet do not allow duplicate value 19 | // but when we add duplicate it return the same value Once 20 | 21 | hs.add(null); 22 | //hs.add(null); 23 | System.out.println(hs); // null is applicable to add 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Inheritance/Example23.java: -------------------------------------------------------------------------------- 1 | class Demo1 2 | { 3 | { 4 | System.out.println("This is the Instance Block - 1 - Demo1"); 5 | } 6 | 7 | /* 8 | Compiler by default create a constructor and also super() within it. 9 | So constructor first call then instance block is checked and executed and then continue 10 | */ 11 | } 12 | class Test1 extends Demo1 13 | { 14 | { 15 | System.out.println("This is the Instance Block - 2 - Test1"); 16 | } 17 | 18 | /* 19 | Every Constructor may be set super() when will execute the parent constructor 20 | */ 21 | } 22 | class Example23 23 | { 24 | public static void main(String args[]) 25 | { 26 | new Test1(); 27 | } 28 | } -------------------------------------------------------------------------------- /InnerClasses/NonStaticInnerClass/NormalInnerClass/Example4.java: -------------------------------------------------------------------------------- 1 | class OuterClassA 2 | { 3 | int number1 = 10; 4 | void method1() 5 | { 6 | 7 | /* 8 | We can not access the members of inner class in outer class. 9 | For these we need to create an Object. 10 | */ 11 | InnerClassB ref = new InnerClassB(); 12 | System.out.println(number1+ref.number2); 13 | } 14 | class InnerClassB 15 | { 16 | float number2 = 11.5f; 17 | } 18 | } 19 | 20 | 21 | 22 | class Example4 23 | { 24 | public static void main(String[] args) 25 | { 26 | //OuterClassA ref = new OuterClassA(); 27 | // ref.method1(); 28 | new OuterClassA().method1(); 29 | } 30 | } -------------------------------------------------------------------------------- /ExceptionHandling/Example12.java: -------------------------------------------------------------------------------- 1 | class Example12 2 | { 3 | public static void main(String[] args) 4 | { 5 | int[] a={10,20,30}; 6 | try 7 | { 8 | System.out.println("These is the try block..."); // Execute 9 | System.out.println(a[2]); // ArrayIndexOutOfBoundsException Execute 10 | System.out.println(10/0); // Can not handle multiple exceptions at a time 11 | } 12 | catch (ArrayIndexOutOfBoundsException ae) 13 | { 14 | System.out.println("These is the catch block..."); // Execute 15 | 16 | } 17 | } 18 | } 19 | 20 | /* 21 | Exception in thread "main" java.lang.ArithmeticException: / by zero 22 | at Example12.main(Example12.java:10) 23 | */ -------------------------------------------------------------------------------- /Polymorphism/RunTime Poly/Example30.java: -------------------------------------------------------------------------------- 1 | class ParentClass 2 | { 3 | protected void m1() // possible to override if access level is at same 4 | { 5 | System.out.println("Inside Private method -- M1 -- Parent"); 6 | } 7 | } 8 | 9 | class ChildClass extends ParentClass 10 | { 11 | protected void m1() // possible to override if access level is at same 12 | { 13 | System.out.println("Inside Private method -- M1 -- Child"); 14 | } 15 | } 16 | class Example30 17 | { 18 | public static void main(String[] args) 19 | { 20 | //Upcasting 21 | ParentClass pp = new ChildClass(); 22 | pp.m1(); // child execute Overriding and gives the preference to child 23 | } 24 | } -------------------------------------------------------------------------------- /Scanner_Classes/Scanner5.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.io.FileReader; 3 | import java.io.FileNotFoundException; 4 | class Scanner5 5 | { 6 | public static void main(String[] args) throws FileNotFoundException 7 | { 8 | FileReader ff=new FileReader("D:\\Programming\\cybersuccess\\ScannerClass\\Scanner2TextFile.txt"); // read the content from file 9 | Scanner scan= new Scanner(ff); 10 | scan.useDelimiter(","); // user define delimiter 11 | while (scan.hasNext()) 12 | { 13 | System.out.println(" "+scan.next()); 14 | } 15 | } 16 | } 17 | // public java.io.File(java.lang.String); 18 | // public java.util.Scanner(java.io.File) throws java.io.FileNotFoundException -------------------------------------------------------------------------------- /StarPattern7.java: -------------------------------------------------------------------------------- 1 | class StarPattern7 2 | { 3 | 4 | 5 | 6 | /* 7 | 8 | * 9 | * * 10 | * * * 11 | * * * * 12 | * * * * * 13 | * * * * 14 | * * * 15 | * * 16 | * 17 | 18 | 19 | */ 20 | 21 | 22 | public static void main(String args[]) 23 | { 24 | for (int column =1; column <=5 ;column++ ) 25 | { 26 | for (int row =1;row<=column ;row++ ) 27 | { 28 | System.out.print("* "); 29 | 30 | } 31 | System.out.println(); 32 | } 33 | 34 | 35 | 36 | for (int column =4; column >=1 ;column-- ) 37 | { 38 | for (int row =1; row<=column ; row++) 39 | { 40 | System.out.print("* "); 41 | } 42 | System.out.println(); 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /Collections/HashMap6.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.HashMap; 4 | 5 | public class HashMap6 { 6 | 7 | public static void main(String[] args) { 8 | HashMap hm=new HashMap(); 9 | hm.put(101, "bheem"); 10 | hm.put(106, "jaggu"); 11 | hm.put(105, "raju"); 12 | 13 | System.out.println(hm); 14 | 15 | HashMap hm1=new HashMap(hm); 16 | hm1.put(102, "chutaki"); 17 | hm1.put(108, "indu"); 18 | hm1.put(103, "kalia"); 19 | System.out.println(hm1); 20 | 21 | HashMap hm2=new HashMap(); 22 | hm2.putAll(hm); 23 | hm2.put(102, "dhollu"); 24 | hm2.put(108, "bhollu"); 25 | hm2.put(103, "laddu"); 26 | System.out.println(hm2); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /MaleFemaleAgeCheck.java: -------------------------------------------------------------------------------- 1 | class MaleFemaleAgeCheck 2 | { 3 | public static void main(String args[]) 4 | { 5 | int men_age = 21; // legal age for mens to marrage 6 | int female_age = 19; // legal age for mens to marrage 7 | char genderfemale = 'F'; // Gender (case_sensitive) 8 | char gendermale = 'M'; // Gender (case_sensitive) 9 | 10 | if((men_age>=21 && gendermale=='M') || (female_age>=18 && genderfemale =='F')) //check both conditions , either one is true then exicute if block otherwise else block... 11 | { 12 | System.out.println("Eligible for marrage..."); 13 | } 14 | else 15 | { 16 | System.out.println("Not Eligible for marrage..."); 17 | } 18 | 19 | } 20 | } -------------------------------------------------------------------------------- /Arrays/Example10.java: -------------------------------------------------------------------------------- 1 | class Example10 2 | { 3 | public static void main(String[] args) 4 | { 5 | 6 | Emp e1=new Emp(101,"Sahil"); /* constructor of Emp class there is need to create an 7 | non static variables and store the current object address also means (this) keyword is used */ 8 | Emp e2=new Emp(102,"Owai"); 9 | Emp e3=new Emp(103,"Navid"); 10 | 11 | Emp[] array= new Emp[3]; // create and array of Emp 12 | 13 | array[0]=e1; 14 | array[1]=e2; 15 | array[2]=e3; 16 | 17 | for (Emp ee : array) // Store the array index in variable type class 18 | { 19 | 20 | System.out.println(ee.rollno+"-"+ee.name); // call constructor local variable. 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /StaticVarMulClassAdd.java: -------------------------------------------------------------------------------- 1 | class FirstClass 2 | { 3 | static int result1; 4 | } 5 | 6 | class SecondClass 7 | { 8 | static float result2; 9 | } 10 | 11 | 12 | class StaticVarMulClassAdd 13 | { 14 | static int number1=10,number2=20; 15 | 16 | public static void main(String args[]) 17 | { 18 | float number3=100.5f,number4=200.5f; 19 | 20 | FirstClass.result1=number1+number2; // use the class_name and dot(.) to access the variable in the another class 21 | System.out.println(FirstClass.result1); 22 | 23 | SecondClass.result2=number3+number4; // use the class_name and dot(.) to access the variable in the another class 24 | System.out.println(SecondClass.result2); 25 | } 26 | } -------------------------------------------------------------------------------- /StringClasses/Example12.java: -------------------------------------------------------------------------------- 1 | class Example12 2 | { 3 | public static void main(String[] args) 4 | { 5 | // StringBuffer to String 2 approach 6 | // 1. Constructor 2. toString() 7 | 8 | StringBuffer sb1 = new StringBuffer("Sahil"); 9 | System.out.println(sb1); 10 | // Constructor 11 | String str1 = new String(sb1); 12 | System.out.println(str1); 13 | // toString() 14 | StringBuffer sb2 = new StringBuffer("Shaikh"); 15 | System.out.println(sb2); 16 | String str2 = sb2.toString(); 17 | System.out.println(str2); 18 | } 19 | } 20 | // public jaav.lang.StringBuffer(java.lang.String); 21 | // public jaav.lang.String(java.lang.StringBuffer); 22 | // public jaav.lang.String toString(); -------------------------------------------------------------------------------- /ExceptionHandling/Example34.java: -------------------------------------------------------------------------------- 1 | class Example34 2 | { 3 | static void m3() 4 | { 5 | System.out.println("These is the M3 method"); 6 | try 7 | { 8 | System.out.println(10/0); 9 | } 10 | catch (ArithmeticException ae) 11 | { 12 | ae.printStackTrace(); 13 | } 14 | } 15 | 16 | static void m2() 17 | { 18 | System.out.println("These is the M2 method"); 19 | m3(); 20 | } 21 | static void m1() 22 | { 23 | System.out.println("These is the M1 method"); 24 | m2(); 25 | } 26 | public static void main(String[] args) 27 | { 28 | m1(); 29 | } 30 | } 31 | //Internally which method does java invoke to print exception? 32 | // ANS: printStackTrace(); -------------------------------------------------------------------------------- /Inheritance/Example26.java: -------------------------------------------------------------------------------- 1 | class Demo1 2 | { 3 | Demo1() 4 | { 5 | System.out.println("This is the 0 args constructor Block - 1 - Demo1"); 6 | } 7 | 8 | /* 9 | Compiler by default create a constructor and also super() within it. 10 | So constructor first call then instance block is checked and executed and then continue 11 | */ 12 | } 13 | class Test1 extends Demo1 14 | { 15 | 16 | { 17 | System.out.println("This is the Instance Block - 1 - Test1"); 18 | } 19 | 20 | 21 | /* 22 | Every Constructor may be set super() when will execute the parent constructor 23 | */ 24 | } 25 | class Example26 26 | { 27 | public static void main(String args[]) 28 | { 29 | new Test1(); 30 | } 31 | } -------------------------------------------------------------------------------- /StringClasses/Example28.java: -------------------------------------------------------------------------------- 1 | import java.util.StringTokenizer; 2 | 3 | class Example28 4 | { 5 | public static void main(String[] args) 6 | { 7 | StringTokenizer st= new StringTokenizer("sahil shaikh , is the , computer engineering , student",","); // print till , in one line and then further ... 8 | while (st.hasMoreTokens()) // checks element is present or not 9 | { 10 | System.out.println(st.nextToken()); //extract the element 11 | } 12 | 13 | } 14 | } 15 | // public StringTokenizer(String str,String delim) 16 | // public boolean hasMoreElements() 17 | // public Object nextElement() 18 | 19 | /* 20 | sahil shaikh 21 | is the 22 | computer engineering 23 | student 24 | */ 25 | -------------------------------------------------------------------------------- /Collections/HashMap5.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.HashMap; 4 | import java.util.Iterator; 5 | import java.util.Map; 6 | import java.util.Set; 7 | 8 | public class HashMap5 { 9 | 10 | public static void main(String[] args) { 11 | HashMap hm=new HashMap(); 12 | hm.put(101, "bheem"); 13 | hm.put(106, "jaggu"); 14 | hm.put(105, "raju"); 15 | 16 | System.out.println(hm); 17 | 18 | Set ss=hm.entrySet(); // Map is Now in Set 19 | 20 | Iterator ii=ss.iterator(); // Cursor Iterator is applicable now 21 | while(ii.hasNext()) 22 | { 23 | Map.Entry me=(Map.Entry)ii.next(); 24 | System.out.println(me.getKey()+" - "+me.getValue()); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /StaticAndNonStatic/StaticvarInNStaticArea.java: -------------------------------------------------------------------------------- 1 | class StaticvarInNStaticArea 2 | { 3 | static float number=100.5f; 4 | static void myMethod() 5 | { 6 | System.out.println("Inside the static method and access by non static area and then main()"); 7 | } 8 | 9 | void nonStaticArea() 10 | { 11 | // by class name 12 | System.out.println(StaticvarInNStaticArea.number); 13 | StaticvarInNStaticArea.myMethod(); 14 | 15 | System.out.println(); 16 | 17 | // direct way 18 | System.out.println(number); 19 | myMethod(); 20 | } 21 | 22 | public static void main(String[] args) 23 | { 24 | StaticvarInNStaticArea refVar = new StaticvarInNStaticArea(); 25 | refVar.nonStaticArea(); 26 | } 27 | } -------------------------------------------------------------------------------- /StringClasses/Example2.java: -------------------------------------------------------------------------------- 1 | class Example2 2 | { 3 | public static void main(String[] args) 4 | { 5 | // approach -1 : without new keyword 6 | String first="vdf"; 7 | String second="school"; 8 | String third="engineering"; 9 | System.out.println(first); 10 | System.out.println(second); 11 | System.out.println(third); 12 | 13 | System.out.println(); 14 | 15 | // approach -2 : with new keyword 16 | String fourth = new String("vdf"); 17 | System.out.println(fourth); 18 | String fifth = new String("school"); 19 | System.out.println(fifth); 20 | String sixth = new String("engineering"); 21 | System.out.println(sixth); 22 | } 23 | } 24 | // public jaav.lang.String(java.lang.String); -------------------------------------------------------------------------------- /Collections/HashMap4.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.Collection; 4 | import java.util.HashMap; 5 | import java.util.Set; 6 | 7 | public class HashMap4 { 8 | 9 | public static void main(String[] args) { 10 | HashMap hm=new HashMap(); 11 | hm.put(101, "bheem"); 12 | hm.put(106, "jaggu"); 13 | hm.put(105, "raju"); 14 | 15 | Set s1=hm.keySet(); // return the all keys in the set format and set is also contain unique data 16 | System.out.println(s1); 17 | 18 | System.out.println(hm); 19 | 20 | Collection cc=hm.values(); // return the all value in the Collection and Collection is also contain unique data / Duplicate data 21 | System.out.println(cc); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Collections/TreeMap6.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.SortedMap; 4 | import java.util.TreeMap; 5 | 6 | public class TreeMap6 { 7 | public static void main(String[] args) { 8 | TreeMap tm=new TreeMap(); 9 | tm.put(103, "oggy"); 10 | tm.put(102, "jack"); 11 | tm.put(105, "motu"); 12 | tm.put(101, "ghasita"); 13 | tm.put(104, "chingum"); 14 | 15 | System.out.println(tm); 16 | 17 | SortedMap sm=tm.headMap(103); 18 | TreeMap tm1=new TreeMap(sm); 19 | System.out.println(tm1); // print all before 103 20 | 21 | SortedMap sm2=tm.tailMap(103); 22 | TreeMap tm2=new TreeMap(sm2); 23 | System.out.println(tm2); // print all after and with 103 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Inheritance/Example2.java: -------------------------------------------------------------------------------- 1 | //Single Level Inheritance 2 | // Bussiness Logic 3 | class A 4 | { 5 | void method1() 6 | { 7 | System.out.println("I'm Parent class..."); 8 | } 9 | } 10 | 11 | class B extends A 12 | { 13 | void method2() 14 | { 15 | System.out.println("I'm Child class..."); 16 | } 17 | } 18 | 19 | //Controller Class 20 | class Example2 21 | { 22 | public static void main(String[] args) 23 | { 24 | // Access only the A class members 25 | A obj1 = new A(); 26 | obj1.method1(); 27 | 28 | // Access both class members (B class is child of A class And Example2 is the Controller Class ) 29 | B obj2 = new B(); 30 | obj2.method1(); 31 | obj2.method2(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Collections/ArrayList11.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class ArrayList11 { 6 | 7 | public static void main(String[] args) 8 | { 9 | // Normal Collection 10 | ArrayList al=new ArrayList(); 11 | al.add(new Emp(101,"sahil")); 12 | //System.out.println(al); print the address 13 | 14 | // There are two ways to extract data 15 | 16 | // 1 approach 17 | 18 | for(Object o:al) 19 | { 20 | Emp e=(Emp)o; 21 | System.out.println(e.empId+" - "+e.empName); 22 | } 23 | 24 | // 2nd approach -- get(index) 25 | 26 | Object o=al.get(0); 27 | Emp e=(Emp)o; 28 | System.out.println(e.empId+" - "+e.empName); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Polymorphism/RunTime Poly/Example18.java: -------------------------------------------------------------------------------- 1 | class Demo 2 | { 3 | void method() // If method is made as final that it is not possible to override. 4 | // Parent method is must final. 5 | { 6 | System.out.println("Inside the Demo class -- >>"); 7 | } 8 | } 9 | class Sample extends Demo 10 | { 11 | final void method() /* It will not matter if child is final or not but 12 | it definetely matter parent is final or not with respect to concept of overriding. 13 | */ 14 | { 15 | System.out.println("Inside the Sample class -- >>"); 16 | } 17 | } 18 | class Example18 19 | { 20 | public static void main(String[] args) 21 | { 22 | Sample ss = new Sample(); 23 | ss.method(); 24 | } 25 | } -------------------------------------------------------------------------------- /StaticAndNonStatic/StaticVarMulClassAdd.java: -------------------------------------------------------------------------------- 1 | class FirstClass 2 | { 3 | static int result1; 4 | } 5 | 6 | class SecondClass 7 | { 8 | static float result2; 9 | } 10 | 11 | 12 | class StaticVarMulClassAdd 13 | { 14 | static int number1=10,number2=20; 15 | 16 | public static void main(String args[]) 17 | { 18 | float number3=100.5f,number4=200.5f; 19 | 20 | FirstClass.result1=number1+number2; // use the class_name and dot(.) to access the variable in the another class 21 | System.out.println(FirstClass.result1); 22 | 23 | SecondClass.result2=number3+number4; // use the class_name and dot(.) to access the variable in the another class 24 | System.out.println(SecondClass.result2); 25 | } 26 | } -------------------------------------------------------------------------------- /Collections/LinkedList3.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.Iterator; 4 | import java.util.LinkedList; 5 | 6 | public class LinkedList3 { 7 | 8 | public static void main(String[] args) { 9 | 10 | LinkedList llEmp=new LinkedList(); 11 | llEmp.add(new Emp(101,"oggy")); 12 | llEmp.add(new Emp(102,"motu")); 13 | llEmp.add(new Emp(103,"bheem")); 14 | llEmp.add(new Emp(104,"patlu")); 15 | 16 | // Iterator and ListIterator Cursor are applicable to LinkedList 17 | Iterator ii=llEmp.iterator(); 18 | 19 | while(ii.hasNext()) 20 | { 21 | Emp ee=ii.next(); 22 | System.out.println(ee.empId+" - "+ee.empName); 23 | 24 | } 25 | 26 | 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /InnerClasses/NonStaticInnerClass/NormalInnerClass/Example6.java: -------------------------------------------------------------------------------- 1 | class OuterClassA 2 | { 3 | // Private members of Outer class can be Access in Inner class. 4 | private int number1 = 10; 5 | private float number2 = 11.5f; 6 | class InnerClassB 7 | { 8 | void method1() 9 | { 10 | System.out.println("Inside Method - 1 of InnerClassB : "); 11 | System.out.println(number1+number2); 12 | } 13 | } 14 | } 15 | 16 | class Example6 17 | { 18 | public static void main(String[] args) 19 | { 20 | /* 21 | OuterClassA ref1 = new OuterClassA(); 22 | OuterClassA.InnerClassB ref2 = ref1.new InnerClassB(); 23 | ref2.method1(); 24 | */ 25 | 26 | new OuterClassA().new InnerClassB().method1(); 27 | } 28 | } -------------------------------------------------------------------------------- /Non_Static_Blocks/Example8.java: -------------------------------------------------------------------------------- 1 | class Example8 2 | { 3 | /* 4 | 5 | 1. Non static variables are use to initialize the non static or instance members/variables. 6 | 2. Instance blocks are used to the implements the bussiness logic during object creation 7 | 8 | */ 9 | 10 | int number1 , number2 , result; // instance members 11 | { 12 | System.out.println("Non static block "+result); 13 | int number1 =19 , number2 = 2 ; 14 | result=number1+number2; 15 | } 16 | 17 | Example8() 18 | { 19 | //result = number1+number2; 20 | System.out.println("0 Parameter constructor "+result); 21 | } 22 | 23 | public static void main(String[] args) 24 | { 25 | Example8 obj1 =new Example8(); 26 | 27 | } 28 | } -------------------------------------------------------------------------------- /ExceptionHandling/Example29.java: -------------------------------------------------------------------------------- 1 | class Example29 2 | { 3 | public static void main(String[] args) 4 | { 5 | try 6 | { 7 | System.out.println("Inside Outer try block"); 8 | System.out.println(10/0); 9 | } 10 | catch (ArithmeticException ae) 11 | { 12 | // we can use the inner try-catch (nested try-catch) 13 | // When exception is occur in the catch block it can handle by inner try-catch 14 | System.out.println("Inside the Inner catch block"); 15 | try 16 | { 17 | System.out.println("Inside the Inner try block"); 18 | System.out.println(10/0); 19 | } 20 | catch (ArithmeticException ae1) 21 | { 22 | System.out.println("Inside the Inner catch block"); 23 | } 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /Arrays/Example9.java: -------------------------------------------------------------------------------- 1 | class Example9 2 | { 3 | public static void main(String[] args) 4 | { 5 | 6 | Emp e1=new Emp(101,"Sahil"); /* constructor of Emp class there is need to create an 7 | non static variables and store the current object address also means (this) keyword is used */ 8 | Emp e2=new Emp(102,"Owai"); 9 | Emp e3=new Emp(103,"Navid"); 10 | 11 | Emp[] array= new Emp[3]; // create and array of Emp 12 | 13 | array[0]=e1; 14 | array[1]=e2; 15 | array[2]=e3; 16 | 17 | for (int i=0;isic.itemId) 24 | { 25 | return 1; 26 | } 27 | else if (itemId>"); 8 | } 9 | } 10 | class Sample extends Demo 11 | { 12 | void method() /* It will not matter if child is final or not but 13 | it definetely matter parent is final or not with respect to concept of overriding. 14 | */ 15 | { 16 | System.out.println("Inside the Sample class -- >>"); 17 | } 18 | } 19 | class Example17 20 | { 21 | public static void main(String[] args) 22 | { 23 | Sample ss = new Sample(); 24 | ss.method(); 25 | } 26 | } -------------------------------------------------------------------------------- /Collections/ArrayList15.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.ArrayList; 4 | import java.util.ListIterator; 5 | 6 | public class ArrayList15 { 7 | 8 | public static void main(String[] args) { 9 | ArrayList al=new ArrayList(); 10 | al.add("A"); 11 | al.add("B"); 12 | al.add("C"); 13 | System.out.println(al); 14 | 15 | // ListIterator is only applicable to the list classes 16 | // Operations -- > read() , remove() , set , add operations 17 | 18 | ListIterator li=al.listIterator(); // cursor bidirectional cursor 19 | while(li.hasNext()) // check element present or not 20 | { 21 | String ss=(String)li.next(); // extract element 22 | System.out.println(ss); 23 | } 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /ExceptionHandling/Example30.java: -------------------------------------------------------------------------------- 1 | class Example30 2 | { 3 | public static void main(String[] args) 4 | { 5 | try 6 | { 7 | System.out.println("Inside Outer try block"); 8 | System.out.println(10/0); 9 | } 10 | catch (ArithmeticException ae) 11 | { 12 | System.out.println("Inside the Inner catch block"); 13 | } 14 | finally 15 | { 16 | // we can use try-catch inside the finally 17 | System.out.println("Inside the finally block"); 18 | try 19 | { 20 | System.out.println("Inside the finally Inner try block"); 21 | System.out.println(10/0); 22 | } 23 | catch (ArithmeticException ae1) 24 | { 25 | System.out.println("Inside the finally Inner catch block"); 26 | } 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /Static_Blocks/Example6.java: -------------------------------------------------------------------------------- 1 | class AnotherClass 2 | { 3 | static int number; 4 | static 5 | { 6 | System.out.println("These is static block : "+ number); 7 | number = 10; 8 | } 9 | 10 | static void method() 11 | { 12 | System.out.println("Value of number is : "+number); 13 | } 14 | } 15 | 16 | 17 | class Example6 18 | { 19 | 20 | public static void main(String[] args) 21 | { 22 | System.out.println("These is Main block..."); 23 | System.out.println("Value of number in AnotherClass is : "+AnotherClass.number); 24 | } 25 | } 26 | 27 | /* 28 | Only when JVM is clear as well as aware to call other class members only then an 29 | explicity call will be give to class loader to load the static members in memory 30 | */ -------------------------------------------------------------------------------- /Inheritance/Example14.java: -------------------------------------------------------------------------------- 1 | // parent / super / base 2 | class A 3 | { 4 | void m1() 5 | { 6 | System.out.println("It is the m1 method of class A -- A"); 7 | } 8 | } 9 | 10 | // child / sub / derived 11 | class B extends A 12 | { 13 | void m1() 14 | { 15 | System.out.println("It is the m1 method of class B -- B"); 16 | } 17 | void test() 18 | { 19 | System.out.println("Test method is called..."); 20 | this.m1(); // super keyword is also used to call the methods. 21 | super.m1(); // super keyword is also used to call the methods. 22 | } 23 | } 24 | 25 | class Example14 26 | { 27 | public static void main(String[] args) 28 | { 29 | A a= new A(); 30 | a.m1(); 31 | B b =new B(); 32 | b.test(); 33 | } 34 | } -------------------------------------------------------------------------------- /Static_Blocks/Example4.java: -------------------------------------------------------------------------------- 1 | class Example4 2 | { 3 | /* 4 | Static blocks are executed during class loading time and non static blocks are executed during object creation 5 | Static blocks are execute first. 6 | Non static blocks and constructor are executed how many times object is created... 7 | */ 8 | 9 | static 10 | { 11 | System.out.println("These is static block "); 12 | } 13 | 14 | { 15 | System.out.println("These is non static block "); 16 | } 17 | Example4() 18 | { 19 | System.out.println("These is 0 Constructor"); 20 | } 21 | public static void main(String[] args) 22 | { 23 | System.out.println("These is Main block..."); 24 | Example4 obj1= new Example4(); 25 | Example4 obj2= new Example4(); 26 | } 27 | } -------------------------------------------------------------------------------- /ExceptionHandling/CustomeCheckException1.java: -------------------------------------------------------------------------------- 1 | class InvalidAmountTransfer extends Exception 2 | { 3 | } 4 | 5 | class Flipkart 6 | { 7 | int productPrice=5000; 8 | void byProduct(int custemerPrice) throws InvalidAmountTransfer 9 | { 10 | if (productPrice==custemerPrice) 11 | { 12 | System.out.println("Amount transfer Successfull..."); 13 | } 14 | else 15 | { 16 | System.out.println("Amount transfer Fail..."); 17 | throw new InvalidAmountTransfer(); 18 | } 19 | } 20 | } 21 | 22 | class CustomeCheckException1 23 | { 24 | public static void main(String[] args) throws InvalidAmountTransfer 25 | { 26 | Flipkart cust1=new Flipkart(); 27 | cust1.byProduct(2000); 28 | } 29 | } 30 | /* 31 | 0 args check exception 32 | */ -------------------------------------------------------------------------------- /Static_Blocks/Example5.java: -------------------------------------------------------------------------------- 1 | class Example5 2 | { 3 | /* 4 | Static blocks are executed during class loading time and non static blocks are executed during object creation 5 | Static blocks are execute first. 6 | 7 | 1. To initialize the static members variables during class loading time. 8 | 2. To implements bussiness logic during class loader. 9 | */ 10 | 11 | static int number; 12 | static 13 | { 14 | System.out.println("These is static block : "+ number); 15 | number = 10; 16 | } 17 | 18 | static void method() 19 | { 20 | System.out.println("Value of number is : "+number); 21 | } 22 | 23 | public static void main(String[] args) 24 | { 25 | System.out.println("These is Main block..."); 26 | method(); 27 | } 28 | } -------------------------------------------------------------------------------- /Collections/ArrayList6.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class ArrayList6 { 6 | 7 | public static void main(String[] args) { 8 | ArrayList al = new ArrayList(); 9 | al.add(100); 10 | al.add("sahil"); 11 | al.add('s'); 12 | al.add(100.5f); 13 | System.out.println(al); 14 | 15 | // remove() is overloaded method and it accept the index 16 | al.remove(0); 17 | 18 | System.out.println(al); 19 | 20 | //al.remove(100.5f); --> could not convert to the index 21 | // remove() method directly can not accept the object value 22 | // 1st convert it to object and pass to remove() 23 | 24 | Float f=100.5f; 25 | al.remove(f); 26 | System.out.println(al); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /ExceptionHandling/Example18.java: -------------------------------------------------------------------------------- 1 | class Example18 2 | { 3 | public static void main(String[] args) 4 | { 5 | int[] a = {1,2,3,4,5}; 6 | try 7 | { 8 | System.out.println("Inside the try block --- 1"); 9 | System.out.println(10/2); 10 | System.out.println(a[10]); 11 | } 12 | catch (ArithmeticException e) 13 | { 14 | System.out.println("Inside ArithmeticException ..."); 15 | } 16 | catch(Exception e) 17 | { 18 | System.out.println("Inside Exception..."); 19 | } 20 | } 21 | } 22 | 23 | /* 24 | It is better recommended that is the exception is raised of particular type 25 | handle it with the same type. 26 | 27 | Multiple catch blocks are allowed if the order of multiple catch blocks are from child to parent 28 | */ -------------------------------------------------------------------------------- /InnerClasses/StaticInnerClass/Example18.java: -------------------------------------------------------------------------------- 1 | class OuterClassA 2 | { 3 | /* 4 | Static Inner Class only allow static member to be 5 | access to outer class members in inner class 6 | */ 7 | 8 | int number1 = 100; 9 | static float number2 = 200.5f; 10 | 11 | void m2() 12 | { 13 | new InnerClassB().m1(); 14 | } 15 | static class InnerClassB 16 | { 17 | void m1() 18 | { 19 | OuterClassA ref2 = new OuterClassA(); 20 | System.out.println(number2); 21 | System.out.println(ref2.number1); 22 | System.out.println(ref2.number1+number2); 23 | 24 | } 25 | } 26 | } 27 | 28 | class Example18 29 | { 30 | public static void main(String[] args) 31 | { 32 | OuterClassA ref3 = new OuterClassA(); 33 | ref3.m2(); 34 | } 35 | } -------------------------------------------------------------------------------- /Non_Static_Blocks/Customer.java: -------------------------------------------------------------------------------- 1 | class HDFC 2 | { 3 | int amountBalance; 4 | HDFC(int amountBalance) 5 | { 6 | System.out.println("Processing your Account details..."); 7 | if(amountBalance>=5000) 8 | { 9 | this.amountBalance=this.amountBalance+amountBalance; 10 | System.out.println("Your account is Successfully Created..."); 11 | } 12 | else 13 | { 14 | System.out.println("Please deposit Sufficient Amount..."); 15 | } 16 | 17 | } 18 | 19 | void geAmountBalance() 20 | { 21 | System.out.println("Your account balance is : "+amountBalance); 22 | } 23 | } 24 | class Customer 25 | { 26 | 27 | public static void main(String[] args) 28 | { 29 | HDFC cutomer1 = new HDFC(5000); 30 | cutomer1.geAmountBalance(); 31 | } 32 | } -------------------------------------------------------------------------------- /Collections/ArrayList7.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | import java.util.ArrayList; 3 | 4 | public class ArrayList7 { 5 | 6 | public static void main(String[] args) { 7 | ArrayList al=new ArrayList(); 8 | al.add(100); 9 | al.add("Sahil"); 10 | al.add(200.5f); 11 | al.add(true); 12 | System.out.println(al); 13 | 14 | //al.add(int index, object element); 15 | 16 | al.add(2, "Shaikh"); // it add the element at specific index within the ArrayList 17 | System.out.println(al); 18 | System.out.println(al.isEmpty()); //To check collection is empty or not 19 | 20 | al.clear(); //To clear the all elements within the ArrayList 21 | System.out.println(al); 22 | 23 | System.out.println(al.isEmpty()); 24 | 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /ExceptionHandling/CustomeUnCheckException1.java: -------------------------------------------------------------------------------- 1 | class InvalidAmountTransfer extends RuntimeException 2 | { 3 | 4 | } 5 | 6 | class Flipkart 7 | { 8 | int productPrice=5000; 9 | void byProduct(int custemerPrice) throws InvalidAmountTransfer 10 | { 11 | if (productPrice==custemerPrice) 12 | { 13 | System.out.println("Amount transfer Successfull..."); 14 | } 15 | else 16 | { 17 | System.out.println("Fail..."); 18 | throw new InvalidAmountTransfer(); 19 | } 20 | } 21 | } 22 | 23 | 24 | /* 25 | args Uncheck exception 26 | */ 27 | 28 | class CustomeUnCheckException1 29 | { 30 | public static void main(String[] args) throws InvalidAmountTransfer 31 | { 32 | Flipkart cust1=new Flipkart(); 33 | cust1.byProduct(2000); 34 | } 35 | } -------------------------------------------------------------------------------- /ExceptionHandling/Example24.java: -------------------------------------------------------------------------------- 1 | class Example24 2 | { 3 | public static void main(String[] args) 4 | { 5 | int[] a={10,20,30}; 6 | try 7 | { 8 | System.out.println("Inside the try block"); 9 | System.out.println(10/0); 10 | } 11 | catch (ArithmeticException ae) 12 | { 13 | // catch block exception 14 | System.out.println("Inside the catch block"); 15 | System.out.println(a[10]); // can not handle exception 16 | } 17 | finally 18 | { 19 | // Finally block will always executed... 20 | System.out.println("Inside the Finally block"); 21 | } 22 | } 23 | } 24 | /* 25 | Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 3 26 | at Example24.main(Example24.java:14) 27 | */ -------------------------------------------------------------------------------- /InnerClasses/NonStaticInnerClass/AnonymousInnerClass/Example22.java: -------------------------------------------------------------------------------- 1 | interface Parent 2 | { 3 | void test1(); 4 | void test2(); 5 | } 6 | 7 | 8 | class Example22 9 | { 10 | Parent pp = new Parent() // Anonymous Class Syntax 11 | // Create object with body 12 | { 13 | public void test1() 14 | { 15 | System.out.println("These is the test - 1 method Inside Anonymous Class"); 16 | } 17 | public void test2() 18 | { 19 | System.out.println("These is the test - 2 method Inside Anonymous Class"); 20 | } 21 | }; 22 | public static void main(String[] args) 23 | { 24 | Example22 ref = new Example22(); 25 | ref.pp.test1(); // access the anonymous class members 26 | ref.pp.test2(); // access the anonymous class members 27 | } 28 | } -------------------------------------------------------------------------------- /ExceptionHandling/Example31.java: -------------------------------------------------------------------------------- 1 | class Example31 2 | { 3 | public static void main(String[] args) 4 | { 5 | try 6 | { 7 | System.out.println("Inside the try block"); 8 | System.exit(0); //shutdown the JVM 9 | System.out.println(10/0); 10 | } 11 | catch (ArithmeticException ae) 12 | { 13 | System.out.println("Inside the catch block"); 14 | } 15 | finally 16 | { 17 | System.out.println("Inside the finally block"); 18 | } 19 | } 20 | } 21 | /* 22 | How to avoid execution of finally?? 23 | ANS: When we use System.exit(int status) method it will be responsible to shutdown the JVM 24 | either under normal condition or under abnormal condition. 25 | 26 | When JVM shutd down then the control will never enter into the finally block. 27 | */ -------------------------------------------------------------------------------- /Polymorphism/RunTime Poly/Example26.java: -------------------------------------------------------------------------------- 1 | class Parent 2 | { 3 | static void carkey() // Method Names are same "Overriding" 4 | { 5 | System.out.println("Give me the Key of Car..."); 6 | } 7 | } 8 | 9 | class Child extends Parent 10 | { 11 | static void carkey() // Method Names are same "Overriding" 12 | { 13 | System.out.println("Give me the Key of Bike..."); 14 | } 15 | } 16 | 17 | /* 18 | This is also called method hidden as child method get hidden because by 19 | nature the method gives same and static by nature and method name remain same. 20 | */ 21 | 22 | class Example26 23 | { 24 | public static void main(String[] args) 25 | { 26 | // Upcasting 27 | Parent pp=new Child(); 28 | pp.carkey(); // Gives the preference to parent class 29 | } 30 | } -------------------------------------------------------------------------------- /Abstraction/Example2.java: -------------------------------------------------------------------------------- 1 | abstract class ApnaBankNetBanking 2 | { 3 | abstract void moneyTransfer(); // To achieve abstraction there should be concept of overriding which is concept of potymorphism 4 | } 5 | 6 | class ApnaBankServer extends ApnaBankNetBanking // To achieve abstraction we require 2 class 7 | // Between two class there should be concept of inheritance 8 | { 9 | void moneyTransfer() // To achieve abstraction there should be concept of overriding which is concept of potymorphism 10 | { 11 | System.out.println("Logic for transfer money..."); 12 | } 13 | } 14 | 15 | class Example2 16 | { 17 | public static void main(String args[]) 18 | { 19 | ApnaBankServer custmer=new ApnaBankServer(); 20 | custmer.moneyTransfer(); 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /Inheritance/Example28.java: -------------------------------------------------------------------------------- 1 | class Demo1 2 | { 3 | // static block executes first in the top to bottom manner. Precedance is gives to parent class 4 | static 5 | { 6 | System.out.println("This is the Static Block - 1 - Demo1"); 7 | } 8 | 9 | { 10 | System.out.println("This is the Instance Block - 1 - Demo1"); 11 | } 12 | // call default constructor and default super() 13 | } 14 | class Test1 extends Demo1 15 | { 16 | static 17 | { 18 | System.out.println("This is the Static Block - 2 - Test1"); 19 | } 20 | 21 | { 22 | System.out.println("This is the Instance Block - 2 - Test1"); 23 | } 24 | } 25 | // call default constructor and default super() 26 | class Example28 27 | { 28 | public static void main(String args[]) 29 | { 30 | new Test1(); 31 | } 32 | } -------------------------------------------------------------------------------- /Polymorphism/RunTime Poly/Example25.java: -------------------------------------------------------------------------------- 1 | class Parent 2 | { 3 | void carkey() // Method Names are same "Overriding" 4 | { 5 | System.out.println("Give me the Key of Car..."); 6 | } 7 | } 8 | 9 | class Child extends Parent 10 | { 11 | void carkey() // Method Names are same "Overriding" 12 | { 13 | System.out.println("Give me the Key of Bike..."); 14 | } 15 | } 16 | 17 | /* 18 | It is possible to override non static methods as they are bounded 19 | by objects. However it is not possible to override static members 20 | and they are bound by classes. 21 | */ 22 | 23 | class Example25 24 | { 25 | public static void main(String[] args) 26 | { 27 | // Upcasting 28 | Parent pp=new Child(); 29 | pp.carkey(); // Gives the preference to child class 30 | } 31 | } -------------------------------------------------------------------------------- /Collections/LinkedList2.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.LinkedList; 4 | 5 | 6 | // Add two LinkedList by 2 ways ie. Constructor based and addAll() 7 | 8 | public class LinkedList2 { 9 | 10 | public static void main(String[] args) { 11 | 12 | LinkedList ll=new LinkedList(); 13 | ll.add(100); 14 | ll.add(244); 15 | ll.add("motu"); 16 | System.out.println(ll); 17 | 18 | LinkedList ll2=new LinkedList(ll); // add one LinkedList to Another LinkedList 19 | ll2.add(101); 20 | ll2.add("patlu"); 21 | System.out.println(ll2); 22 | 23 | LinkedList ll3=new LinkedList(); 24 | ll3.addAll(ll2); // add one LinkedList to Another LinkedList 25 | ll3.add(102); 26 | ll3.add("chingum"); 27 | System.out.println(ll3); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /InnerClasses/NonStaticInnerClass/NormalInnerClass/Example5.java: -------------------------------------------------------------------------------- 1 | class OuterClassA 2 | { 3 | int number1 = 10; 4 | void method1() 5 | { 6 | System.out.println("Inside Method - 1 of OuterClassA : "+number1); 7 | } 8 | class InnerClassB 9 | { 10 | // Non static members access in non static area 11 | float number2 = 11.5f; 12 | void method2() 13 | { 14 | System.out.println("Inside Method - 2 of InnerClassB : "+number2); 15 | method1(); 16 | } 17 | } 18 | } 19 | 20 | class Example5 21 | { 22 | public static void main(String[] args) 23 | { 24 | /* 25 | OuterClassA ref1 = new OuterClassA(); 26 | OuterClassA.InnerClassB ref2 = ref1.new InnerClassB(); 27 | ref2.method2(); 28 | */ 29 | 30 | new OuterClassA().new InnerClassB().method2(); 31 | } 32 | } -------------------------------------------------------------------------------- /Collections/ArrayList20.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.ArrayList; 4 | import java.util.ListIterator; 5 | 6 | public class ArrayList20 { 7 | 8 | public static void main(String[] args) { 9 | ArrayList al=new ArrayList(); 10 | al.add("sahil"); 11 | al.add("shaikh"); 12 | al.add("dev"); 13 | System.out.println(al); 14 | 15 | // extract data from ArrayList from start to end 16 | ListIterator li=al.listIterator(); 17 | while(li.hasNext()) 18 | { 19 | //System.out.println(li.next()); 20 | if (li.next().equals("shaikh")) 21 | { 22 | li.remove(); //Manupulatuion is possible using cursor 23 | // remove is used to remove item 24 | 25 | } 26 | } 27 | System.out.println(al); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Inheritance/Example12.java: -------------------------------------------------------------------------------- 1 | class Nonvariables 2 | { 3 | int number1 = 10; 4 | float number2 = 20.5f; 5 | 6 | } 7 | class NonvariablesAndMethod extends Nonvariables 8 | { 9 | int number3 = 100; 10 | float number4 = 200.5f; 11 | 12 | void method(int number5 , float number6) 13 | { 14 | System.out.println(number5+number6); //Addition of arguments number 15 | System.out.println(number3+number4); //Addition of nonstatic variable within same class number 16 | System.out.println(number1+number2); //Addition of nonstatic variable different class but inherited by these class number 17 | } 18 | } 19 | class Example12 20 | { 21 | public static void main(String args[]) 22 | { 23 | NonvariablesAndMethod obj1 = new NonvariablesAndMethod(); 24 | obj1.method(1000,2000.5f); 25 | } 26 | } -------------------------------------------------------------------------------- /Collections/HashSet3.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.HashSet; 4 | 5 | public class HashSet3 { 6 | public static void main(String[] args) { 7 | HashSet hs1=new HashSet(); 8 | hs1.add(10); 9 | hs1.add(20); 10 | hs1.add(30); 11 | hs1.add(40); 12 | System.out.println(hs1); // insertion order is not maintain 13 | 14 | HashSet hs2=new HashSet(hs1); // add one HashSet to another using Constructor based approach 15 | hs2.add(100); 16 | hs2.add(200); 17 | hs2.add(300); 18 | hs2.add(400); 19 | System.out.println(hs2); 20 | 21 | HashSet hs3=new HashSet(); 22 | hs3.addAll(hs1); // Using addAll(); 23 | hs3.add(1000); 24 | hs3.add(2000); 25 | hs3.add(3000); 26 | hs3.add(4000); 27 | System.out.println(hs3); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /InnerClasses/NonStaticInnerClass/MethodLocalInnerClass/Example12.java: -------------------------------------------------------------------------------- 1 | class OuterClassA 2 | { 3 | void m1() 4 | { 5 | class InnerClassB 6 | { 7 | 8 | int number1=100; 9 | } 10 | InnerClassB ref1 = new InnerClassB(); 11 | System.out.println(ref1.number1); 12 | } 13 | } 14 | 15 | /* 16 | A class declare within Method is called Method Local Inner Class 17 | 18 | Once method is active (Within Stack) only then it is possible to declare 19 | the class as well as we can create an object. 20 | 21 | Once method is inactive (out of stack) then the respective object is eligible for distruction. 22 | 23 | */ 24 | 25 | class Example12 26 | { 27 | public static void main(String[] args) 28 | { 29 | 30 | OuterClassA ref2 = new OuterClassA(); 31 | ref2.m1(); 32 | } 33 | } -------------------------------------------------------------------------------- /InnerClasses/NonStaticInnerClass/NormalInnerClass/Example2.java: -------------------------------------------------------------------------------- 1 | class OuterClassA 2 | { 3 | int number1 = 100; 4 | class InnerClassB 5 | { 6 | int number2 = 200; 7 | } 8 | } 9 | 10 | /* 11 | The Outer Class members can access in the Inner class 12 | But Inner Class members directly can not access in Outer class 13 | 14 | Whenever we compile it , it generates the .class file of inner class as well as outer class 15 | These are connect by "$" 16 | */ 17 | 18 | class Example2 19 | { 20 | public static void main(String[] args) 21 | { 22 | // Outer Class 23 | OuterClassA ref1 = new OuterClassA(); 24 | System.out.println(ref1.number1); 25 | 26 | // Inner Class 27 | OuterClassA.InnerClassB ref2= ref1.new InnerClassB(); 28 | System.out.println(ref2.number2); 29 | } 30 | } -------------------------------------------------------------------------------- /StringClasses/Example18.java: -------------------------------------------------------------------------------- 1 | class Example18 2 | { 3 | public static void main(String[] args) 4 | { 5 | String str="Sahil"; 6 | 7 | System.out.println(str.charAt(4)); // l 8 | 9 | System.out.println(str.contains("hil")); //true 10 | System.out.println(str.contains("abc")); //false : str not contain these letters 11 | 12 | System.out.println(str.indexOf("h")); //2 13 | System.out.println(str.indexOf("z")); //-1 : str not contain these letters 14 | 15 | System.out.println(str.lastIndexOf("h")); //2 16 | System.out.println(str.lastIndexOf("z")); //-1 : str not contain these letters 17 | } 18 | } 19 | 20 | // public char charAt(int index); 21 | // public int indexOf(int); 22 | //public boolean contains(java.lang.CharSequence); 23 | // public int lastIndexOf(java.lang.String); -------------------------------------------------------------------------------- /Interfaces/Example29.java: -------------------------------------------------------------------------------- 1 | interface It1 2 | { 3 | /* 4 | Default is created here saparate for interface it is not a access modifier and it behave as public by nature. 5 | 6 | To create complete non static method within in interface we have prefix keyword i.e default. 7 | 8 | The default keyword in interface by complete and JVM that it allow to create complete non static method in interface 9 | 10 | The nature of these method will public 11 | */ 12 | default void method() 13 | { 14 | System.out.println("Inside the method of Interface"); 15 | } 16 | } 17 | 18 | class Example29 implements It1 //default methods is in interface are allow to be inherited. 19 | { 20 | public static void main(String[] args) 21 | { 22 | Example29 obj = new Example29(); 23 | obj.method(); 24 | } 25 | } -------------------------------------------------------------------------------- /Polymorphism/RunTime Poly/Example22.java: -------------------------------------------------------------------------------- 1 | class Demo1 2 | { 3 | int number1=10; 4 | void method1() 5 | { 6 | System.out.println("Inside Method - 1 and number 1 is : "+number1); 7 | } 8 | } 9 | class Demo2 extends Demo1 10 | { 11 | int number2=20; 12 | void method2() 13 | { 14 | System.out.println("Inside Method - 2 and number 2 is : "+number2); 15 | } 16 | } 17 | class Test 18 | { 19 | void method3(Demo1 d1) //Upcasting 20 | { 21 | d1.method1(); 22 | //d1.method2(); Error 23 | Demo2 d2 =(Demo2)d1; //Downcasting 24 | System.out.println(); 25 | d2.method1(); 26 | d2.method2(); 27 | 28 | } 29 | } 30 | class Example22 31 | { 32 | public static void main(String[] args) 33 | { 34 | Test.method3(new Demo2()); //create object of Demo2 class Upcasting 35 | } 36 | } -------------------------------------------------------------------------------- /StringClasses/Example7.java: -------------------------------------------------------------------------------- 1 | class Example7 2 | { 3 | public static void main(String[] args) 4 | { 5 | /*StringBuffer str1 = "Sahil"; Compilation Error 6 | 7 | We can not create StringBuffer object directly without new keyword. 8 | 9 | */ 10 | 11 | StringBuffer str1 = new StringBuffer("Sahil"); 12 | System.out.println(str1); 13 | System.out.println(str1.toString()); // toString()--> StringBuffer 14 | 15 | /* 16 | The toString() is called from object class is always be responsible to print address. 17 | 18 | But it is same toString() is called from String or StringBuffer class or any other child class 19 | it will not responsible to print the address but it print the some other value. 20 | */ 21 | } 22 | } 23 | 24 | // public jaav.lang.StringBuffer(java.lang.String); -------------------------------------------------------------------------------- /InnerClasses/NonStaticInnerClass/AnonymousInnerClass/Example21.java: -------------------------------------------------------------------------------- 1 | abstract class Parent 2 | { 3 | abstract void test1(); 4 | abstract void test2(); 5 | } 6 | 7 | class Child 8 | { 9 | Parent pp = new Parent() // Anonymous Class Syntax 10 | // Create object with body 11 | { 12 | void test1() 13 | { 14 | System.out.println("These is the test - 1 method Inside Anonymous Class"); 15 | } 16 | void test2() 17 | { 18 | System.out.println("These is the test - 2 method Inside Anonymous Class"); 19 | } 20 | }; 21 | 22 | } 23 | 24 | class Example21 25 | { 26 | public static void main(String[] args) 27 | { 28 | Child ref = new Child(); 29 | ref.pp.test1(); // access the anonymous class members 30 | ref.pp.test2(); // access the anonymous class members 31 | } 32 | } -------------------------------------------------------------------------------- /SerializationAndDeserialization/CollegeSerial.java: -------------------------------------------------------------------------------- 1 | import java.io.FileOutputStream; 2 | import java.io.ObjectOutputStream; 3 | import java.io.FileNotFoundException; 4 | import java.io.IOException; 5 | 6 | class CollegeSerial 7 | { 8 | public static void main(String[] args) throws FileNotFoundException,IOException 9 | { 10 | Student ss=new Student(101,"Sahil"); 11 | FileOutputStream fos=new FileOutputStream("D:\\Programming\\cybersuccess\\SerializationAndDeserialization\\StudentData.txt"); 12 | ObjectOutputStream oos= new ObjectOutputStream(fos); 13 | oos.writeObject(ss); 14 | } 15 | 16 | } 17 | 18 | //public FileOutputStream(String name) throws FileNotFoundException 19 | //public ObjectOutputStream(OutputStream out) throws IOException 20 | //public final void writeObject(Object obj) throws IOException 21 | -------------------------------------------------------------------------------- /ExceptionHandling/CustomeCheckException2.java: -------------------------------------------------------------------------------- 1 | class InvalidAmountTransfer extends Exception 2 | { 3 | InvalidAmountTransfer(String exceptionInfo) 4 | { 5 | super(exceptionInfo); 6 | } 7 | } 8 | 9 | class Flipkart 10 | { 11 | int productPrice=5000; 12 | void byProduct(int custemerPrice) throws InvalidAmountTransfer 13 | { 14 | if (productPrice==custemerPrice) 15 | { 16 | System.out.println("Amount transfer Successfull..."); 17 | } 18 | else 19 | { 20 | throw new InvalidAmountTransfer("Amount transfer Fail..."); 21 | } 22 | } 23 | } 24 | 25 | class CustomeCheckException2 26 | { 27 | public static void main(String[] args) throws InvalidAmountTransfer 28 | { 29 | Flipkart cust1=new Flipkart(); 30 | cust1.byProduct(2000); 31 | } 32 | } 33 | /* 34 | args check exception 35 | */ -------------------------------------------------------------------------------- /Abstraction/Example4.java: -------------------------------------------------------------------------------- 1 | abstract class Demo1 2 | { 3 | abstract void method1(); 4 | abstract void method2(); 5 | abstract void method3(); 6 | } 7 | 8 | class Example4 extends Demo1 9 | { 10 | void method1() 11 | { 12 | System.out.println("These is the method -- 1"); 13 | } 14 | 15 | void method2() 16 | { 17 | System.out.println("These is the method -- 2"); 18 | } 19 | /* 20 | 21 | If in child class all the abstract methods are not implements 22 | or one of them is also not implemented then child class by nature will be abstract class. 23 | And it throw an error 24 | "Example4 is not abstract and does not override abstract method method3() in Demo1" 25 | 26 | */ 27 | public static void main(String[] args) 28 | { 29 | Example4 ref = new Example4(); 30 | ref.method1(); 31 | } 32 | } -------------------------------------------------------------------------------- /Inheritance/Example17.java: -------------------------------------------------------------------------------- 1 | class Demo 2 | { 3 | Demo(int a) // through an error 4 | { 5 | System.out.println("Constructor 0 argument of Demo -- 1"); 6 | } 7 | } 8 | 9 | class Test extends Demo 10 | { 11 | Test() 12 | { 13 | /* the super() [statement or method] is used to call 14 | another constructor within the super class (Another class). 15 | 16 | If we do not add the super() then compiler add it automatically while compiling 17 | 18 | Whenever compiler add super() it is responsible to call the 0 arguments constructor only. 19 | Argument constructor are not called 20 | */ 21 | System.out.println("Constructor 0 argument of Test -- 2"); 22 | } 23 | } 24 | class Example17 25 | { 26 | public static void main(String args[]) 27 | { 28 | new Test(); // nameless object creation 29 | } 30 | } -------------------------------------------------------------------------------- /ExceptionHandling/Example10.java: -------------------------------------------------------------------------------- 1 | class Example10 2 | { 3 | public static void main(String[] args) 4 | { 5 | try 6 | { 7 | System.out.println("These is the try block... Before"); // Execute 8 | System.out.println(10/0); // ArithmeticException NOT Execute 9 | System.out.println("Control Back after execution... After"); // NOT Execute After control goes out of the try block it will never return back to try block 10 | } 11 | catch (ArithmeticException ae) 12 | { 13 | System.out.println("These is the catch block..."); // Execute 14 | //System.out.println(10/2); // Execute 15 | } 16 | } 17 | } 18 | 19 | /* 20 | Never ever used generic statements . 21 | 22 | Never ever write statements after the exception. 23 | 24 | After control goes out of the try block it will never return back to try block 25 | */ -------------------------------------------------------------------------------- /Non_Static_Blocks/Example9.java: -------------------------------------------------------------------------------- 1 | class DemoAnother 2 | { 3 | int number1 , number2 , result; // instance members 4 | { 5 | result=number1+number2; 6 | System.out.println("Non static block "+result); 7 | int number1 =19 , number2 = 2 ; 8 | result=number1+number2; 9 | } 10 | 11 | DemoAnother() 12 | { 13 | //result = number1+number2; 14 | System.out.println("0 Parameter constructor "+result); 15 | } 16 | 17 | } 18 | 19 | 20 | 21 | class Example9 22 | { 23 | /* 24 | 25 | 1. Non static variables are use to initialize the non static or instance members/variables. 26 | 2. Instance blocks are used to the implements the bussiness logic during object creation 27 | 28 | */ 29 | 30 | 31 | public static void main(String[] args) 32 | { 33 | DemoAnother obj1 =new DemoAnother(); 34 | 35 | } 36 | } -------------------------------------------------------------------------------- /Polymorphism/RunTime Poly/Example21.java: -------------------------------------------------------------------------------- 1 | class Demo1 2 | { 3 | int number1=10; 4 | void method1() 5 | { 6 | System.out.println("Inside Method - 1 and number 1 is : "+number1); 7 | } 8 | } 9 | class Demo2 extends Demo1 10 | { 11 | int number2=20; 12 | void method2() 13 | { 14 | System.out.println("Inside Method - 2 and number 2 is : "+number2); 15 | } 16 | } 17 | class Test 18 | { 19 | void method3(Demo1 d1) //Upcasting 20 | { 21 | d1.method1(); 22 | //d1.method2(); Error 23 | Demo2 d2 =(Demo2)d1; //Downcasting 24 | System.out.println(); 25 | d2.method1(); 26 | d2.method2(); 27 | 28 | } 29 | } 30 | class Example21 31 | { 32 | public static void main(String[] args) 33 | { 34 | Test ref = new Test(); 35 | ref.method3(new Demo2()); //create object of Demo2 class Upcasting 36 | } 37 | } -------------------------------------------------------------------------------- /StringClasses/Example5.java: -------------------------------------------------------------------------------- 1 | class String1 extends Object // extends Object (Optional) 2 | { 3 | String str; 4 | String1(String str) // class type 5 | { 6 | this.str=str; 7 | } 8 | //Syntax 9 | public String toString() 10 | { 11 | return str; 12 | } 13 | } 14 | class Example5 15 | { 16 | public static void main(String[] args) 17 | { 18 | Example5 ref1 = new Example5(); 19 | System.out.println(ref1); // Print address 20 | 21 | String str = new String("Sahil"); 22 | System.out.println(str); 23 | 24 | String1 ref2 = new String1("Shaikh"); 25 | System.out.println(ref2); 26 | 27 | /* 28 | Whenever the reference variable of String class is printed. 29 | It will internally call the toString() of the String class. 30 | And these is print the value rather than address. 31 | */ 32 | } 33 | } -------------------------------------------------------------------------------- /Abstraction/Example14.java: -------------------------------------------------------------------------------- 1 | abstract class Demo2 2 | { 3 | int number; 4 | static 5 | { 6 | System.out.println("These is the Static block "); 7 | 8 | } 9 | { 10 | System.out.println("These is the instance block "+number); 11 | number= 100; 12 | } 13 | Demo2() 14 | { 15 | // super() by default present 16 | System.out.println("The value of Number is : "+number); 17 | number=10; 18 | } 19 | void method() 20 | { 21 | System.out.println("Method And value is : "+number); 22 | } 23 | } 24 | class Example14 extends Demo2 25 | { 26 | 27 | public static void main(String[] args) 28 | { 29 | // nameless object creation 30 | new Example14().method(); 31 | } 32 | } 33 | 34 | /* 35 | Sequence of execution: 36 | 37 | static 38 | non static 39 | constructor 40 | methiod 41 | . 42 | . 43 | . 44 | */ -------------------------------------------------------------------------------- /Abstraction/Example5.java: -------------------------------------------------------------------------------- 1 | abstract class Demo1 2 | { 3 | abstract void method1(); 4 | abstract void method2(); 5 | abstract void method3(); 6 | } 7 | 8 | class Example5 extends Demo1 9 | { 10 | void method1() 11 | { 12 | System.out.println("These is the method -- 1"); 13 | } 14 | 15 | void method2() 16 | { 17 | System.out.println("These is the method -- 2"); 18 | } 19 | void method3() 20 | { 21 | System.out.println("These is the method -- 3"); 22 | } 23 | 24 | /* 25 | To make child class is a normal class we must to implements the all 26 | abstract method within it. Otherwise child class it by nature abstract class. 27 | */ 28 | 29 | public static void main(String[] args) 30 | { 31 | Example5 ref = new Example5(); 32 | ref.method1(); 33 | ref.method2(); 34 | ref.method3(); 35 | } 36 | } -------------------------------------------------------------------------------- /Collections/ArrayList18.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | 6 | public class ArrayList18 { 7 | 8 | public static void main(String[] args) 9 | { 10 | ArrayList al=new ArrayList(); 11 | al.add(new Emp(101,"sahil")); 12 | al.add(new Emp(102,"shaikh")); 13 | al.add(new Emp(103,"dev")); 14 | 15 | //System.out.println(al); printing the address 16 | 17 | Iterator i=al.iterator(); 18 | while(i.hasNext()) 19 | { 20 | Emp e=(Emp)i.next(); 21 | System.out.println(e.empId+" - "+e.empName); 22 | } 23 | System.out.println("-------------"); 24 | Iterator ii=al.iterator(); 25 | while(ii.hasNext()) 26 | { 27 | Emp ee=ii.next(); 28 | System.out.println(ee.empId+" - "+ee.empName); 29 | 30 | } 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Collections/ArrayList28.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | 6 | public class ArrayList28 { 7 | 8 | public static void main(String[] args) 9 | { 10 | 11 | ArrayList al=new ArrayList(); 12 | 13 | al.add(new Zomato(101,"pizza")); 14 | al.add(new Zomato(401,"burger")); 15 | al.add(new Zomato(301,"paneer")); 16 | al.add(new Zomato(201,"nan")); 17 | 18 | System.out.println(al); 19 | for(Zomato snc : al) 20 | { 21 | System.out.println(snc.itemId+" "+snc.itemName); 22 | } 23 | System.out.println("----------"); 24 | Collections.sort(al , new ZomatoNameComparator()); // Sort alphabetically 25 | 26 | for(Zomato z1 : al) 27 | { 28 | System.out.println(z1.itemId+" "+z1.itemName); 29 | } 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Collections/LinkedHashMap3.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.LinkedHashMap; 4 | 5 | public class LinkedHashMap3 { 6 | public static void main(String[] args) { 7 | LinkedHashMap lhm1=new LinkedHashMap(10,0.75f,false ); 8 | lhm1.put(101, "oggy"); 9 | lhm1.put(102, "jack"); 10 | lhm1.put(103, "motu"); 11 | lhm1.put(104, "patlu"); 12 | System.out.println("Before : "+lhm1); 13 | lhm1.get(103); 14 | System.out.println("After : "+lhm1); 15 | 16 | LinkedHashMap lhm2=new LinkedHashMap(10,0.75f,true ); 17 | lhm2.put(101, "bheem"); 18 | lhm2.put(102, "chutki"); 19 | lhm2.put(103, "jaggu"); 20 | lhm2.put(104, "raju"); 21 | System.out.println("Before : "+lhm2); 22 | lhm2.get(101); // print at the last 101 - bheem 23 | System.out.println("After : "+lhm2); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Interfaces/Example20.java: -------------------------------------------------------------------------------- 1 | interface Interface1 2 | { 3 | void method1(); 4 | void method2(); // Overrides the methods 5 | } 6 | 7 | interface Interface2 8 | { 9 | void method2(); // Overrides the methods 10 | void method3(); 11 | } 12 | class Example20 implements Interface1 , Interface2 // Multiple inheritance allowed by interface 13 | { 14 | public void method1() 15 | { 16 | System.out.println("These is the method - 1"); 17 | } 18 | public void method2() // Overrides the methods 19 | { 20 | System.out.println("These is the method - 2"); 21 | } 22 | public void method3() 23 | { 24 | System.out.println("These is the method - 3"); 25 | } 26 | public static void main(String[] args) 27 | { 28 | Example20 obj = new Example20(); 29 | obj.method1(); 30 | obj.method2(); 31 | obj.method3(); 32 | } 33 | } -------------------------------------------------------------------------------- /Collections/ArrayList24.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | 6 | public class ArrayList24 { 7 | 8 | public static void main(String[] args) 9 | { 10 | ArrayList al= new ArrayList(); 11 | al.add("motu"); 12 | al.add("patlu"); 13 | al.add("jhataka"); 14 | al.add("ghasita"); 15 | System.out.println(al); 16 | 17 | Collections.sort(al); // sort the string in a - z manner 18 | 19 | // sort() internally responsible to call the compareTo() method from String class. 20 | // String class implements as interface called Comparable. 21 | // Comparable interface contains the compareTo() 22 | // Hence String classes and all wrapper classes implements comparable interface 23 | 24 | 25 | System.out.println(al); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /StaticAndNonStatic/StaticVMInStaticAreaInMClass.java: -------------------------------------------------------------------------------- 1 | class AnotherClass 2 | { 3 | static char name='A'; 4 | 5 | static void myMethod() 6 | { 7 | System.out.println("Static member in another class access by another class..."); 8 | } 9 | } 10 | 11 | /* 12 | 13 | Static variables , Static methods are declare and initialize in one class and accessed in another class 14 | 15 | 1. by class name 16 | 2. by object creation 17 | 18 | */ 19 | 20 | class StaticVMInStaticAreaInMClass 21 | { 22 | public static void main(String args[]) 23 | { 24 | // by class name 25 | System.out.println(AnotherClass.name); 26 | AnotherClass.myMethod(); 27 | System.out.println(); 28 | 29 | // by object creation 30 | AnotherClass refVar =new AnotherClass(); 31 | System.out.println(refVar.name); 32 | refVar.myMethod(); 33 | } 34 | } -------------------------------------------------------------------------------- /ExceptionHandling/CustomeUnCheckException2.java: -------------------------------------------------------------------------------- 1 | class InvalidAmountTransfer extends RuntimeException 2 | { 3 | InvalidAmountTransfer(String exceptionInfo) 4 | { 5 | super(exceptionInfo); 6 | } 7 | } 8 | 9 | class Flipkart 10 | { 11 | int productPrice=5000; 12 | void byProduct(int custemerPrice) throws InvalidAmountTransfer 13 | { 14 | if (productPrice==custemerPrice) 15 | { 16 | System.out.println("Amount transfer Successfull..."); 17 | } 18 | else 19 | { 20 | throw new InvalidAmountTransfer("Amount transfer Fail..."); 21 | } 22 | } 23 | } 24 | 25 | 26 | /* 27 | args Uncheck exception 28 | */ 29 | 30 | class CustomeUnCheckException2 31 | { 32 | public static void main(String[] args) throws InvalidAmountTransfer 33 | { 34 | Flipkart cust1=new Flipkart(); 35 | cust1.byProduct(2000); 36 | } 37 | } -------------------------------------------------------------------------------- /Polymorphism/Compile Time Poly/Example4.java: -------------------------------------------------------------------------------- 1 | class Amazon 2 | { 3 | void payment(float debitCreditCard) 4 | { 5 | System.out.println("Logic to implements transaction of Debit / Credit card --- 1"); 6 | } 7 | void payment(String upi) 8 | { 9 | System.out.println("Logic to implements upi id --- 2"); 10 | } 11 | void payment(int cash) 12 | { 13 | System.out.println("Logic to implements transaction of cash --- 3"); 14 | } 15 | } 16 | 17 | class Example4 18 | { 19 | 20 | /* 21 | Q. Why it is called compile time polymorphism? 22 | Ans: Because The binding call which method is done during compilation process. 23 | */ 24 | 25 | public static void main(String[] args) 26 | { 27 | Amazon custmer = new Amazon(); 28 | custmer.payment("sahil@123"); // it decide which method is to be call by passing the argumnets. 29 | } 30 | } -------------------------------------------------------------------------------- /StarPattern8.java: -------------------------------------------------------------------------------- 1 | class StarPattern8 2 | { 3 | 4 | /* 5 | 6 | * 7 | ** 8 | *** 9 | **** 10 | ***** 11 | **** 12 | *** 13 | ** 14 | * 15 | 16 | 17 | */ 18 | 19 | public static void main(String args[]) 20 | { 21 | for (int column=1;column<=5;column++) 22 | { 23 | for (int space=4;space>=column;space--) 24 | { 25 | System.out.print(" "); 26 | } 27 | 28 | for (int row=1;row<=column ;row++) 29 | { 30 | System.out.print("*"); 31 | } 32 | System.out.println(); 33 | } 34 | 35 | for (int column=1;column<=4 ;column++ ) 36 | { 37 | for (int space=1;space<=column;space++ ) 38 | { 39 | System.out.print(" "); 40 | } 41 | for (int row=4;row>=column ;row--) 42 | { 43 | System.out.print("*"); 44 | } 45 | System.out.println(); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /Polymorphism/RunTime Poly/Example16.java: -------------------------------------------------------------------------------- 1 | 2 | class Sample2 3 | { 4 | Sample2 test() 5 | { 6 | System.out.println("I am class Sample - 2"); 7 | return new Sample2(); // It is possible to override if parent method return type and child method return type are different at class level 8 | } 9 | } 10 | 11 | class Sample3 extends Sample2 12 | { 13 | Sample3 test() // It is possible to override if parent method return type and child method return type are different at class level 14 | { 15 | System.out.println("I am class Sample - 3"); 16 | return new Sample3(); // It is possible to override if parent method return type and child method return type are different at class level 17 | } 18 | } 19 | class Example16 20 | { 21 | public static void main(String args[]) 22 | { 23 | Sample3 obj = new Sample3(); 24 | obj.test(); 25 | } 26 | } -------------------------------------------------------------------------------- /PrimeNumber.java: -------------------------------------------------------------------------------- 1 | class PrimeNumber 2 | { 3 | public static void main(String[] args) 4 | { 5 | //System.out.println("Hello World!"); 6 | 7 | int number =8; 8 | int temp = 0; // Temporary variable used and assign the value to avoid the printing of repesting statement in for loop 9 | 10 | for(int i =2; i<=number-1; i++) // Number is greater than 1 (prime number divisible by 1 and itself so i start from 2 till number - 1) 11 | { 12 | if(number%i==0) // If any number is divided by the between any number so it is not prime number 13 | { 14 | temp=temp+1; // Reference is here of Temp variable 15 | } 16 | } 17 | 18 | if(temp>0) // And here [temp] 19 | { 20 | System.out.println("Number is not Prime"); 21 | } 22 | else 23 | { 24 | System.out.println("Number is Prime"); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /StaticAndNonStatic/StaticVarMethInStaticArea.java: -------------------------------------------------------------------------------- 1 | class StaticVarMethInStaticArea 2 | { 3 | /* 4 | In the same class static variables , methods are accessed by 3 ways 5 | but object creation is not prefered in the same class. 6 | */ 7 | static int number=10; 8 | 9 | static void myMethod() 10 | { 11 | System.out.println("Inside the static myMethod..."); 12 | } 13 | public static void main(String[] args) 14 | { 15 | // directlly way 16 | System.out.println(number); 17 | myMethod(); 18 | 19 | // by class name 20 | System.out.println(StaticVarMethInStaticArea.number); 21 | StaticVarMethInStaticArea.myMethod(); 22 | 23 | // by object creation but not prefered 24 | StaticVarMethInStaticArea referencevar=new StaticVarMethInStaticArea(); 25 | System.out.println(referencevar.number); 26 | referencevar.myMethod(); 27 | } 28 | } -------------------------------------------------------------------------------- /StringClasses/Example3.java: -------------------------------------------------------------------------------- 1 | class Example3 2 | { 3 | public static void main(String[] args) 4 | { 5 | // approach -1 : without new keyword 6 | String first="vdf"; 7 | String second="vdf"; 8 | String third="engineering"; 9 | System.out.println(first==second); // return true 10 | System.out.println(second==third); // return false 11 | System.out.println(third==first); // return false 12 | 13 | System.out.println(); 14 | 15 | // approach -2 : with new keyword 16 | String fourth = new String("vdf"); 17 | String fifth = new String("engineering"); 18 | String sixth = new String("engineering"); 19 | System.out.println(sixth==fourth); // return false 20 | System.out.println(fourth==fifth); // return false 21 | System.out.println(fifth==sixth); // return false 22 | } 23 | } 24 | // public jaav.lang.String(java.lang.String); -------------------------------------------------------------------------------- /Collections/ArrayList22.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | public class ArrayList22 { 8 | 9 | public static void main(String[] args) 10 | { 11 | // convert Array to Collection 12 | 13 | // 1st Approach by asList() method --> List 14 | 15 | String[] arr= {"motu","patlu","jhatka","ghasita"}; 16 | List ls=Arrays.asList(arr); 17 | 18 | for(String s: arr) 19 | { 20 | System.out.println(s); 21 | } 22 | 23 | // 2nd approach constructor base --> ArrayList 24 | String[] arr2= {"bheem","jaggu","raju","kalia"}; 25 | ArrayList al= new ArrayList(Arrays.asList(arr2)); 26 | System.out.println(al.size()); 27 | 28 | for(String s: arr2) 29 | { 30 | System.out.println(s); 31 | } 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Inheritance/Example24.java: -------------------------------------------------------------------------------- 1 | class Demo1 2 | { 3 | { 4 | System.out.println("This is the Instance Block - 1 - Demo1"); 5 | } 6 | 7 | /* 8 | Compiler by default create a constructor and also super() within it. 9 | So constructor first call then instance block is checked and executed and then continue 10 | */ 11 | } 12 | class Test1 extends Demo1 13 | { 14 | Test1() 15 | { 16 | // super by compiler 17 | System.out.println("This is the 0 args Constructor - Test1"); 18 | } 19 | Test1(int a) 20 | { 21 | // super by compiler 22 | System.out.println("This is the 1 args Constructor - Test1"); 23 | } 24 | 25 | /* 26 | Every Constructor may be set super() when will execute the parent constructor 27 | */ 28 | } 29 | class Example24 30 | { 31 | public static void main(String args[]) 32 | { 33 | new Test1(); 34 | new Test1(10); 35 | } 36 | } -------------------------------------------------------------------------------- /Polymorphism/RunTime Poly/Example11.java: -------------------------------------------------------------------------------- 1 | class Papa 2 | { 3 | void method() 4 | { 5 | System.out.println("These is the method inside the --- Papa"); 6 | } 7 | } 8 | 9 | class Beta extends Papa 10 | { 11 | void method() 12 | { 13 | System.out.println("These is the method inside the --- Beta"); 14 | } 15 | } 16 | 17 | /* 18 | Overrinding is the process where child and Parent has same objective , 19 | the child will inherites the parent objective but will change the behavior is called Overriding. 20 | 21 | 1. Minimus we require two classes 22 | 2. There should be is a relationship between both classes. 23 | 3. The parent class method signature and child class method signature should same 24 | */ 25 | 26 | class Example11 27 | { 28 | public static void main(String[] args) 29 | { 30 | Beta b = new Beta(); 31 | b.method(); 32 | } 33 | } -------------------------------------------------------------------------------- /StarPattern10.java: -------------------------------------------------------------------------------- 1 | class StarPattern10 2 | { 3 | 4 | /* 5 | 6 | * 7 | * * 8 | * * * 9 | * * * * 10 | * * * * * 11 | * * * * 12 | * * * 13 | * * 14 | * 15 | 16 | */ 17 | 18 | 19 | public static void main(String[] args) 20 | { 21 | for (int column=1;column<=5 ;column++ ) 22 | { 23 | for (int space =4;space>=column ;space--) 24 | { 25 | System.out.print(" "); 26 | } 27 | for (int row =1; row<=column;row++ ) 28 | { 29 | System.out.print("* "); 30 | } 31 | System.out.println(); 32 | } 33 | 34 | for (int column=4;column>=1;column--) 35 | { 36 | for (int space=4;space>=column;space-- ) 37 | { 38 | System.out.print(" "); 39 | } 40 | for (int row=1;row<=column;row++) 41 | { 42 | System.out.print("* "); 43 | } 44 | System.out.println(); 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /StarPattern9.java: -------------------------------------------------------------------------------- 1 | class StarPattern9 2 | { 3 | 4 | /* 5 | 6 | * 7 | ** 8 | *** 9 | **** 10 | ***** 11 | **** 12 | *** 13 | ** 14 | * 15 | 16 | */ 17 | 18 | 19 | public static void main(String[] args) 20 | { 21 | 22 | for (int column=1;column<=5 ;column++ ) 23 | { 24 | for (int space =4;space>=column ;space--) 25 | { 26 | System.out.print(" "); 27 | } 28 | for (int row =1; row<=column;row++ ) 29 | { 30 | System.out.print("*"); 31 | } 32 | System.out.println(); 33 | } 34 | 35 | for (int column=4;column>=1;column--) 36 | { 37 | 38 | for (int space=4;space>=column;space-- ) 39 | { 40 | System.out.print(" "); 41 | } 42 | for (int row=1;row<=column;row++) 43 | { 44 | System.out.print("*"); 45 | } 46 | System.out.println(); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Polymorphism/RunTime Poly/Example15.java: -------------------------------------------------------------------------------- 1 | class Sample1 2 | { 3 | 4 | } 5 | class Sample2 6 | { 7 | Sample1 test() 8 | { 9 | System.out.println("I am class Sample - 2"); 10 | return new Sample1(); // It is possible to override if parent method return type and child method return type are same at class level 11 | } 12 | } 13 | 14 | class Sample3 extends Sample2 15 | { 16 | Sample1 test() // It is possible to override if parent method return type and child method return type are same at class level 17 | { 18 | System.out.println("I am class Sample - 3"); 19 | return new Sample1(); // It is possible to override if parent method return type and child method return type are same at class level 20 | } 21 | } 22 | class Example15 23 | { 24 | public static void main(String args[]) 25 | { 26 | Sample3 obj = new Sample3(); 27 | obj.test(); 28 | } 29 | } -------------------------------------------------------------------------------- /Collections/TreeMap4.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.Iterator; 4 | import java.util.Map; 5 | import java.util.Set; 6 | import java.util.TreeMap; 7 | 8 | public class TreeMap4 { 9 | 10 | public static void main(String[] args) { 11 | TreeMap tm1=new TreeMap(new TreeMap4EmployeeComparator()); 12 | tm1.put(new Employee(101, "sahil"), "developer"); 13 | tm1.put(new Employee(103, "jac"), "tester"); 14 | tm1.put(new Employee(105, "lio"), "hr"); 15 | tm1.put(new Employee(104, "sam"), "manager"); 16 | 17 | Set ss=tm1.entrySet(); 18 | Iterator ii=ss.iterator(); 19 | while(ii.hasNext()) 20 | { 21 | Map.Entry me=(Map.Entry)ii.next(); 22 | Employee ee=(Employee)me.getKey(); 23 | System.out.print(ee.empId+" - "+ee.empName+" == "); 24 | Object value= me.getValue(); 25 | System.out.println(value); 26 | 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /InnerClasses/NonStaticInnerClass/AnonymousInnerClass/Example19.java: -------------------------------------------------------------------------------- 1 | class Example19 2 | { 3 | /* 4 | These is the type of Non Static Inner class 5 | 6 | It is called as nameless class. 7 | 8 | Nameless classes means we do not give name but compiler give name is called Anonymous Class 9 | */ 10 | 11 | static boolean status=true; 12 | static void m1() 13 | { 14 | System.out.println("Inside m-1 method..."+status); 15 | } 16 | static class Test 17 | { 18 | static void method1() 19 | { 20 | System.out.println("Inside method-1 of Test Class"); 21 | m1(); 22 | } 23 | void method2() 24 | { 25 | System.out.println("Inside method-2 of Test Class"); 26 | m1(); 27 | } 28 | } 29 | public static void main(String[] args) 30 | { 31 | Example19.Test.method1(); 32 | new Example19.Test().method2(); // OR new Test().method2(); 33 | } 34 | } -------------------------------------------------------------------------------- /Polymorphism/RunTime Poly/Example12.java: -------------------------------------------------------------------------------- 1 | class Papa 2 | { 3 | void method() 4 | { 5 | System.out.println("These is the method inside the --- Papa"); 6 | } 7 | } 8 | 9 | class Beta extends Papa 10 | { 11 | void method(int money) 12 | { 13 | System.out.println("These is the method inside the --- Beta"); 14 | } 15 | } 16 | 17 | /* 18 | Overrinding is the process where child and Parent has same objective , 19 | the child will inherites the parent objective but will change the behavior is called Overriding. 20 | 21 | 1. Minimus we require two classes 22 | 2. There should be is a relationship between both classes. 23 | 3. The parent class method signature and child class method signature should same 24 | */ 25 | 26 | class Example12 27 | { 28 | public static void main(String[] args) 29 | { 30 | Beta b = new Beta(); 31 | b.method(1000); 32 | } 33 | } -------------------------------------------------------------------------------- /Collections/LinkedHashSet2.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.LinkedHashSet; 4 | 5 | public class LinkedHashSet2 { 6 | 7 | public static void main(String[] args) { 8 | 9 | LinkedHashSet lhs1=new LinkedHashSet(); 10 | lhs1.add(10); 11 | lhs1.add(20); 12 | lhs1.add(30); 13 | lhs1.add(40); 14 | System.out.println(lhs1); // insertion order is not maintain 15 | 16 | LinkedHashSet lhs2=new LinkedHashSet(lhs1); // add one HashSet to another using Constructor based approach 17 | lhs2.add(100); 18 | lhs2.add(200); 19 | lhs2.add(300); 20 | lhs2.add(400); 21 | System.out.println(lhs2); 22 | 23 | LinkedHashSet lhs3=new LinkedHashSet(); 24 | lhs3.addAll(lhs1); // Using addAll(); 25 | lhs3.add(1000); 26 | lhs3.add(2000); 27 | lhs3.add(3000); 28 | lhs3.add(4000); 29 | System.out.println(lhs3); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /ExceptionHandling/Example19.java: -------------------------------------------------------------------------------- 1 | class Example19 2 | { 3 | public static void main(String[] args) 4 | { 5 | int[] a = {1,2,3,4,5}; 6 | try 7 | { 8 | System.out.println("Inside the try block --- 1"); 9 | System.out.println(10/0); 10 | System.out.println(a[10]); 11 | } 12 | catch(Exception e) 13 | { 14 | System.out.println("Inside Exception..."); 15 | } 16 | catch (ArithmeticException e) 17 | { 18 | System.out.println("Inside ArithmeticException ..."); 19 | } 20 | 21 | } 22 | } 23 | 24 | /* 25 | Multiple catch blocks are not allowed if the order of the catch block are from parent to child. 26 | 27 | All exception will handled in the first catch block (Exception e) thats why error generate. 28 | 29 | Example19.java:16: error: exception ArithmeticException has already been caught 30 | catch (ArithmeticException e) 31 | */ -------------------------------------------------------------------------------- /ExceptionHandling/Shadi1.java: -------------------------------------------------------------------------------- 1 | class Shadi1 2 | { 3 | void govtMarriegeOfficer() 4 | { 5 | System.out.println("Inside Government Marriege Officer method..."); 6 | try 7 | { 8 | System.out.println("Internet not working , but we will try to fix and handle..."); 9 | Thread.sleep(3000); 10 | } 11 | catch (InterruptedException ie) 12 | { 13 | System.out.println("we will try another way to fix and handle..."); 14 | } 15 | } 16 | 17 | void marriegeAgent() 18 | { 19 | System.out.println("Inside Marriege Agent method..."); 20 | govtMarriegeOfficer(); 21 | } 22 | 23 | void govtMarriegeOffice() 24 | { 25 | System.out.println("Inside Government Marriege Officer method..."); 26 | marriegeAgent(); 27 | } 28 | 29 | public static void main(String[] args) 30 | { 31 | Shadi1 couple = new Shadi1(); 32 | couple.govtMarriegeOffice(); 33 | } 34 | } -------------------------------------------------------------------------------- /ExceptionHandling/Shadi2.java: -------------------------------------------------------------------------------- 1 | class Shadi2 2 | { 3 | void govtMarriegeOfficer() 4 | { 5 | System.out.println("Inside Government Marriege Officer method..."); 6 | Thread.sleep(3000); 7 | /* 8 | Shadi2.java:6: error: unreported exception InterruptedException; must be caught or declared to be thrown 9 | Thread.sleep(3000); 10 | ^ 11 | 1 error 12 | error: compilation failed 13 | */ 14 | } 15 | 16 | void marriegeAgent() 17 | { 18 | System.out.println("Inside Marriege Agent method..."); 19 | govtMarriegeOfficer(); 20 | } 21 | 22 | void govtMarriegeOffice() 23 | { 24 | System.out.println("Inside Government Marriege Officer method..."); 25 | marriegeAgent(); 26 | } 27 | 28 | public static void main(String[] args) 29 | { 30 | Shadi2 couple = new Shadi2(); 31 | couple.govtMarriegeOffice(); 32 | } 33 | } -------------------------------------------------------------------------------- /Collections/ArrayList16.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | 6 | public class ArrayList16 { 7 | 8 | public static void main(String[] args) 9 | { 10 | // Normal Collection 11 | ArrayList al=new ArrayList(); 12 | al.add("101"); 13 | al.add("sahil"); 14 | al.add("prog"); 15 | System.out.println(al); 16 | Iterator i=al.iterator(); 17 | while (i.hasNext()) 18 | { 19 | String s = (String) i.next(); 20 | System.out.println(s); 21 | 22 | } 23 | 24 | 25 | 26 | // Generic Collection 27 | ArrayList a2=new ArrayList(); 28 | a2.add("100"); 29 | a2.add("shaikh"); 30 | a2.add("dev"); 31 | System.out.println(a2); 32 | Iterator ii=a2.iterator(); 33 | while (ii.hasNext()) { 34 | System.out.println((String)ii.next()); 35 | } 36 | 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Inheritance/Example1.java: -------------------------------------------------------------------------------- 1 | class Test 2 | { 3 | int number1 = 10; 4 | } 5 | 6 | class Example1 extends Test 7 | { 8 | // extends keyword is used for the inherites the members of another class members 9 | float number2=12.5f; 10 | public static void main(String[] args) 11 | { 12 | // Access only the Test class members 13 | Test obj1 = new Test(); 14 | System.out.println("----------Parent class----------"); 15 | System.out.println("The value of number - 1 is : "+obj1.number1); 16 | //System.out.println("The value of number - 1 is : "+obj1.number2); 17 | 18 | // Access both class members (Example1 class is child of Test class) 19 | Example1 obj2 = new Example1(); 20 | System.out.println("----------Child class----------"); 21 | System.out.println("The value of number - 1 is : "+obj2.number1); 22 | System.out.println("The value of number - 1 is : "+obj2.number2); 23 | } 24 | } -------------------------------------------------------------------------------- /Non_Static_Blocks/Example10.java: -------------------------------------------------------------------------------- 1 | class Example10 2 | { 3 | int number; 4 | { 5 | System.out.println("Instance Block And value of number is : "+number); 6 | number = 21; 7 | System.out.println("Instance Block And value of number is : "+number); 8 | } 9 | 10 | Example10() 11 | { 12 | System.out.println("0 Arguments parameters And value of number is : "+number); 13 | number = 12; 14 | System.out.println("0 Arguments parameters And value of number is : "+number); 15 | } 16 | 17 | Example10(int num) 18 | { 19 | System.out.println("1 Arguments parameters And value of number is : "+number); 20 | number = 22; 21 | System.out.println("1 Arguments parameters And value of number is : "+number); 22 | } 23 | 24 | 25 | public static void main(String args[]) 26 | { 27 | Example10 obj1= new Example10(); 28 | Example10 obj2= new Example10(9); 29 | 30 | } 31 | } -------------------------------------------------------------------------------- /Polymorphism/RunTime Poly/Example32.java: -------------------------------------------------------------------------------- 1 | class ParentClass 2 | { 3 | public void m1() // it is not possible to override if access level is in Descresing level 4 | { 5 | System.out.println("Inside Private method -- M1 -- Parent"); 6 | } 7 | } 8 | 9 | class ChildClass extends ParentClass 10 | { 11 | protected void m1() //it is notpossible to override if access level is in Descresing level 12 | { 13 | System.out.println("Inside Private method -- M1 -- Child"); 14 | } 15 | } 16 | 17 | /* 18 | 19 | Increasing Order: 20 | 1.Private 21 | 2.default 22 | 3.protected 23 | 4.public 24 | 25 | Descrising Order: 26 | 1.public 27 | 2.protected 28 | 3.default 29 | 4.private 30 | */ 31 | 32 | 33 | class Example32 34 | { 35 | public static void main(String[] args) 36 | { 37 | //Upcasting 38 | ParentClass pp = new ChildClass(); 39 | pp.m1(); // error 40 | } 41 | } -------------------------------------------------------------------------------- /SwapNumber.java: -------------------------------------------------------------------------------- 1 | class SwapNumber 2 | { 3 | 4 | // Swap value of number using third variable or temp variable 5 | 6 | public static void main(String args[]) 7 | { 8 | int number1=10,number2=15; //initialization and declaration 9 | int temp=0; //swap using temporary variable 10 | 11 | System.out.println("The number1 is before swapping : " +number1); //initial value of number1 is 10 12 | System.out.println("The number2 is before swapping : " +number2); //initial value of number2 is 15 13 | 14 | temp=number1; //move value of number1 in temp 15 | number1=number2; //value of number2 is assign to number1 so now number1 = 15 16 | number2=temp; //value of temp is assign to number2 so now number2 = 10 17 | 18 | System.out.println("The number1 is After swapping : " +number1); 19 | System.out.println("The number2 is After swapping : " +number2); 20 | 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Collections/ArrayList17.java: -------------------------------------------------------------------------------- 1 | package collections; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | 6 | public class ArrayList17 { 7 | 8 | public static void main(String[] args) 9 | { 10 | // Normal Collection 11 | ArrayList al=new ArrayList(); 12 | al.add("101"); 13 | al.add("sahil"); 14 | al.add("prog"); 15 | System.out.println(al); 16 | Iterator i=al.iterator(); 17 | while (i.hasNext()) 18 | { 19 | String s = (String) i.next(); 20 | System.out.println(s); 21 | 22 | } 23 | 24 | 25 | 26 | // Generic Collection 27 | ArrayList a2=new ArrayList(); 28 | a2.add("100"); 29 | a2.add("shaikh"); 30 | a2.add("dev"); 31 | System.out.println(a2); 32 | // Generic Cursor 33 | Iterator ii=a2.iterator(); 34 | while (ii.hasNext()) { 35 | System.out.println(ii.next()); 36 | } 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /InnerClasses/NonStaticInnerClass/NormalInnerClass/Example7.java: -------------------------------------------------------------------------------- 1 | class OuterClassA 2 | { 3 | // call the non static members in the Inner Class In non static area 4 | int number1 = 10; 5 | float number2 = 11.5f; 6 | class InnerClassB 7 | { 8 | int number3 = 100; 9 | float number4 = 200.5f; 10 | void method1(int number5 , float number6) 11 | { 12 | System.out.println("Inside Method - 1 of InnerClassB : "); 13 | System.out.println(number5+number6); 14 | System.out.println(number3+number4); 15 | System.out.println(number1+number2); 16 | } 17 | } 18 | } 19 | 20 | class Example7 21 | { 22 | public static void main(String[] args) 23 | { 24 | /* 25 | OuterClassA ref1 = new OuterClassA(); 26 | OuterClassA.InnerClassB ref2 = ref1.new InnerClassB(); 27 | ref2..method1(1000 , 2000.5f); 28 | */ 29 | 30 | new OuterClassA().new InnerClassB().method1(1000 , 2000.5f); 31 | } 32 | } --------------------------------------------------------------------------------