Skip to content

Embark on a Java mastery journey with our Core Java Training repository! This collection provides a solid foundation in Java programming, covering key concepts like classes, objects, inheritance, and more. Explore hands-on exercises, real-world examples, and dive into the heart of Java development.

thisarakaushan/Core-Java-Training

Repository files navigation

Core-Java-Training

Importance

  • Data Type Size in bytes:

    • Byte - 1
    • short - 2
    • int - 4
    • long - 8
    • float - 4
    • double - 8
    • char - 2 // But in c programming char size is '1'.
    • string // Dependent on string length
    • boolean - 1 bit
  • Executable code will have a MAIN method but Libraries will not have a main method.

  • Values Type variables are stored in a STACK memory. --> Value type is faster that reference type.

  • Reference Type variables are stored in a HEAP memory.

  • String s1 = "Name"; String s2 = "Name"; System.out.println(s1 = s2); and System.out.println(s1.equals(s2));--> Both are same.

  • But,
    String s1 = "Name"; This keep the value in String POOL.
    String s2 = new String("Name"); --> This keep the value in HEAP memory but outside the String POOL.
    System.out.println(s1 = s2); and System.out.println(s1.equals(s2));--> Both are different.

  • Because, == operator compares reference (or) memory location and .equals() method will compare the value of the variables

  • String is immutable but int is mutable.

  • String data = NULL; --> This is not an error.

    • In Java, null is a valid value that can be assigned to any reference type, including String. But; int data = NULL; --> This will give an error because In Java, you cannot assign null to a primitive data type.
  • In Java, you can assign null to an Integer variable [Integer = NULL], which is the wrapper class for the primitive int type. This is because Integer is an object and can hold a reference to null.

  • Integer data = 5; int age = data; --> No error Assigning an integer value to a primitive int variable in Java does not give an error. Both integer data = 5; and int data = 5; are valid assignments.

Regular Expressions

Example:

  • We check for equality of two string
  • We check if the value is strating the some string or character.
  • We check if the value is ending with some data.
  • We also check if the text is of a specific pattern - like email id, zip code, etc...

image

  • Types:

image
image

Examples:

image
image

image

  • StringBuilder creates a default capasity(16 bytes) in the memory.

Operators:

Operator Category Operators List

  • Arithmetic { +, -, /, *, % }
  • Relational { <, >, =<, =>, !=, == }
  • Logical { &&, ||, ! }
  • Bitwise { &, |, ^ }
  • Shift { <<, >> }
  • Unary { ++, - - }
  • Assignment { +=, -=, *=, /=, = }
  • In Java, Math is not alibrary. It is a class.

  • Using Copy con filename.java , we can perform Command Line Coding in Java.

  • Object Oriented Concept in Java:

    1. Encapsulation : Mainly Security and Modularity.
    2. Inheritance: Mainly Reusability
    3. Polymorphism: means many forms - methods like Overloading and Overriding
    4. Abstraction
  • Normal Class cannot have Abstract method.

  • Abstract Class can have Normal method.

  • Abstraction has two categories such as:

    1. Abstract Class: - Normal methods: reusability - Abstrcts methods: must be implemented in derived class

    2. Interface: acts like a contract and any class implementing interface must override all the methods.

Note

  • When we are creating classes in same package, that time we don't want to import anypackages.

  • If any variable common for every objects, keep that variable as Static.

    • ex: when we are calculating salary of employee in a company, this time company name will be same for every employee. So, we create company variable as Static.
      • String static company = "ABC";
  • When method is declare as Static, we don't need to call by object it, we can access by class name.

    • ex: we supposed class name is Employee.
      • we can create static method like --> public static void displayEmpData()
      • we can access this method without creating any object; - Employee.displayEmpData();
      • Also we can access using object but some warning will display.
  • Static method cannot access Normal variable but Normal method can access Static variable.

  • Normal Class can have Static method .

  • Normal variable should be Private, if not anyone can access and modify them.

  • Contructors:

    • Constructor is used initialize the class variables.
    • Contructor name should be as same as class name.
    • Class can have multiple constructors.
    • Contructor doesn't have return type or any void.
    • While we are creating a constructor, a default constructor is automatically created but is hidden from view.
  • Access Modifiers:

                               S a m e  P a c k a g e              O t h e r P a c k a g e
|Access Modifiers | Within Class | Derived Class| Other Class  | Derived Class | Other Class |
|-----------------|--------------|--------------|--------------|---------------|-------------|
| Public          | YES          | YES          | YES          | YES           | YES         |
| Private         | YES          | NO           | NO           | NO            | NO          |
| Protected       | YES          | YES          | YES          | YES           | NO          |
| Default         | YES          | YES          | YES          | NO            | NO          |

Arrays

  • Arrays are collections of similar data types.

  • Array index starts from zero.

  • Array elements are stored sequentially in memory location / requires continous memory.

  • Array size cannot be modified / cannot modify the size dynamically.

  • Array variable size = data_type X array_size

  • Arrays give easy access to elements and simple data struccture. It has sequential processing and index-based sorting. Also array supports for multidimentional data.

  • Types of loops

    Example: int[] numbers = new int[n]

    1. For loop -> for(int i=0; i<n; i++) or for(int i=0; i<numbers.length; i++) - System.out.println(numbers[i])
    2. For Each loop -> for(int n: numebrs) - - System.out.println(n)
    3. ForEach -> numbers.forEach(n -> System.out.println(n))

Note

  • _Multiple Inheritance _only support for Interface in Java, not for classes. Interface is a pure abstract class.

  • In C programming,
    - int n; printf(n); - output : Garbage value

  • In Java programming, - for same output will be an error -> compilation error

  • Scanner sc = new Scanner(System.io);
    String str = sc.next();
    Input: Hello World but Output: Hello

  • When we use nextLine() we can get full input line as output.

  • If we want to get one character we can use char = sc.next().charAt(0);

  • Use MessageFormat method form java.text.MessageFormat package, instead of using +for concatenation operation.

    Example:

    String name = "ABC";
    int age = "10";
    
    System.out.println("I am "+name+". I am "+age+" years old.");
    

    Best way:

    details = MessageFormat._format_("I am {0}. I am {1} years old.", name, age);
    System.out.println(details);
    
  • Use String.valueOf() instead of toString().

    Example:

    String data = null;
    System.out.println(data.toString());
    
    String data = null;
    System.out.println(String.valueOf(data));
    
  • Use equalsIgnoreCase() when compared to toUpperCase().equals or toLowerCase().equals.

  • When using StringBuilder or StringBuffer append method for single a single character always use single quote.

    Example: sb.append('a') is better than sb.append("a")

  • StringBuilder default capacity is 16.

Collections and Maps:

image image

Click here to review more about ArrayList.
Click here to review more about LinkedList.
Click here to review more about HashSet.
Click here to review more about TreeSet.
Click here to review more about HashMap.

Wrapper Class:

  1. byte - BYTE
  2. short - SHORT
  3. int - Integer
  4. long - LONG
  5. float - FLOAT
  6. double - DOUBLE
  7. char - Character
  8. boolean - BOOLEAN

ARRAY and ARRAYLIST

  • ARRAYs are fixed size data structures, meaning that once you declare an array with a specific size, you cannot change it. You need to know the size of gthe array at the timr of declaration. arrays generally have better performance than ARRAYLISTs because they have direct access to the elemnts using index_based access.

  • ARRAYLISTs, on the other hand, are dynamically data structures that can increases in size as needed. They automatically handle the resizing of the array to accommodate new elements. ARRAYLISTs are part of the standard library (Java Collection Framework) and provide additional features like dynamic resizing, easy addition and removal of elements.

  • Declaration ArrayList : Use util package _ ArrayList data = new ArrayList();_

    • Example:
     import java.util.ArrayList;
     import java.util.Scanner;
    
     public class AddNumbers {
     
         public static void main(String[] args) {
     
             Scanner scanner = new Scanner(System.in);
             ArrayList<Integer> numbers = new ArrayList<>();
    
             System.out.println("Enter the first number: ");
             int num1 = scanner.nextInt();
             numbers.add(num1);
    
             System.out.println("Enter the second number: ");
             int num2 = scanner.nextInt();
             numbers.add(num2);
    
             int sum = numbers.get(0) + numbers.get(1);
             System.out.println("Sum of the two numbers: " + sum);
    
             scanner.close();
         }
      }
    

    or

    List data = Arrays.asList(10,20)

    • Example:
      import java.util.ArrayList;
      import java.util.Arrays;
      import java.util.List;
      import java.util.Scanner;
      
      public class AddNumbers {
      
          public static void main(String[] args) {
      
              Scanner scanner = new Scanner(System.in);
      
              System.out.println("Enter the first number: ");
              int num1 = scanner.nextInt();
      
              System.out.println("Enter the second number: ");
              int num2 = scanner.nextInt();
      
              List<Integer> numbers = Arrays.asList(num1, num2);
      
              int sum = numbers.get(0) + numbers.get(1);
              System.out.println("Sum of the two numbers: " + sum);
      
              scanner.close();
          }
      }
      
    • Creating a .jar file we can run it from command prompt. Runnable jar file has a main method. But noraml jar file hasn't main method.

