Test in java

A

Arun

Guest
**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;


Code:
void displayInfo() 
{
 System.out.println(studentName + " is " + studentAge + " years old.");
}

}

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

  1. Compiler and Interpreter

Compiler

Translates the entire program (source code) into machine code at once.

Execution is faster.

Example: C, C++ use compilers.

Interpreter

Translates the program line by line and executes it immediately.

Execution is slower.

Example: Python, Java (partly) use interpreters.

πŸ‘‰ Java uses both:

Java code is compiled by javac (compiler) into bytecode.

Then the JVM (Java Virtual Machine) interprets/executes the bytecode.

  1. Class and Object

Class β†’ A blueprint/template that defines properties and behaviors.

Object β†’ A real-world entity created from the class.

βœ… Example:

class Car {
String color;
void drive() {
System.out.println("Car is driving");
}
}

public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // Object creation
myCar.color = "Red";
myCar.drive();
}
}

  1. ASCII and Unicode

ASCII (American Standard Code for Information Interchange)

7-bit code (128 characters).

Covers English letters, digits, symbols.

Example: β€˜A’ = 65, β€˜a’ = 97.

Unicode

16-bit or more (65,536+ characters).

Supports all languages (English, Tamil, Hindi, Chinese, etc.).

Java uses Unicode for characters.

**

Continue reading...
 


Join 𝕋𝕄𝕋 on Telegram
Channel PREVIEW:
Back
Top