Java 17 for Absolute Beginners: A Complete Guide to Get Started with Java Programming
- Get link
- X
- Other Apps
Want to start learning Java programming? This guide is just for you. We’ll cover the basics of Java 17 in simple language—no experience needed!
✅ What You’ll Learn:
-
What is Java?
-
Java 17 Features (for beginners)
-
Basic Syntax
-
Data Types
-
Operators
-
Conditionals
-
Loops
-
Functions (Methods)
-
Object-Oriented Programming (OOP)
-
New Java 17 features (Text blocks, Records)
-
Practice Program
📌 What is Java?
Java is:
-
✅ A popular and easy-to-learn language
-
✅ Platform-independent (write once, run anywhere)
-
✅ Object-oriented (based on real-world objects)
-
✅ Used for apps, websites, games, and more
Java 17 is the latest Long-Term Support (LTS) version, released in 2021.
1. Java 17 Basic Syntax
Every Java program has a class
and a main()
method.
public class HelloJava {
public static void main(String[] args) {
System.out.println("Hello, Java 17!");
}
}
🔹 Notes:
-
public class HelloJava
: Class definition -
public static void main
: Program entry point -
System.out.println
: Print to console
🔢 2. Data Types in Java
Java has two categories of data types:
✔ Primitive Data Types:
Data Type | Size | Example |
---|---|---|
| 4 bytes | 100 |
| 4 bytes | 3.14f |
| 8 bytes | 3.14159 |
| 2 bytes | 'A' |
| 1 bit | true / false |
| 8 bytes | 123456L |
| 2 bytes | 32000 |
| 1 byte | 127 |
-
String
,Arrays
,Objects
, etc.
3. Operators in Java
✔ Arithmetic: + - * / %
✔ Relational: == != > < >= <=
✔ Logical: && || !
4. Conditional Statements
➤ if-else:
int score = 85;
if (score >= 90) {
System.out.println("Excellent!");
} else if (score >= 60) {
System.out.println("Passed");
} else {
System.out.println("Failed");
}
➤ switch (enhanced in Java 17):
String day = "MONDAY";
switch (day) {
case "MONDAY" -> System.out.println("Start of the week");
case "FRIDAY" -> System.out.println("Weekend is near!");
default -> System.out.println("Regular day");
}
🔁 5. Loops in Java
Loop Type | When to Use |
---|---|
for loop |
When the number of iterations is known |
while loop |
When you want to repeat code until a condition is false |
do-while loop |
Like while , but runs at least once |
for-each loop |
For looping through arrays or collections |
➤ for loop:
for (int i = 1; i <= 5; i++) {
System.out.println("Count: " + i);
}
➤ while loop:
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
➤ do-while loop:
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
➤ for-each
Loop (Enhanced for loop)
Used to iterate over arrays, lists, or collections.
public class ForEachExample {
public static void main(String[] args) {
String[] fruits = {"Apple", "Banana", "Mango"};
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}
6. Functions (Methods)
public class Calculator {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int sum = add(10, 20);
System.out.println("Sum = " + sum);
}
}
7. Object-Oriented Programming (OOP) in Java 17
Java uses classes and objects to model real-world things.
➤ Class & Object Example:
public class Animal {
String name;
void makeSound() {
System.out.println(name + " makes a sound");
}
public static void main(String[] args) {
Animal dog = new Animal();
dog.name = "Dog";
dog.makeSound();
}
}
OOP Concepts:
Concept | Description |
---|---|
Class | Blueprint for objects |
Object | Instance of a class |
Inheritance | One class inherits another |
Encapsulation | Hiding internal details using |
Polymorphism | Many forms: method overloading/overriding |
Abstraction | Hide complex logic behind interfaces |
8. Java 17 Features for Beginners
✔ Text Blocks (Multi-line Strings):
String html = """
<html>
<body>
<h1>Hello, Java 17!</h1>
</body>
</html>
""";
System.out.println(html);
✔ Records (Immutable Data Classes):
public record Person(String name, int age) {}
public class Main {
public static void main(String[] args) {
Person p = new Person("Alice", 25);
System.out.println(p.name());
System.out.println(p.age());
}
}
9. Practice Program
public class Student {
String name;
int marks;
void printResult() {
System.out.println("Name: " + name);
if (marks >= 50) {
System.out.println("Status: Pass");
} else {
System.out.println("Status: Fail");
}
}
public static void main(String[] args) {
Student s = new Student();
s.name = "Bob";
s.marks = 65;
s.printResult();
}
}
- Get link
- X
- Other Apps
Comments
Post a Comment