Skip to content

Repository files navigation

Learning Java with Projects

Static Badge

  • Programming is like riding a bike; you can read about how to do it all day, but the best way to get started is to practice.

  • A Core Java Bootcamp is a structured training program where i learned the fundamentals of Java programming from basic to intermediate level.

  • Java is a high-level, object-oriented, platform-independent programming language developed by Sun Microsystems in 1995 (now owned by Oracle).

  • Java follows the principle: Write Once, Run Anywhere (WORA)

  • This means Java programs can run on any system that has the Java Virtual Machine (JVM).


🚀 Features of Java

i] Object-Oriented

ii] Platform Independent

iii] Secure

iv] Robust

v] Multithreaded

vi] Portable

vii] High Performance


Java applications


🎯 Before getting started

Now, before we immerse ourselves into writing code here's a few things you should know about Java: Java is a portable, secure, robust, compiled, object-oriented, and platform-independent programming language. This means it can run on any machine, regardless of what operating system is used.

  • What is programming?

Computer programming refers to the detail or steps of instructions given to a computer in an appropriate computer language, which enable the computer to perform a variety of tasks in sequence. A set of instructions written in a computer language is typically called a program or code.

  • Java Virtual Machine

JVM stands for Java Virtual Machine. It is our smallest unit. When you run the Java program, Java compiler first compiles your Java code to bytecode. Then, the JVM translates bytecode into native machine code (set of instructions that a computer's CPU executes directly).

  • Java Runtime Environment

JRE (Java Runtime Environment) is a software package that provides Java class libraries, Java Virtual Machine (JVM), and other components that are required to run Java applications. If you need to run Java programs, but not develop them, JRE is what you need.

  • Java Development Kit

JDK stands for Java Development Kit. It refers to all the standard libraries used in programming the software and a number of development tools (compilers, JavaDoc, Java Debugger, etc). When you download JDK, JRE is also downloaded with it.

JDK-JRE-JVM

📂 Projects

For our projects we need Java and once this is in your system

The projects in our list are assignments focusing on different skills in Core Java needed to build out a well-rounded Java skill set; progressing in difficulty as the list continues.

  1. Introduction
  2. Looping
  3. Arrays and Strings
  4. Object-Oriented Programming
  5. Constructor
  6. Abstract Classes and Interfaces
  7. Exceptions
  8. Multithreading
  9. Collections

1. Introduction

Starting with the java cocepts, first i learned the concepts of class, data types, Wrapper classes, variable types (static, local, instance), arithmetics, and the Scanner class.

Book.java is the corresponding assignment.

Here, we create a Book class, which asks the user for three inputs: book name, author name, publish status, and then prints those values sequentially to the console.

output

2. Looping

Looping consists of a) decision-making constructs (if, ladder if else, switch) since they make logical decisions (true/false) based on the conditions provided b) conditional statements, which are the executable block of code (or branch to a specific code) dependent on certain conditions (while, do_while, for).

Employee.java is the corresponding assignment.

Here, the user is supposed to enter his name, daily salary, and number of office attending-days that month. His monthly salary is calculated and used to justify to him his job designation.

OUTPUT1

3. Arrays and Strings

Array

  • An Array is a data structure in Java that is used to store multiple values of the same data type in a single variable.

  • Collection of elements of the same data type stored in contiguous memory locations.

  • Each element in an array is stored at a specific index position, starting from 0.

Example - int[] numbers = {10, 20, 30, 40};

  • Stores same data type elements

  • Fixed size

  • Index starts from 0

  • Stored in continuous memory

String

  • A String in Java is an object that represents a sequence of characters.

Example - String name = "Yuvi";

In this concept we'll tackle a question: Counting the number of vowels and consonants in a string array specified by the user.

VowelsConsonants.java contains the solution and here is a demo output:

Output-3

4. Object-Oriented Programming

The purpose of OOP is to improve code readability and reusability. Data is accessed and controlled. Code is modified and maintained. All this is easier with object-oriented programming.

  • The four pillars of OOP are:

    a) inheritance

    b) polymorphism

    c) encapsulation

    d) abstraction

  • A class stands at the heart of OOP. It is a blueprint of an object, and an object is an instance or a copy of a class. An object is also a runtime entity with an identity, state, and behavior - all described by methods and variables. It is initialized using the new keyword.

