Java Basics


Basics of Java Programming
  • Java is a high-level, object-oriented, platform-independent programming language.
  • Java code is written inside classes and the program execution starts from the main method:
    
    public class MyClass {
        public static void main(String[] args) {
            // Code goes here
        }
    }
    
    
    Key Concepts:
  • Class → Blueprint for objects
  • Method → Block of code that performs a task
  • Main Method → public static void main(String[] args) (starting point of program)
Hello World Program
    
    public class HelloWorld {
        public static void main(String[] args) {
            System.out.println("Hello, World!");
        }
    }
    
    
    Explanation:
  • System.out.println() 
    → prints a line on the console.
  • The file should be saved as
    HelloWorld.java
    (same as class name).
Variables and Data Types
    Variables
  • Variable → A name that refers to a value stored in memory.
  • Declared with a data type.

Example:

    
    int age = 25;
    double pi = 3.14;
    char grade = 'A';
    boolean isJavaFun = true;
    String name = "Alice";
    
    
Data Types
Type Description Example
int Integer numbers int x = 10;
double Decimal numbers double pi = 3.14;
char Single character char c = 'A';
boolean True or false boolean flag = true;
String Text (sequence of characters) String str = "Java";
Operators in Java
Arithmetic Operators
Operator Meaning Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus (remainder) a % b

Example:

    
    int a = 10, b = 3;
    System.out.println(a + b); // 13
    System.out.println(a % b); // 1
    
    
Relational Operators
Operator Meaning Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus (remainder) a % b
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b

Example:

    
        System.out.println(5 > 3); // true
    
    
Logical Operators
Operator Meaning Example
&& Logical AND (a > 0 && b > 0)
` `
! Logical NOT !(a > b)

Example:

    
    System.out.println((5 > 3) && (8 > 5)); // true
    System.out.println(!(5 == 5)); // false
    
    
Input and Output
Output (Display something)

Use

System.out.print()
and
System.out.println()

    
    System.out.println("Hello Java");  // prints with a new line
    System.out.print("Hello ");        // prints without new line
    System.out.print("Java");
    
    
Input (Take user input)

Use Scanner class from java.util package.

Example:

    
    import java.util.Scanner;

    public class InputExample {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in); // create scanner object

            System.out.print("Enter your name: ");
            String name = input.nextLine();          // read a full line

            System.out.print("Enter your age: ");
            int age = input.nextInt();                // read an integer

            System.out.println("Hello, " + name + ". You are " + age + " years old.");
        }
    }
    
    

Important Scanner methods:

  • nextLine() → read a line of text
  • nextInt() → read an integer
  • nextDouble() → read a decimal number
  • next() → read a single word (without spaces)