Java Fundamentals & Syntax
Duration: 7.0 min
Java Fundamentals & Syntax
Java is a powerful, object-oriented programming language that runs on the Java Virtual Machine (JVM). This makes it platform-independent - write once, run anywhere. Java is widely used in enterprise applications, Android development, and large-scale systems. Understanding Java fundamentals is essential for becoming a professional developer. The JVM handles memory management, garbage collection, and provides security features that make Java robust and reliable for production systems.
JVM, JDK, and JRE
The Java ecosystem consists of three key components: JVM (Java Virtual Machine) executes bytecode, JRE (Java Runtime Environment) provides libraries and JVM, and JDK (Java Development Kit) includes compiler and tools. Understanding these distinctions is crucial for Java development. The JVM is what makes Java platform-independent - it abstracts away OS-specific details. The JDK is what you need for development, while JRE is sufficient for running Java applications.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Hello, World!public class Variables {
public static void main(String[] args) {
int age = 25;
String name = "Alice";
double salary = 50000.50;
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
}Name: Alice
Age: 25
Salary: 50000.5public class DataTypes {
public static void main(String[] args) {
byte b = 127;
short s = 32000;
int i = 2147483647;
long l = 9223372036854775807L;
float f = 3.14f;
double d = 3.141592653589793;
char c = 'A';
boolean bool = true;
System.out.println("All data types initialized");
}
}All data types initializedpublic class TypeCasting {
public static void main(String[] args) {
int intValue = 100;
long longValue = intValue;
double doubleValue = 99.99;
int intFromDouble = (int) doubleValue;
System.out.println("Int to Long: " + longValue);
System.out.println("Double to Int: " + intFromDouble);
}
}Int to Long: 100
Double to Int: 99public class Comments {
// Single line comment
public static void main(String[] args) {
/* Multi-line comment
explaining the code */
System.out.println("Comments example");
}
}Comments examplepublic class Operators {
public static void main(String[] args) {
int a = 10, b = 5;
System.out.println("Addition: " + (a + b));
System.out.println("Subtraction: " + (a - b));
System.out.println("Multiplication: " + (a * b));
System.out.println("Division: " + (a / b));
System.out.println("Modulus: " + (a % b));
}
}Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulus: 0public class Comparison {
public static void main(String[] args) {
int x = 10, y = 20;
System.out.println("x == y: " + (x == y));
System.out.println("x != y: " + (x != y));
System.out.println("x > y: " + (x > y));
System.out.println("x < y: " + (x < y));
}
}x == y: false
x != y: true
x > y: false
x < y: truepublic class Logical {
public static void main(String[] args) {
boolean a = true, b = false;
System.out.println("a && b: " + (a && b));
System.out.println("a || b: " + (a || b));
System.out.println("!a: " + (!a));
}
}a && b: false
a || b: true
!a: false💡 Tip: Best Practice: Always use meaningful variable names and follow Java naming conventions (camelCase for variables, PascalCase for classes).
💡 Tip: Common Mistake: Forgetting semicolons at the end of statements. Every statement in Java must end with a semicolon.
Learn more: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/
❓ What does JVM stand for?
❓ Which component contains the Java compiler?
❓ What is the correct syntax for the main method?
❓ Which data type is used for whole numbers?
❓ What is type casting?
❓ Which operator is used for modulus operation?
❓ What is the output of 10 / 3 in Java?
❓ Which of these is a valid variable name?
❓ What does the && operator do?
❓ How do you write a single-line comment in Java?