Skip to content

pr4shxnt/beginner.java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Introduction to Java

What is Java?

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).

Key Features

  • 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

Getting Started

Prerequisites

  • Java Development Kit (JDK) installed [Used JDK 17 on the tutorial]
  • A code editor or IDE (IntelliJ IDEA, VS Code, Eclipse)
  • Basic programming knowledge

Your First Program

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
    }
}

How is my code working ?

The Java compilation and execution process follows these steps:

┌───────────────┐     ┌───────────────┐     ┌─────────────┐     ┌─────────────┐
│ Source Code   │     │ Compiler      │     │ Bytecode    │     │ JVM         │
│ (.java)       │────▶│ (JDK)         │────▶│ (.class)    │────▶│             │
└───────────────┘     └───────────────┘     └─────────────┘     └─────────────┘ 
  1. Source Code (.java): You write your Java code in .java files
  2. Compiler (JDK): The Java compiler translates your code into bytecode
  3. Bytecode (.class): Platform-independent intermediate code
  4. JVM: Executes the bytecode on any system with a JVM installed, even a calculator if JVM is installed can run your code.

Main components of our 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(){

    }
 }

Basic Concepts

  • 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

Output:

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 println if 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 sout and enter in any CODE editor to get boilerplate of this fucking output syntax. Chill now.

Boiler Plate:

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") or console.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.

Variables

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, c or d.

Primitive Data Types

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

Example:

int count = 10;
boolean isActive = true;
double temperature = 98.6;

Non-Primitive Data Types

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.

Taking Input in Java

Using Scanner Class

To take user input in Java, use the Scanner class from the java.util package.

Import the Scanner class:

import java.util.Scanner;

Create a Scanner object:

Scanner sc = new Scanner(System.in);

Reading different data types:

  • 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();

Complete Example:

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
    }
}

Important Notes:

  • Always close the Scanner when done using sc.close() to avoid resource leaks
  • Use nextLine() after nextInt() or nextDouble() to consume the newline character
  • next() reads only one word, while nextLine() reads the entire line

Arithmetic Operators

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

Example:

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

Resources

About

idk maybe sum learn java typeshii ?

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages