Java Miniatures is a comprehensive repository dedicated to honing Java programming skills and delving into software engineering concepts. As a passionate Computer Science graduate aspiring to excel in the field of Software Engineering, I've curated this repository to serve as a platform for my continuous learning journey and to showcase my expertise in Java programming.
The repository encompasses a wide range of Java topics, starting from fundamental concepts and progressing towards more advanced software engineering principles. Each section of the repository is meticulously crafted to provide clear explanations, code examples, and practical projects, all aimed at deepening understanding and mastery of Java and its application in real-world software development scenarios. The repo will include the fundamental Computer Science topics.
Throughout this learning journey, I've encountered various challenges, both technical and conceptual. However, each challenge has served as an opportunity for growth and learning. In the future, I aim to implement additional features such as interactive coding exercises, more extensive project examples, and collaborative learning opportunities to further enhance the repository's utility and effectiveness as a learning resource.
This repository IS NOT A ROADMAP FOR LEARNING JAVA exactly, you can use different arrangement e.g. If you want to learn generics first before knowing about frameworks (e.g.spring boot) it won't cause a problem, I use this repo more likely as a documentation for my programming skills progress, how my code, thinking, experience in using java change over time in addition to aiming to be a good reference in future (ISA) for whom are concerned about learning java and software engineering.
Java Basics
- Elementary Programming
- Control Structure Part 1: (Selections)
- Mathematical Functions,Characters, and Strings
- Control Structure Part 2: (Iterators)
- Methods
- Arrays
Object-Oriented Programming
- Objects and Classes
- Object-Oriented Thinking
- Inheritance and Polymorphism
- Exception Handling and Text I/O
- Abstract Classes and Interfaces
Java in Action
- Build tools
- Ant
- Maven
- Gradle
- building GUI
- JavaFX
- Java Swing
- Web Frameworks
- Spring
- Springboot
- ORM
- JPA
- Hibernate
Advanced Java Topics
- Binary I/O
- Generics
- How JVM works
- Multithreading and Parallel Programming
- Networking
- Java Database Programming
Data Structures
- Recursion
- Stacks & Queues
- Lists
- Trees
- Priority Queues
- Maps & Hash Tables
- Search Trees
Algorithms
- Asymptotic analysis
- Sorting
- Searching
- Graphs
- Strings
- OOA & OOD
- SOLID Principles
- Design Patterns
- Refactoring Techniques
- Software Development Methodologies
- Small-scale Java Projects
- Medium-scale Software Engineering Projects
- Real-world Application Examples
according to Java Docs official website Oracle Docs.
'Java is a general-purpose, concurrent, strongly typed, class-based object-oriented programming language'
Don't worry these charactaristics of the language will be more clear as we go down in this repo
To get started with java we must install JDK on our local machine, (here, I'm considering you are a beginner or totally newcommer in programming world or even didn't visit virtualization concepts|tools before).
Installing JDK is a must it provides the necessary tools and libraries to develop, compile, and run Java applications.
click on the following link oracle and install the latest JDK versions on your own device
download and install JDK based on your os and processor acritecture set
"JDK" stands for Java Development Kit, it's the development environment for building java applications, it consists of JVM -Java Virtual Machine-, JRE -Java Runtime Environment- and development tools like compiler etc.
Java is machine independent language or it's more descriptive to refer to it as platform-independent; you can run it on any platform, while the JVM is platform-dependent; different JVM is designed for each platform (Linux, Windows, MacOs).
In othwe words, if we have three local devices, each of which has a different platform installed in it e.g.(windows, mac, linux), all is required to install a JVM for each device and the java files can run on any machine of them after compiling it just one time.
Once you downloaded the latest version of JDK here you have two options:
-
if it's your first time downloading
you need to edit system environment variables first
These instructions for windows users only ⚠:
- open your Program Files directory navigate till you find the jdk directory
- open the bin directory then copy its path
- press win key on your kewboard ⌨ then type in search bar down edit system environment variables
- click on Evironment Variables, from user variables choose path then click on edit
- on path window click add new, then paste the path you copied earlier and press enter
- click on move up to make it on the top of the list and voila!
-
if you've already installed earlier JDK version
-
open your cmd(win key, type cmd | win key + R then type cmd)
-
write the following:
java -version
I have JDK version 17 installed on my own, now we are going to switch it to the new version of JDK
- we'll repeat the steps of the previous option (editing system environment variables, etc)
- Check your java version again in cmd (restart it if you didn't close it already)
and Voila!
-
-
Remember this part mentioned earlier ? class-based
Java is a class-based object-oriented programming language
Let's see how this concept was applied!
Writing your first Hello world program!:-D "programmers' traditional custom"
prerequisites ⚠
if you didn't install the jdk yet, go to this part and install it
It's better if you have an IDE, till this point it's not mandatory yet but then it will be as we are not at the age of DOS anymore.
-
If you don't have an IDE: (note: I won't let you stay lazy forever, you'll have to install it)
-
create a new directory (folder) for your java practice, right-click, new, text document
-
yes, that's it just empty text document, rename this document "HelloWorld.java"
-
open the document, as mentioned before java is class based so to write any instructions they must be inside classes, now let's type our first class:
public class HelloWorld{}
public & class are both keywords all written in lower case letters
HelloWorld is the name of the classand it must be written in pascal case (the first letter of each word is capital)
inside the curly braces
{}
we are going to illustrate our code-
keywords or reserved words are predefined words in a programming language with a specific use, you can't use them for naming (simply, because the compiler will be confused if it's the word used by the language to identify some order to be executed by the program or it's just a programmer defined variable)
-
every java program should at least have one class, to execute the program we should have a main method (in other words method its name is "main")
-
inside our created class we define the main method as follows:
public static void main(String[] args){}
public, static, void, String are all keywords
naming methods using camelCase in java if it consists of more than one word other wise all lower case
-
the curly braces
{}
mean block of code, here we write the lines of code that will be executed, their place is after every class, method, control statements, specific keywords e.g. static keyword. They also represent a specific scope (we'll talk about this soon). -
inside the curly braces of the main method we will print our first hello world
System.out.println("Hello World!");
Do Not Forget The Semicolon!
any string is enclosed in double quotes
System.out.println() is the method we'll use for printing anything on the terminal or console screen to user.
-
to run your program open your cmd
win + R type cmd then enter ↩
, in the same directory, if you cannot navigate to the directory where the java file lies, simply in the address bar of your folder window type cmd as follows: -
inside cmd type
javac {yourfilename.java}
(don't forget the extension)javac replacethis.java
- write the name of the file right with its correct formate or you'll face
error: file not found
javac stands for java compile, you'll notice that a
.class
file is created on your directory this is thebytecode
. you compile one time and run the code infinitely.- to run the program write
java {yourfilename}
(note here without the extension).
and Voila!
-
you can check the file here in this directory Hello.java file.
Now we consider you a real programmer
-
every programming language has a syntax define it, simply like grammar in English edfines how you form a sentence has context, simmilarly in programming the syntax allows you to write program instructions correct or with a context for the compiler.
-
curly braces
{}
define a block of code -in other words lines of code- and also define scope. -
yup, just like C language semicolon is the statements terminator
;
-
//
double slashes for single line comments and/* comments lines go here */
for multiple lines of comments (for those who are new to programming, comments are setences or lines won't be treated as code or instruction they will be ignored by the compiler, programmers use comments to clarify points about their code, like comments show imcomplete implementation or reference to the original source code or even to clear confusion about the code they wrote) -
java is case sensitive language (you get syntax error if you mistyped a keyword or wrote its letetr capital rather than small or vice versa), e.g.
Public void fun(){...}
--> syntax error (Public is not a keyword) -
parenthesis
()
follow functions names, flow control keywords like for, while, if...etc. -
while square brackets follow 'Array' types
[]
e.g.String[]
-> indecates array of type String. -
later in advanced java programming we will get familiar with the ArrayList, we will find the angle brackets
<>
follow ArrayLists keyword in which contain the Type of the ArrayList (ArrayList is simply resizable array) e.g.ArrayList<String>
. -
every java program must have at least one class.
-
java program start execution from main method.
-
string is enclosed in double quotes while character is enclosed in single quotes.
-
Syntax errors: mistyping, missing puncituation
-
Runtime errors: occur while program is actually running if the IDE indeicated a process cannot be carried out or through input errors.
-
Logical errors (semantic) -cruel one😈-: the code actually works fine but doesn't give you the expected output e.g. you wanted to make simple program that multiply numebrs. now look at the followin code:
int x = 2, y = 2;
int result = x + y;
System.out.println(result); >>>> 4 looks fine nothing wrong, no errors, the result is right :)
try again making y = 3 instead of 2
int x = 2, y = 3;
int result = x + y;
System.out.println(result); >>> 5 here there is a problem, the code is working fine as no errors appear but wait a minute 😅, a plus " + " sign where inserted instead of " * " multiplication sign.
here where the error takes place, it's not in code it workds fine but doesn't give you the expected results, this type of errors are dedicted and corrected using tests we will come to this point later.
for more on errors types here is a useful link not for new commers , this is nice introduction to errors types of erros in java.
-
a variable is used to store data, you can think of variables as where we store values, these values can be numeric, string (group of characetrs), reference, etc.
-
basic syntax to declare a variable in java
datatype {variable-name} = value;
- declaring means you announce to your compiler about storage place of name (your variable name) and type (your variable type) and it has to reserve or allocate this amount of memory space.
- definition is when you assign a value to this identifier or the variable, equal sign
=
is the assignment operator. - assignment is right-to-lef operation -this is super important-, as you say the value 3 is assigned to variable x of type integer for this statement
int x = 3;
. - obviously, we don't have to mention "not to forget the damn semicolon
;
after each statement you write", thank you :) .
-
naming conventions:
- camel case.
- do not start naming a varable with a number
- you can start your variable name with dollar sign
$
or with underscore_
. - variables cannot be reserverd words, e.g. you cannot name your variable 'static' gives you syntax error.
- for clean code early teaching purpose, choose meaningful names for your variables describe them.
- do not contain spaces in your variable name, keep using camel case if your variable name is more than one word.
-
data types:
- In general they are two types numerical or non-numerical, numerical like integers, floating point or decimal numbers, non-numrical like strings "Hello my name is" or boolean like True or False.
- In java they are two categories, premitive data types and reference datatypes, they are classified based on how they store data, primitive copies only values while reference hold the address to the value they just copied, if you couldn't understand this no problem it will be clear later.
- primitive data types are:
- byte
- short
- int
- long
- float
- double
- boolean
- char
- reference data type -the only one you'll use for now- is String (take care of the capital letter).
- data types tell the compiler how much space to reserve for that variable in memory.
-
note as variable word declares, its value can be changes during the execution of the program, from this point let's get familiar with constants.
-
constants cannot change their value, we declare them using
final
keyword then data type follows final then the constant name, e.g.final float PI = 3.14;
- Upper case as naming convention for constants.
-
note you cannot re-declare variable but you can re-assign i, e.g.
-
you can perform simple operations using binary arithmetic operators:
+ , - , * , / , %
-
for taking custom input from user, either from console or keyboard input, we are going to start using classes.
-
In java to take user input using three classes: Console, BufferedReader, and Scanner. For now we will deal with Scanner only.
-
first of all we need to import the class, so using the keyword
import
we will invoke theScanner
class as follows: -
Then, we create a new object from Scanner class
- we created object of name input of Scanner class using new operator.
-
importing statement is placed before class declaration.
-
simple java program takes input from user to calculate the area of a circle: you can simply see the file here: ComputeCircleArea.java
-
so, at any point you want to take user input:
import java.util.Scanner;
- inside main mathod block of code
Scanner input = new Scanner(System.in);
- based on what data you want to take, e.g. prompt user to enter integer:
int number = input.nextInt();
and that's it.
-
Read-Evaluate-Print Loop (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results.
-
it's an interactive tool for quickly prototyping java code, all without the need for a
public static void main
or the need to compile your code before executing it. -
it's super easy open your cmd or powershell and type jshell
-
try simple declaring variable called hello,
var hello = "welcome to jshell"
- find more on this link how to use jshell
In this section you will learn the way you take decision.
According to documentation:
control flow statements, break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code.
types of control flow statements:
- Decision making statements.
- Iteration (Looping) statements.
- Branching statements.
in this section we'll talk about Selections | Conditionals | Decision Making statements.
Conditional keywords: if
, else
, else if
,switch
,case
,break
.
Conditional statement evaluate a (condition) if it's true or false to perform set of instructions:
a boolean expression is expression will yeild true or false, e.g. is 4 + 5 = 9? if their sum equal to nine so the answer is yes otherwise no. so, an expression will be given to be evaluated.
now, the question is how to build a conditional:
before anything let's learn about the second category you hear in this guide, relational operators:
==
<=
>=
!=
>
<
note that assignment operator
=
is not the same as==
equality operator, equality operator mean if the value of smth equals some value on the other hand the assignment oeprator assigns value to variable.
Conditionals evaluate boolean expressions, boolean expression are expression expected to have true or false as answer so we are kinda building comparison statments.
it's simply like saying: is multiplying 7 by 10 equals 70? if yes please print the result and if not do nothing.
-
Checking for alternatives: using else if we can check for alternatives, e.g. here is a simple program to check if a number is positive or negative or equal to 0:
-
you can find more examples in Test Files Elemntary, e.g. addition Quiz
we can check using if
then another if
but what we hit the goal from first time why do we continue checking? that's why we use else if instead of if to skip the rest of conditional statement checking if we already satisfied one.
- any boolean expression is enclosed by parenthesis
()
. - if by mistake you placed a semicolon after if statement
if ( y == x );
is like doing this:if ( y == x ){}
means do nothing. - avoid redundant testing of boolean expression e.g.
if (even == true)
better do thisif (even)
, so if even have a number other than zero it sill evaluate to true or if even a boolean variable it will evaluate to its boolean value. - avoid equaltiy test to float variables to avoid percision problem of floating point numbers.
- you can simplyfy boolean variable assignment:
now we could build simple conditionals using of, else and if else. What if we want to check two conditions at the same time, can we? actually yes, using compound boolean expressions we can check two conditions at the same time, but first let's get familiar with the logical operators:
|| OR operator
&& AND operator
! NOT operator
^ XOR operator
if you are a computer science, or electrical engineering student you are already familiar with thess operators as you took this knowledge in Logic design course -probably it's name nearly like this-, if you or not here's a simple explaination and I'll provide you a link for a video to simplify the concept more:
OR operator ||
check if one of the expressions at least
is true or both of them.
e.g. (5+6 == 11 || 5+7 == 11)
evaluates to true because the first expression is true. it will only evaluates to false if both of them are false.
AND operator &&
check if both of the expressions must be true
if one of them is false so the whole evaluation of both of them will be false.
NOT operator !
is to negate any value to oppose any expression in other words: !(expression) means to flip the evaluation value of this expression if it's evaluated to true the not operator will make it false and so on. same when we use !=
we mean not equal.
XOR operator ^
is used to check if both expressions evaluates to different answers, simply if both of the expression is evaluated to true or both false it return to us false, if they are different (true , false or false , true) return to us true.
we can use DeMorgan's law to best practise these compound boolean expression conditionals
!(condition1 && condition2) == ! condition1 && ! condition2 using DeMorgan's.
note &&
and ||
are called lazy operator as they perform short circuit concept as if false && anything
evaluates to false without checking the second expression same in true || anything
will evaluate to true without checking the second expression.
we use switch case statements with menu-like -means many choices- problems, e.g. choosing operation to be done on numerical values, based on day of week choosen perform some action:
This is a simple program perform basic operations on the entered numbers: you can check it from here switch program
- if
break
is not used, switch will evaluate all other cases, we use default if the expression doesn't match any case.
lastly we will learn about the boolean ternary operator (boolean expression)? (if true)expression1 : (if false)expression2
this oeprator return exp1 if it evaluates to true and exp if it evaluates to false, e.g. :
In this section you will learn about some built-in mathematical functions in addition to introducing two data types (Character and String).
let's breakdown the concepts we are going to learn:
- Methods
- Important built-in methods
- example using these methods
- limitations
- Character datatype
- Operations
- Casting
- String Reference Type
- Important methods for dealing with Strings
- Java String pool