Skip to content

Welcome to my Java Fundamentals Playground! This repo is all about learning the basics of Java and practicing core concepts. It's my personal coding space where I tinker with Java code, try out new stuff, and (sometimes) break things while figuring out how it all works. πŸ˜„

Notifications You must be signed in to change notification settings

iamjayandhan/Java-Tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Hello JD!

Java (2 Steps) Java (1 Step) Java + JDBC
1. javac FileName.java
2. java FileName
java FileName.java java -cp ".;C:\path\to\file.jar" FileName.java
Creates byte code in local storage Creates byte code in its own memory Creates byte code in its own memory

Rule of thumbπŸ˜‡

  • In general, Java does not allow running a .java file directly if it contains multiple classes (even if only one of them is public).
  • The Java runtime tries to compile and execute the file in a single step, but since the file contains multiple classes, it ==cannot directly determine the entry point (the main method) or handle other class dependencies correctly.==

Lets Dive into it!

  1. Single-Step Execution (java FileName.java):

    • This works for simple Java scripts (introduced in Java 11+) with a single file containing a main method.
    • It does ==not work== for more complex files that define multiple classes or depend on other files.
  2. Two-Step Execution (javac + java):

    • First Step (javac): ==Compiles== all classes and creates .class files for each.
    • Second Step (java): ==Runs== the main method of the specified class by referencing the .class file.
  3. ==Why Two-Step Execution Is Necessary== πŸ˜‡:

    • Java needs .class files to manage the dependencies and structure of the program, especially when multiple classes are involved.
    • Directly running a .java ==file doesn't allow== the runtime to handle dependencies or locate auxiliary classes.

Enna tha solla vara?

  • Use java FileName.java for simple, single-class programs.
  • Use javac FileName.java followed by java ClassName for multi-class programs or more complex setups.
  • Number of class files == number of class present in the file (No matter wheather it is seperate or nested class!)

Too many import statements?

replace those with this single line!

   import module java.base.*;

Not sure about the methods in class? No worries(javap on rescue)!

The ==javap== command lists the methods and constructors of the class that you specify.

  1. List methods of String class:
    javap java.lang.String
  2. List all methods (including private) of String:
    javap -private java.lang.String

For your own file?

  1. Compile your Java file:
    javac OutputExercise.java
  2. List methods of your own class (OutputExercise):
    javap OutputExercise

Understanding Java Packages: (Compile and Run):

Why Compile and Run from the Root Directory in Java?

  1. Packages Require Structure:
    • The package statement (e.g., package package1;) ==tells Java to treat the file as part of a named package.==
    • The ==JVM expects the .class== file to be inside a folder named after the package (package1).
  2. JVM Starts at the Root Directory:
    • The root directory is where the JVM begins looking for packages.
    • If you’re inside package1 and run the program, the ==JVM looks for Class1 in the default package (no package), causing an error.==
  3. Correct Steps:
    • Compile: Use the root directory to ensure the .class file is saved with the correct package path.
    javac package1/Class1.java
    • Run: Use the root directory to reference the fully qualified class name:
    java package1.Class1

Why Not Run Inside the Package Folder? ==Inside package1, you cannot reference package1.Class1 because the JVM doesn’t know it’s in a package.==

Java Collection Hierarchy

  1. Collection (Interface) => ==The root interface of the Java Collection Framework, representing a group of objects (List, Set, Queue).==

  2. Collections (Class) => ==A utility class in java.util that provides static methods (e.g., sorting, searching, synchronization) for working with collections.==

Below is the hierarchical structure of Java's Collection Framework:

// Java Collection Framework - Full Interface & Implementation Hierarchy

Iterable<E>  // Root interface
   β”œβ”€β”€ Collection<E>  
   β”‚      β”œβ”€β”€ List<E>  // Ordered, allows duplicates
   β”‚      β”‚      β”œβ”€β”€ ArrayList<E>       // Dynamic array-based
   β”‚      β”‚      β”œβ”€β”€ LinkedList<E>      // Doubly linked list
   β”‚      β”‚      β”œβ”€β”€ Vector<E>          // Synchronized ArrayList
   β”‚      β”‚      β”œβ”€β”€ Stack<E>           // LIFO stack (extends Vector)
   β”‚      β”‚
   β”‚      β”œβ”€β”€ Set<E>  // No duplicates
   β”‚      β”‚      β”œβ”€β”€ HashSet<E>         // Unordered, backed by HashMap
   β”‚      β”‚      β”œβ”€β”€ LinkedHashSet<E>   // Maintains insertion order
   β”‚      β”‚      β”œβ”€β”€ TreeSet<E>         // Sorted, backed by TreeMap
   β”‚      β”‚
   β”‚      β”œβ”€β”€ Queue<E>  // FIFO & Priority-based
   β”‚      β”‚      β”œβ”€β”€ PriorityQueue<E>   // Heap-based priority queue
   β”‚      β”‚      β”œβ”€β”€ LinkedList<E>      // Can be used as Queue (Deque)
   β”‚      β”‚      β”œβ”€β”€ ArrayDeque<E>      // Efficient double-ended queue
   β”‚      β”‚
   β”‚      β”œβ”€β”€ Deque<E>  // Double-ended queue
   β”‚             β”œβ”€β”€ LinkedList<E>   // Implements Deque
   β”‚             β”œβ”€β”€ ArrayDeque<E>   // Faster than Stack
   β”‚
   β”œβ”€β”€ Map<K, V>  // Key-Value pairs (Has special iterators)
   β”‚      β”œβ”€β”€ HashMap<K, V>        // Unordered key-value store
   β”‚      β”œβ”€β”€ LinkedHashMap<K, V>  // Maintains insertion order
   β”‚      β”œβ”€β”€ TreeMap<K, V>        // Sorted key-value store
   β”‚      β”œβ”€β”€ WeakHashMap<K, V>    // Keys get garbage collected
   β”‚      β”œβ”€β”€ ConcurrentHashMap<K, V>  // Thread-safe HashMap
   β”‚      β”œβ”€β”€ Hashtable<K, V>      // Legacy synchronized map
   β”‚
   β”œβ”€β”€ Specialized Collections  // Thread-safe & blocking collections
   β”‚      β”œβ”€β”€ CopyOnWriteArrayList<E>  // Thread-safe ArrayList
   β”‚      β”œβ”€β”€ CopyOnWriteArraySet<E>   // Thread-safe HashSet
   β”‚      β”œβ”€β”€ LinkedBlockingQueue<E>   // Blocking queue for concurrency
   β”‚      β”œβ”€β”€ LinkedBlockingDeque<E>   // Blocking deque (both ends)
   β”‚
   β”œβ”€β”€ Iterators  // Traversal mechanisms for collections
   β”‚      β”œβ”€β”€ Iterator<E>        // Used in all Collection<E>
   β”‚      β”œβ”€β”€ ListIterator<E>    // Used only in List<E> (bi-directional)
   β”‚      β”œβ”€β”€ Spliterator<E>     // Supports parallel iteration (Java Streams)
   β”‚      β”œβ”€β”€ Enumeration<E>     // Legacy iterator (Vector, Hashtable)
   β”‚
   β”œβ”€β”€ Additional Interfaces  // Advanced collections
   β”‚      β”œβ”€β”€ BlockingQueue<E>     // Queue with thread-safety (Producer-Consumer)
   β”‚      β”œβ”€β”€ NavigableSet<E>      // Extended Set<E> with navigation methods
   β”‚      β”œβ”€β”€ NavigableMap<K, V>   // Extended Map<K, V> with navigation methods
   β”‚      β”œβ”€β”€ ConcurrentMap<K, V>  // Thread-safe Map (like ConcurrentHashMap)