ARRAYLIST AND LINKEDLIST

  • ARRAYLIST provides faster accesss to elements using index-based access as it can directly calculate the memory location of the elements. However, insertions and deletions in the middle of the list require shifting elements which can be slow for large lists. ArrayList generally uses less memory than Linkedlist since it only needs to store the elements and the dynamic array that holds them.

  • ArrayList default capasity is 10, also allows for NULL values.

    • Using eachCapasity() we can check the size of the ArrayList before initialize the size.
image
  • LinkedList is more efficient for frequent insertions and deletions in the middle of the list because it only requires updating the pointers. However, accessing elements by index, it needs to traverse the list from beginning or end to reach the desired elements. LinkedList uses more memory as it needs to store additional pointers(prevoius and next elements addresses) for each elements.

  • LinkedList implements Doubly LinkedList, also allows to NULL values

    • Uses AddFirst(E e) method to add element to the beginning of the list. It takes boolean value.
    • and AddLast(E e) method to add element to the end of the list. It takes void.
  • Using following example we can get an idea about the operation speed between ArrayList and LinkedList.

image

image
image

PRIORITYQUEUE

Example: Queue data = new PriorityQueue()

  • Queue and PriorityQueue are from java.util package.

  • Here are differenet methods such as;

    1. data.peek() - It takes the top elemets in the Queue. It is FIFO algorithm.
    2. data.poll() - It removes the top element of the queue and display the second element.
  • PriorityQueue default initial capacity is 11.

Write in a Flat file and Read from a Flat file

Methods: From java.io.* package

  1. FileReader --> Read and print Character by Character. It is required file object.

    Example:

   Files f1 = new Files("path\file_name.txt");
   FileReader fr = new FileReader(f1);
   int ch = fr.read();
   while(ch != -1)
   {
      System.out.println((char)ch);
      ch = fr,read();
      fr.close();
   }
  1. BufferedReader --> Read and Print Character by Character ot Line by Line. It is required FileReader object.

    Example:

       // Taking FileReader object.
       try (BufferedReader reader = new BufferedReader(new             
       FileReader("myfile.txt")))
       {
           String line;
           while ((line = reader.readLine()) != null)
           {
           // process each line of the file
           }
       }
       catch (IOException e)
       {
           // handle exception
       }
    
  2. Files.readAllFiles --> read all lines and print. It is List of String.

    Example:

       List<String> fileContent;
       try
       {
          fileContent = Files.readAllFiles(Paths.get("path\file_name.txt"));
          fileContent.forEach(f -> System.out.println(f));
       }
    

    or

         try
         {
            List<String> fileContent = new ArrayList<String>();
         }
    
  3. FileWriter --> Read and print something from flatf file.

    Example:

       File f1 = new File("path\file_name.txt");
       try
       {
          FileWriter fw = new  FileWriter(f1);
    
          // Read a name from the file
          System.out.println("Enter the id: ");
          name = obj.nextLine();
          fw.write(name);
          fw.close();
       }
    
  • When we use FileWriter(f1) method, it takes the input from flat file then that input will deleted. BUT when we use FileWriter(f1, true), it takes the input from flat file without destroying the input.

HashSet and TreeSet

  • Set<String> fruits = new HashSet<>() from java.util.* package. Elements in a HashSet are not stored in any particular order, it is stored in a natural order. The order in which elements are inserted may not be preserved. Then insertion and retrieval operations have an average time complexity.

  • Also doesn’t provide any get() method because it is not index-based. Null value allowed only once.

  • In HashSet, the Object.clone() method is used to create a shallow copy of an object. The clone() method is defined in the Object class, and every class in Java inherits this method. However, to use clone() effectively, a class must implement the Cloneable interface. If a class does not implement Cloneable and calls clone(), it will throw a CloneNotSupportedException. HashSet in Java, the clone() method is inherited from the Object class, and HashSet itself does not override or modify this method. Therefore, when you call clone() on a HashSet object, you will get a shallow copy of the set.

  • A shallow copy means that the new HashSet will contain the same elements (references) as the original HashSet. However, the elements themselves are not cloned. This means that if the elements in the HashSet are mutable objects, changes made to those objects in the original HashSet will also be reflected in the cloned HashSet, as they both refer to the same objects.

    Example:

    import java.util.HashSet;
    
      public class HashSetCloneExample
      {
          public static void main(String[] args)
          {
              HashSet<String> originalSet = new HashSet<>();
              originalSet.add("apple");
              originalSet.add("banana");
              originalSet.add("orange");
      
              // Create a shallow copy of the originalSet
              HashSet<String> clonedSet = (HashSet<String>) originalSet.clone();
      
              System.out.println("Original Set: " + originalSet);
              System.out.println("Cloned Set: " + clonedSet);
      
              // Modify the original set
              originalSet.add("grapes");
      
              System.out.println("Original Set after modification: " + originalSet);
              System.out.println("Cloned Set after modification: " + clonedSet);
          }
      }
    

    Output:

    Original Set: [orange, banana, apple]
    Cloned Set: [orange, banana, apple]
    Original Set after modification: [grapes, orange, banana, apple]
    Cloned Set after modification: [orange, banana, apple]
    
  • As you can see from the output, the cloned set remains unchanged even after modifying the original set. This is because both the original and cloned sets share the same objects (references) for their elements.

  • Keep in mind that while clone() can be useful in some cases, it has certain limitations, especially when dealing with complex objects or collections of objects. In such cases, it is often better to use other techniques like creating a deep copy manually or using third-party libraries for deep cloning.

  • TreeSet<Integer> treeSet = new TreeSet<>() from java.util.* package. Elements in a TreeSet are always stored in sorted order. This allows efficient range queries and iteration over the elements in a sorted manner. Then insertion and retrieval operations have higher time complexity because it inserts the values in ascending order. TreeSet doesn’t allow null values.

