DEV Community

MANOJ K
MANOJ K

Posted on

Introduction to Java - Basics


Java is one of the most popular and widely used programming languages in the world. It is simple, secure, and platform-independent, making it a favorite among beginners and professionals alike. If you’re starting your journey with Java, here are some core concepts you should understand.

1.. Keywords

*Keywords are reserved words in Java that have a special meaning.
*Examples: class, int, if, else, for, public, static.
*You cannot use keywords as variable names, method names, or identifiers.
*Java has 50+ keywords.

2.. Tokens

*A token is the smallest unit of a Java program.
Types of tokens:
1)Keywords
2)Identifiers
3)Literals
4)Operators
5)Separators (; , { } [ ])

3.. Identifiers

*Identifiers are names given to variables, methods, classes, or objects.
*Examples: age, Student, main, calculateSum.

*Rules for Identifiers:

1) Can contain letters, digits, _, and $.
2) Cannot start with a digit.
3) Cannot use keywords.
4) Case-sensitive (Age ≠ age).
5) No spaces allowed.

4.. Literals

  • Literals are fixed values written directly in the code.

  • Types of Literals:

1) Integer → 10
2) Floating-point → 3.14
3) Character → 'A'
4) String → "Hello"
5) Boolean → true, false
6) Null Literal → null

5.. Declaration & Initialization

  • Declaration: Telling the compiler about a variable.

        int age;
    

*Initialization: Assigning a value to a variable.

        age = 20;
Enter fullscreen mode Exit fullscreen mode
  • Both together:

        int age = 20;
    

6.. Features of Java

  • Simple & Object-Oriented
  • Platform Independent (WORA – Write Once, Run Anywhere)
  • Secure
  • Robust (strong memory management)
  • Portable
  • Multithreaded
  • High Performance (with JIT Compiler)

7.. History of Java

  • Developed by James Gosling at Sun Microsystems in 1991.
  • Released to the public on 23 May 1995.
  • Originally called Oak, later renamed Java.
  • Currently owned by Oracle Corporation.

8.. Operators

*Operators are symbols that perform operations.

*Types of Operators:

1) Arithmetic → + - * / %
2) Relational → < > <= >= == !=
3) Logical → && || !
4) Assignment → = += -=
5) Unary → ++ --
6) Ternary → ?:

9.. Data Types

*Java data types define the kind of data a variable can hold.

1) Primitive Types (8):

*byte, short, int, long (integers)
*float, double (decimals)
*char (characters)
*boolean (true/false)

2) Non-Primitive Types:

*String, Arrays, Classes, Objects

10.. Class and Object

  • Class: A blueprint for objects.

class Student {
String name;
int age;
}

  • Object: An instance of a class.

Student s1 = new Student();

11.. Methods and Types

  • A method is a block of code that performs some task.

  • Types of methods:

1) Predefined Methods – already available in Java (e.g., System.out.println())

2) User-defined Methods – created by programmers.

12.. Number System Conversions

  • Java supports different number systems:

  • Decimal ↔ Binary ↔ Octal ↔ Hexadecimal

  • Examples:

1) Decimal (10) → Binary (1010)

2) Decimal (10) → Octal (12)

3) Decimal (10) → Hex (A)

13.. Program Execution Life Cycle

  • Steps in executing a Java program:

1) Write Code → Create .java file.
2) Compile → javac generates .class bytecode.
3) Class Loader → Loads bytecode into JVM.
4) Bytecode Verifier → Checks security and errors.
5) Interpreter / JIT Compiler → Converts bytecode to machine code.
6) Execution → Runs on CPU.

14.. Local Variable vs Instance Variable

*Local Variable → Declared inside a method and used only within it.

*Instance Variable → Declared inside a class but outside any method. Each object gets its own copy.

*Example:

class Example {
int instanceVar = 10; // instance variable
void show() {
int localVar = 20; // local variable
}
}

15.. Compiler and Interpreter

  • Compiler: Translates Java source code into bytecode (.class file).

  • Interpreter (JVM): Executes bytecode line by line.

  • Together, they make Java both compiled and interpreted.





Top comments (0)