Connection strings(Explore more!πŸ₯³πŸ₯³):

1. Mysql (My local DB):

* DRIVER = com.mysql.cj.jdbc.Driver
* STRING = jdbc:mysql://localhost:3306/kgisl_fourth_batch","root","πŸ˜‡"
* CMD = java -cp ".;libs\mysql-connector-j-9.1.0.jar" File.java

2. Postgresql (Supabase Online DB):

* DRIVER = org.postgresql.Driver
* STRING = jdbc:postgresql://aws-0-ap-south-1.pooler.supabase.com:6543/postgres?user=postgres.yshxqhzwcekhvudlkaju&password=πŸ˜‡
* CMD = java -cp ".;libs\postgresql-42.7.4.jar" File.java

3. Microsoft Mysql server 2022 (My local DB):

* DRIVER = com.microsoft.sqlserver.jdbc.SQLServerDriver
* STRING = Server=localhost\SQLEXPRESS;Database=master;Trusted_Connection=True;
* CMD = java -cp ".;libs\mssql-jdbc-12.2.0.jre11.jar" File.java

SETUP LOCAL FOR ORACLE JAR (College system)

1. Set class path for jar if working in local!

    set classpath=;C:\oracle\product\10.2.0\db_1\install\patches\5923165\files\jdbc\lib\ojdbc14.jar  
    echo %classpath%  

2. Get System username of Oracle!

C:\oracle\product\10.2.0\db_1\network\Admin\tnsnames.ora

Search for tnsnames, choose second listed with small letters! Look for orcl block and copy Host name. Use for host name!

    Connection con = DriverManager.getConnection("jdbc:oracle:thin:@KITEORACLE38191.kgisledu.com:1521/orcl","scott","tiger");

Like this!


Different Flavors of Editions of Java!

1. Java SE (Standard Edition)

  • Purpose: Core Java platform for basic applications.
  • What It Includes: Tools for writing and running Java programs (e.g., JDK, JRE).
  • Uses: Desktop apps, simple server apps, command-line tools.

2. GraalVM

  • Purpose: A powerful runtime supporting Java and other languages (like Python, JavaScript).
  • What It Offers:
    • Better performance through advanced compilation.
    • Mix Java with other languages easily (polyglot).
  • Uses: Multi-language apps, fast performance apps.

3. Java EE (Enterprise Edition) β€” Now Jakarta EE

  • Purpose: Java for big business apps.
  • What It Includes: APIs for web apps, databases, and messaging.
  • Uses: Scalable enterprise apps, web services, and microservices.

4. Java ME (Micro Edition)

  • Purpose: Java for small, resource-limited devices.
  • What It Includes: Lightweight tools for tiny devices.
  • Uses: Mobile phones, IoT devices, embedded systems.

5. JavaFX

  • Purpose: Build rich graphical apps for desktops and more.
  • What It Offers:
    • Modern GUI tools (charts, tables, 3D graphics).
    • Multimedia and animations.
  • Uses: Desktop UIs, multimedia apps.

Quick Comparison:

Edition Key Focus Example Uses
Java SE Core Java platform Desktop tools, small apps
GraalVM High performance, multi-language Multi-language apps, faster apps
Java EE Enterprise-level apps Web services, large systems
Java ME Small devices Mobile, IoT, embedded systems
JavaFX Graphical user interfaces Desktop UIs, animations

Current codespaces on cloud

  1. https://github.com/codespaces/
  2. https://gitpod.io/workspaces/

About

Welcome to my Java Fundamentals Playground! This repo is all about learning the basics of Java and practicing core concepts. It's my personal coding space where I tinker with Java code, try out new stuff, and (sometimes) break things while figuring out how it all works. πŸ˜„

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published