Skip to main content

Java Fundamentals - Build, Compile, and Create with Confidence

Java Fundamentals - Build, Compile, and Create with Confidence

1. Introduction to Java

Java is one of the most popular and widely used programming languages. It’s platform-independent, object-oriented, and known for its stability and scalability.

  • Write once, run anywhere (WORA).
  • Strongly typed and class-based.
  • Used for Android, web, and enterprise applications.
Note: Java runs on the JVM (Java Virtual Machine) and compiles .java files into .class bytecode.

2. Setting Up Java

To start coding in Java:

  • Install JDK (Java Development Kit).
  • Use an IDE like IntelliJ IDEA, Eclipse, or VS Code.
public class Main {
  public static void main(String[] args) {
    System.out.println("Hello, Java!");
  }
}
Tip: Every Java program must have a main method — it’s the entry point.

3. Basic Syntax

  • Class names should start with a capital letter.
  • Statements end with a semicolon (;).
  • Code blocks use curly braces { }.

4. Variables and Data Types

Variables must have a type before use:

int age = 25;
double price = 19.99;
char grade = 'A';
String name = "Alice";
boolean isJavaFun = true;
Note: Java is statically typed — you must declare variable types explicitly.

5. Operators

int a = 10, b = 5;

System.out.println(a + b); // 15
System.out.println(a > b); // true
System.out.println(a == b); // false
System.out.println(a % b); // 0

6. Conditionals and Loops

if (a > b) {
  System.out.println("a is greater");
} else {
  System.out.println("b is greater");
}

for (int i = 1; i <= 5; i++) {
  System.out.println(i);
}

7. Methods (Functions)

public class Main {
  static int add(int x, int y) {
    return x + y;
  }

  public static void main(String[] args) {
    System.out.println(add(5, 3));
  }
}

8. Classes and Objects

class Person {
  String name;
  int age;

  Person(String n, int a) {
    name = n;
    age = a;
  }

  void introduce() {
    System.out.println("Hi, I'm " + name + " and I'm " + age);
  }
}

public class Main {
  public static void main(String[] args) {
    Person p = new Person("Bob", 25);
    p.introduce();
  }
}

9. Inheritance and Polymorphism

class Animal {
  void sound() {
    System.out.println("Animal sound");
  }
}

class Dog extends Animal {
  void sound() {
    System.out.println("Woof!");
  }
}

public class Main {
  public static void main(String[] args) {
    Animal a = new Dog();
    a.sound(); // Woof!
  }
}

10. Arrays and Collections

String[] fruits = {"Apple", "Banana", "Cherry"};
System.out.println(fruits[0]);

import java.util.ArrayList;

ArrayList list = new ArrayList<>();
list.add("Java");
list.add("Kotlin");
System.out.println(list.size());

11. Exception Handling

try {
  int result = 10 / 0;
} catch (ArithmeticException e) {
  System.out.println("Error: " + e.getMessage());
} finally {
  System.out.println("End of program");
}

12. Best Practices

  • Follow CamelCase naming for classes and methods.
  • Keep methods short and focused.
  • Use meaningful variable names.
  • Close resources like files or scanners properly.

13. Mini Project: Simple Calculator

import java.util.Scanner;

public class Calculator {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter two numbers: ");
    int a = input.nextInt();
    int b = input.nextInt();
    System.out.println("Sum: " + (a + b));
  }
}

14. Conclusion

Java is a robust, portable, and versatile language that powers millions of apps worldwide. Once you master the basics, move on to topics like Streams, Multithreading, and Spring Framework.

Practice regularly and write small projects to strengthen your understanding!

Comments

Popular posts from this blog

How Android came into Existence

Android is a Linux Based Working Framework developed by GOOGLE which gives a rich application System and helps in creating intelligent applications. The first android operating system was released to the mobile market in 2007 with a considerable lot of its variants named in Sequential request going from A-N and impending is O.                                                               ANDROID VERSION HISTORY Alpha – The first primary adaptation of Android working Framework by Google. It has fundamental usefulness with a straightforward program and other Google applications like Gmail, Guides and YouTube.   Beta - Later on with Android 1.1 few greater usefulness added, the Programming interface changes from level 1 in Android 1.0 to level 2. It upholds connection with MMS (Multimedia Messages...