| Java (2 Steps) | Java (1 Step) | Java + JDBC |
|---|---|---|
1. javac FileName.java2. 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 |
- 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.==
-
Single-Step Execution (
java FileName.java):- This works for simple Java scripts (introduced in Java 11+) with a single file containing a
mainmethod. - It does ==not work== for more complex files that define multiple classes or depend on other files.
- This works for simple Java scripts (introduced in Java 11+) with a single file containing a
-
Two-Step Execution (
javac+java):- First Step (
javac): ==Compiles== all classes and creates.classfiles for each. - Second Step (
java): ==Runs== themainmethod of the specified class by referencing the.classfile.
- First Step (
-
==Why Two-Step Execution Is Necessary== π:
- Java needs
.classfiles 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.
- Java needs
- Use
java FileName.javafor simple, single-class programs. - Use
javac FileName.javafollowed byjava ClassNamefor 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!)
replace those with this single line!
import module java.base.*;The ==javap== command lists the methods and constructors of the class that you specify.
- List methods of
Stringclass:javap java.lang.String
- List all methods (including private) of
String:javap -private java.lang.String
For your own file?
- Compile your Java file:
javac OutputExercise.java
- List methods of your own class (
OutputExercise):javap OutputExercise
- 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).
- 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.==
- 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.==
-
Collection (Interface) => ==The root interface of the Java Collection Framework, representing a group of objects (List, Set, Queue).==
-
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)* 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
* 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
* 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
set classpath=;C:\oracle\product\10.2.0\db_1\install\patches\5923165\files\jdbc\lib\ojdbc14.jar
echo %classpath% C:\oracle\product\10.2.0\db_1\network\Admin\tnsnames.oraSearch 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!
- 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.
- 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.
- Purpose: Java for big business apps.
- What It Includes: APIs for web apps, databases, and messaging.
- Uses: Scalable enterprise apps, web services, and microservices.
- Purpose: Java for small, resource-limited devices.
- What It Includes: Lightweight tools for tiny devices.
- Uses: Mobile phones, IoT devices, embedded systems.
- 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.
| 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 |