Inheritance

  • Inheritance is an OOPS concept where one class acquires the properties and methods of another class

i] Single ii] Multi-level/chained iii] Hierarchical iv] Hybrid (mix of first 3) inheritance.

  • Class can access properties of another class. First class is called parent-/base-/superclass, second is called child-/derived-/subclass. Implemented using extends keyword. Multiple inheritance not supported.

Inheritance

Inheritance

Polymorphism

  • One name, many forms (poly: many, morphism: behavior). One function can have multiple behaviors.

  • Ways: a) function overloading/compile-time overloading: same name, different parameters b) function overriding/runtime-/dynamic polymorphism / dynamic method dispatch: same name, same parameters (signature) / one reference (not object) for all subtypes, same method called on all, type of object being referred at time of call.

Encapsulation

  • Wrapping and binding of data in single unit. Variables declared in class with private keyword. Variables accessed with getters and setters. Methods inside the class.

Abstraction

  • Hiding implementation details of the code and exposing only necessary information to the user (example: ATM machine). Declared using abstract keyword. If method is abstract class must also be abstract. Class can't be instantiated, only extended in a child class.

Larger Project: Banking System

Now let's have a look at a menu-driven banking application: BankingApp.java.

The user must input his bank information. Then, a menu appears:

Bank-Output-1 Bank-Output-2

After picking an option the instruction is carried out until the loop exits :

Bank-Output-3

5. Constructor

  • A Constructor is a special method in Java that is used to initialize objects.

  • Special type of method which is used to initialize/create an object.

  • Rules:

  1. Constructor and class name should be the same
  2. Constructor doesn't contain return type
  3. Constructor cannot be abstract, static or final. Types: 1) default, 2) parameterized. A constructor cannot be overridden, but it can be overloaded.

Watch this overloaded constructor in Student.java:

Overloading Constructor

6. Abstract Classes and Interfaces

  • An abstract class must be declared with the abstract keyword. It can have abstract and non-abstract methods. It can't be instantiated. It can have constructors and static or final methods. If you want to declare a method as abstract the class must also be abstract.

  • Interfaces are used to achieve multiple inheritance in Java - at a time you can extend multiple classes. An interface object cannot be created. An interface is defined by the interface keyword. By default, interface methods are abstract. A class to interface relationship is defined by implements. An interface to interface relationship is defined by extends.

  • An example of the two is provided in Parent.java. Here is the output:

Outputt

7. Exceptions

  • An Exception is an unexpected event that occurs during program execution and disrupts the normal flow of the program.

  • In Java, exceptions are handled using Exception Handling to prevent program crashes.

Exceptions

  • Keywords Used in Exception Handling

    i] try → Block of code that may cause exception

    ii] catch → Handles the exception

    iii] finally → Executes always

    iv] throw → Used to explicitly throw exception

    v] throws → Declares exception

To recover from exceptions we can make use of try-catch, throw/throws, or finally blocks. We can handle exceptions with multiple catch-blocks. The finally-block code executes at any cost. See how throw and throws keywords work.

In ThrowPositiveException.java we see an example of a user-defined exception:

Exceptions-Result

8. Multithreading

  • Multithreading is a process of executing multiple threads simultaneously within a single program.

  • It allows a program to perform multiple tasks at the same time, improving performance and CPU utilization.

  • In Java, multithreading is achieved using:

i] Thread class

ii] Runnable interface

A thread is a process / execution of a program. A thread gets created by extending the Thread class or by implementing Runnable interface, the latter allowing for more flexibility (multiple inheritance).

To execute the thread we use the start method, which automatically calls the Thread class' run method.

If multiple processes are running simultaneously there will be interleaving. To avoid this

RunnableTest.java is an example of calling the 10's table from two different threads.

Multi Join

9. Collections

Collections = Used to store multiple objects dynamically.

Most used collections:

i] ArrayList

ii] HashSet

iii] HashMap

✔ ArrayList → Allows duplicates ✔ HashSet → No duplicates ✔ HashMap → Key-Value pairs ✔ All are in java.util package

i] A L

ii] H S

iii] H M

About

Core Java concepts, hands-on programs and projects for building a strong Java foundation.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages