Java is a high-level, object-oriented programming language designed for building robust, scalable applications. It follows the principle of "write once, run anywhere" (WORA) through the Java Virtual Machine (JVM).
- Object-Oriented: Organized around objects and classes
- Platform Independent: Runs on any system with a JVM
- Secure: Built-in security features and memory management
- Multi-threaded: Supports concurrent programming
- Robust: Strong exception handling and type checking
- Java Development Kit (JDK) installed [Used JDK 17 on the tutorial]
- A code editor or IDE (IntelliJ IDEA, VS Code, Eclipse)
- Basic programming knowledge
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}The Java compilation and execution process follows these steps:
┌───────────────┐ ┌───────────────┐ ┌─────────────┐ ┌─────────────┐
│ Source Code │ │ Compiler │ │ Bytecode │ │ JVM │
│ (.java) │────▶│ (JDK) │────▶│ (.class) │────▶│ │
└───────────────┘ └───────────────┘ └─────────────┘ └─────────────┘
- Source Code (.java): You write your Java code in
.javafiles - Compiler (JDK): The Java compiler translates your code into bytecode
- Bytecode (.class): Platform-independent intermediate code
- JVM: Executes the bytecode on any system with a JVM installed, even a calculator if JVM is installed can run your code.
- Function : Is written inside the class component.
void main(){
}- Class: it can hold many function as it wants
class Main {
void first(){
}
void second(){
}
}- Variables & Data Types: int, String, boolean, etc.
- Control Flow: if/else, loops, switch statements
- Functions/Methods: Reusable blocks of code
- Classes & Objects: Blueprints and instances
- Exception Handling: try/catch blocks
System.out.print("Hello World");-
In order to get output in your java code you need to memorize this shit ass code. Don't ever forget the semicolon at the end. Else you gonna be a dumbass like me searching for the bug all over the code and ending up its a shitass semicolon i missed. I hate thiss life that I have to learn Java already. You should hate yours too. <3.
-
Use
printlnif you want to print in new line. Fuck Java, the bare minimum in other lang are struggled bread here. Implementation:
System.out.println("Hello World");
System.out.print("Hello World");- Type
soutand enter in any CODE editor to get boilerplate of this fucking output syntax. Chill now.
public class Main{
public static void main(String[] args){
}
}- This shit is the boiler plate of java, I have to write whole of this just inorder to print Hello World. Isn't python ot javascript too simple ? just write
print("Hello World")orconsole.log("Hello World")and the world would wave you back, This fucking java just wants me to write a whole ass novel just to get replied Hello World. Fuck you Java.
A variable is like a labeled storage box in your computer’s memory where you can keep a piece of data (like a number, text, or more complex info) that can change over time.
Think of it this way:
- You have a box labeled age.
- You put the number 25 inside it.
- Later, you can open the box and change it to 26.
In java we can declare variables with:
int age = 19;
boolean is_virgin = true;
double pi = 3.141;
String name = "Prashant";- Variables are stored inside memory and can be accessed with the variable lables such as
a,b,cord.
Java has several primitive data types:
- int: Integer values (e.g.,
42) - Size: 4 bytes - double: Decimal numbers (e.g.,
3.14) - Size: 8 bytes - boolean: True or false values - Size: 1 byte (though it may vary)
- char: Single characters (e.g.,
'A') - Size: 2 bytes - long: Large integers (e.g.,
1000000L) - Size: 8 bytes - float: Single-precision decimals (e.g.,
2.5f) - Size: 4 bytes - byte: Small integers (e.g.,
100) - Size: 1 byte - short: Short integers (e.g.,
30000) - Size: 2 bytes
int count = 10;
boolean isActive = true;
double temperature = 98.6;Non-primitive data types in Java are more complex and can hold multiple values or objects. They include:
-
Arrays: A collection of elements of the same type.
int[] numbers = {1, 2, 3, 4, 5};
-
Strings: A sequence of characters.
String greeting = "Hello, World!";
-
Classes: User-defined data types that can contain fields and methods.
class Person { String name; int age; }
-
Interfaces: A reference type in Java, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types.
interface Animal { void makeSound(); }
-
Enums: A special Java type used to define collections of constants.
enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
These non-primitive data types allow for more complex data structures and functionalities in Java programming.
To take user input in Java, use the Scanner class from the java.util package.
import java.util.Scanner;Scanner sc = new Scanner(System.in);- Read a word (String):
String name = sc.next();- Read an entire line (String):
String fullName = sc.nextLine();- Read an integer:
int age = sc.nextInt();- Read a floating-point number:
double price = sc.nextDouble();- Read a boolean:
boolean isActive = sc.nextBoolean();import java.util.Scanner;
public class InputExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.println("Name: " + name);
System.out.println("Age: " + age);
sc.close(); // Close the scanner
}
}- Always close the
Scannerwhen done usingsc.close()to avoid resource leaks - Use
nextLine()afternextInt()ornextDouble()to consume the newline character next()reads only one word, whilenextLine()reads the entire line
Java supports basic arithmetic operations:
- Addition (
+): Adds two numbers - Subtraction (
-): Subtracts one number from another - Multiplication (
*): Multiplies two numbers - Division (
/): Divides one number by another - Modulus (
%): Returns the remainder of a division
int a = 10;
int b = 3;
System.out.println(a + b); // 13
System.out.println(a - b); // 7
System.out.println(a * b); // 30
System.out.println(a / b); // 3
System.out.println(a % b); // 1