๐After deciding to strengthen my programming fundamentals as part of my DevOps learning journey, I started learning Java.
I know these are basic concepts, but every experienced developer once started here. Building strong fundamentals makes learning advanced topics much easier later.
"Starting from scratch is never a weakness. Whether the concepts are basic or advanced, every lesson learned today becomes the building block for tomorrow's expertise."
In this blog, I'll summarize everything I learned during my first four days of Java.
Day 1 โ Introduction to Java
Java is a high-level, object-oriented programming language designed to be platform-independent.
The famous Java principle is:
Write Once, Run Anywhere (WORA)
This is possible because Java programs are first compiled into bytecode, which can run on any operating system that has a Java Virtual Machine (JVM).
How Java Works
Java Source Code (.java)
โ
โผ
Java Compiler
(javac)
โ
โผ
Bytecode (.class)
โ
โผ
Java Virtual Machine (JVM)
โ
โผ
Machine Code
โ
โผ
Operating System
(Windows/Linux/macOS)
The JVM converts bytecode into machine code that the operating system understands.
JDK vs JRE vs JVM
JVM (Java Virtual Machine)
- Executes Java bytecode
- Converts bytecode into machine code
- Makes Java platform independent
JRE (Java Runtime Environment)
The JRE contains:
- JVM
- Core Java Libraries
- Runtime files
Its purpose is simply to run Java applications.
JDK (Java Development Kit)
The JDK contains:
- JRE
- Java Compiler (javac)
- Debugger
- Documentation
- Development tools
The JDK is used to develop Java applications.
IDE
IDE stands for Integrated Development Environment.
Popular Java IDEs include:
- IntelliJ IDEA
- Eclipse
- VS Code
- NetBeans
An IDE provides:
- Code completion
- Debugging
- Syntax highlighting
- Project management
First Java Program
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Understanding the code
public
Accessible from anywhere.
class
Defines a class.
Hello
Class name.
main()
Entry point of every Java application.
System.out.println()
Prints output to the console.
Java Keywords
Keywords are reserved words already defined by Java.
Examples:
class
public
private
static
void
if
else
switch
while
for
return
Keywords cannot be used as variable names.
Variables
A variable stores data in memory.
Example:
int age = 22;
Rules for naming variables:
- Case-sensitive
- Must begin with a letter,
_, or$ - Cannot begin with a number
- Cannot be a Java keyword
- Prefer camelCase naming
Example:
int employeeAge;
String firstName;
double accountBalance;
Primitive Data Types
Java has eight primitive data types.
| Data Type | Size | Example |
|---|---|---|
| byte | 1 byte | 100 |
| short | 2 bytes | 2000 |
| int | 4 bytes | 100000 |
| long | 8 bytes | 100000000L |
| float | 4 bytes | 12.5f |
| double | 8 bytes | 12.56 |
| char | 2 bytes | 'A' |
| boolean | 1 bit (logical) | true |
Example:
int age = 22;
float cgpa = 8.75f;
double salary = 25000.55;
char grade = 'A';
boolean isPassed = true;
Notice the f after float values.
Without it, Java treats decimal numbers as double.
Type Conversion
Implicit Conversion (Widening)
Smaller data types are automatically converted into larger data types.
int age = 25;
long number = age;
No data loss occurs.
Explicit Conversion (Casting)
Large data types can be converted into smaller ones.
int age = 150;
byte newAge = (byte) age;
Output:
-106
Since byte ranges only from -128 to 127, overflow occurs.
Comments
Single-line comment
// This is a comment
Multi-line comment
/********************
This is
a multi-line comment
********************/
Day 2 โ Binary Number System
Computers understand only:
0
1
Every value stored in memory is represented in binary.
Example:
int age = 12;
Internally:
00000000 00000000 00000000 00001100
Bytes and Bits
1 Byte = 8 Bits
short = 16 bits
int = 32 bits
long = 64 bits
MSB and LSB
MSB
Most Significant Bit
In signed integers, the MSB represents the sign bit.
- 0 โ Positive
- 1 โ Negative (using two's complement representation)
LSB
Least Significant Bit
Represents the smallest place value.
Two's Complement
Negative numbers are represented using Two's Complement.
Steps:
- Invert all bits
- Add 1
Example:
5
Binary
00000101
Invert
11111010
Add 1
11111011
= -5
Java Operators
Arithmetic Operators
+
-
*
/
%
Example:
int a = 10;
int b = 3;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
System.out.println(a%b);
Assignment Operators
=
+=
-=
*=
/=
%=
Relational Operators
==
!=
>
<
>=
<=
Example:
System.out.println(5>3);
System.out.println(5==5);
System.out.println(5!=3);
Logical Operators
&&
||
!
Example
boolean a = true;
boolean b = false;
System.out.println(a && b);
System.out.println(a || b);
System.out.println(!a);
Bitwise Operators
&
|
^
~
<<
>>
>>>
Increment and Decrement
i++;
i--;
Ternary Operator
condition ? value1 : value2;
Example
int age = 18;
String result = age >= 18 ? "Adult" : "Minor";
System.out.println(result);
Taking Input from User
Java provides the Scanner class.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.println(age);
sc.close();
}
}
Useful methods:
nextInt()
nextFloat()
nextDouble()
next()
nextLine()
nextBoolean()
Calling close() releases the underlying input resource when you're done using the scanner.
Day 3 โ Conditional Statements
Java executes code depending on conditions.
if Statement
int age = 20;
if(age >= 18){
System.out.println("Eligible");
}
if-else
if(age >=18){
System.out.println("Adult");
}
else{
System.out.println("Minor");
}
if-else-if
int marks = 85;
if(marks >=90){
System.out.println("A");
}
else if(marks>=75){
System.out.println("B");
}
else{
System.out.println("C");
}
Nested if
if(age>=18){
if(age<60){
System.out.println("Working");
}
}
Switch Statement
int day = 3;
switch(day){
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid");
}
Switch statements improve readability when checking many fixed values.
Day 4 โ Loops
Loops execute a block of code repeatedly.
for Loop
for(int i = 1; i <= 5; i++){
System.out.println(i);
}
Structure:
Initialization
Condition
Update
Loop Body
Example:
Sum of first n numbers
int n = 10;
int sum = n * (n + 1) / 2;
System.out.println(sum);
while Loop
Used when the number of iterations is unknown.
int i = 1;
while(i <= 5){
System.out.println(i);
i++;
}
do-while Loop
Executes at least once.
int i = 1;
do{
System.out.println(i);
i++;
}while(i <= 5);
break
Stops the loop immediately.
for(int i=1;i<=10;i++){
if(i==5)
break;
System.out.println(i);
}
continue
Skips the current iteration.
for(int i=1;i<=5;i++){
if(i==3)
continue;
System.out.println(i);
}
Nested Loops
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
System.out.print("* ");
}
System.out.println();
}
Output
* * *
* * *
* * *
Labeled Break
outer:
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
if(i==2)
break outer;
}
}
A labeled break exits multiple nested loops at once.
What's Next?
The next topic in my learning journey is Arrays in Java, where I'll explore:
- Creating arrays
- Accessing elements
- Iterating using loops
- Array operations
- Multidimensional arrays
Stay tuned!
Final Thoughts
These first four days reminded me that programming isn't about memorizing syntaxโit's about understanding how the language works behind the scenes.
From learning how Java compiles code into bytecode, to understanding primitive data types, operators, conditions, and loops, every concept lays the groundwork for more advanced topics like Object-Oriented Programming, Collections, and Multithreading.
I'm documenting this journey publicly to reinforce my own understanding and hopefully help others who are just getting started.
If you're also learning Java, feel free to connect and share your learning journey. Happy coding! ๐
Top comments (0)