HashMap

  • Map<String, Integer> scores = new HashMap<String, Integer>() from java.util.* package. HashMap takes key, value pair.

  • There are 2 method to add data:

    • data.add()
    • data.put()
  • We can get keys and values different ways;

    Example:

    // Using For Loop takes keys
    for(String key: data.keyset())
    {
        System.out.println(key+":"+ data.get(key));
    }
    

    or

    // Using forEach 
    data.forEach((key, value) -> System.out.println(Key+":"+ value));
    

    or

    // Taking values using For Loop
    for(String value: data.values())
    {
        System.out.println(value+":"+ data.get(value));
    }
    

Compilation Error and Runtime Error

  • A compilation error, also known as a compile-time error or syntax error, occurs when the source code written by the programmer does not conform to the rules of the programming language. It happens during the compilation phase when the code is being translated into machine-executable code by the compiler. Compilation errors prevent the successful creation of an executable program and must be fixed before the code can be executed.

    Example: - missing of semicolon ; or double quote "", etc... - String assigns to an integer values

    • A runtime error, also known as an exception or a run-time exception, occurs during the execution of the program. It happens when the code is running, and the program encounters an unexpected situation that it cannot handle. Runtime errors cause the program to terminate abnormally if not caught and handled properly through exception handling mechanism.

      Example: - ArrayIndexOutOfboundException - ArithmeticException - NumberFormatException - NullPointerException - more
  • We can control using Exception Handling.

Exception Handling

  • Exception handling is a crucial programming concept used to manage unexpected or exceptional situations that can occur during the execution of a program. These exceptional situations, known as exceptions, can arise due to various reasons, such as invalid inputs, resource unavailability, network issues, or programming errors.

  • The primary purpose of exception handling is to prevent the unexpected situation of the program when an exception occurs and creates an user friendly interface. Instead of crashing, the program can detect and respond to exceptions, allowing it to recover or exit, providing valuable information about the issue.

Example:

public static void main(String[] args)
{
	float num1, num2, result;
	Scanner obj = new Scanner(System.in);

	try // start from catch block
    	{
		System.out.println("Enter Two numbers: ");
		num1 = Float.parseFloat(obj.nextLine());
		num2 = Float.parseFloat(obj.nextLine());
	
		result = num1 / num2;
	
		System.out.println("Answer: " + result);
	}
    	catch (NumberFormatException ex) // First catch block
    	{
		System.out.println("Please again check the input. Only numbers are allowed.");
	}

	catch (Exception e) // second catch block
    	{
		System.out.println("Some error occurred. Contact the admin.");
	}

	finally // End of the all catch blocks, we take the finally block
    	{
		System.out.println("Print any key to exit.");
		obj.close();
	}
}

  • According to the above code we declare two catch block after try block because we can use multiple catch blocks after try bloack.

  • Catch blocks will execute according to the unexpected errors in the program. In example, first catch block is NumberFormatException exception that will display when occured some error in format of numbers. But second exception is Exception, that will catch all unexpected error which cannot by handle first catch block because when we take a catch block as Exception that will act as a Super Exception. This catch block should be placed end of the all other catch block, otherwise only we can use Super Exception.

  • End of all try and catch block we declare the finally block. Finally block executes when exception accured or not. It displays every statements inside the finally block.

About

Embark on a Java mastery journey with our Core Java Training repository! This collection provides a solid foundation in Java programming, covering key concepts like classes, objects, inheritance, and more. Explore hands-on exercises, real-world examples, and dive into the heart of Java development.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages