DEV Community

Arun
Arun

Posted on

Basics of java

  1. Declaration (Declarisation) ✓✓ Declaration is the process of introducing a variable in a program by specifying its data type and name. At this point, memory is reserved for the variable, but it does not yet have a value (unless given a default).

📌 Syntax:
datatype variableName;
📌 Example:
int age; // declaration
String name; // declaration
Here, age is declared as an integer, name as a string.

  1. Initialization ✓✓ Initialization means assigning an initial value to a declared variable. ✓✓ It can happen at the time of declaration or later in the code.

📌 Syntax:
datatype variableName = value;
📌 Example:
int age = 20; // declaration + initialization
String name = "Arun"; // initialized with value
Here, age is initialized with 20, name with "Arun".

Program Execution Life Cycle in Java

  1. Write the Code
    ✓✓ You write the Java program in a .java file using an editor or IDE.
    **Example: Hello.java

  2. Compile the Code
    ✓✓ The Java compiler (javac) compiles the .java file into bytecode.

Output : A . class file is created.
**Example: Hello.class

  1. Class Loader
    ✓✓ When you run the program, the Class Loader loads the .class file into JVM memory.
    ✓✓ It handles loading, linking, and initialization of classes.

  2. Bytecode Verification
    ✓✓ JVM’s Bytecode Verifier checks the .class file for security and correctness (e.g., no illegal code, stack overflow issues

  3. JVM Execution
    ✓✓ The Interpreter reads bytecode instructions one by one and executes them.
    ✓✓The JIT Compiler (Just-In-Time) may also compile frequently used bytecode into native machine code for better performance.

  4. Execution in OS
    ✓✓ The translated code finally executes as native instructions on the operating system and hardware.

  5. Program Output
    ✓✓The program produces the result/output on the console.

What is methods and types of methods in java ?

✓ A method in Java is a block of code that performs a specific task, executes when it is called, and can return a value or be void.

👉 In simple words: A method is like a function in other languages, used for code reusability and modularity.

***Syntax of a Method
returnType methodName(parameters) {
// method body
// statements
return value; // if returnType is not void
}
Types of Methods in Java

  1. Built-in Methods (Predefined methods) ✓✓ Provided by Java libraries (inbuilt).
  2. User-defined Methods ✓✓ Created by the programmer to perform custom tasks.

Rules for identifiers in java

✓ In Java, an identifier is the name you give to classes, methods, variables, interfaces, etc.
✅ Rules for Identifiers in Java:

  1. Allowed characters Letters (A-Z, a-z) Digits (0-9) Underscore (_) Dollar sign ($) 👉 Example: myVar, student_name, $_count
  2. Must not begin with a digit
    Invalid - 1number
    Valid - number1

  3. No special characters or spaces (except _ and $)
    Invalid - my-name, first name
    Valid - myName, first_name

  4. Cannot be a reserved keyword (like int, class, static, if)
    Invalid: class
    Valid: className

  5. Case-sensitive
    Student and student are different identifiers

  6. No length limit:
    ✓ You can make identifiers very long, but short and meaningful names are recommended.

  7. Convention rules (not enforced by compiler but recommended):

✓ Variables & methods → start with a lowercase letter (studentAge, calculateSum())

✓ Classes & interfaces → start with an uppercase letter (Student, CarDetails)

✓ Constants → written in UPPERCASE with underscores (MAX_VALUE, PI)

**Example Program
class StudentDetails {
int studentAge;

String studentName;

void displayInfo() 
{
 System.out.println(studentName + " is " + studentAge + " years old.");
}
Enter fullscreen mode Exit fullscreen mode

}

  1. What is Datatypes in java ?

✓ A data type in Java is a classification that specifies which type of value a variable can hold and what kind of operations can be performed on it.

In simple words:
👉 Data type tells the compiler and JVM what kind of data (numbers, characters, text, true/false, etc.) is stored in a variable.

**Example Program
int age = 25; // int → stores integer values
double salary = 4500.50; // double → stores decimal values
char grade = 'A'; // char → stores a single character
boolean flag = true; // boolean → stores true/false

Types of datatypes

  1. Primitive Data Types
  2. Non primitive Data Types

Top comments (0)