←Back to Subject
Notes
Notes
Study material and explanations
Diagrams
Diagrams
Visualize concepts easily
Mnemonics
Mnemonics
Tricks to remember faster
One Night Prep
One Night Prep
Short and focused revision
Revision
Revision
Quick summary and key points
Important Questions
Important Questions
Practice most relevant questions
Notes

Primitive Data Types, Variables, Constants, Operators & Expressions

Study material and explanations for Unit 1

Primitive Data Types, Variables, Constants, Operators & Expressions

1. Data Types in Java

Java is a statically and strongly typed language, which means every variable must have its data type declared before it is used, and the compiler strictly checks type compatibility.

Java data types are divided into two categories:

  1. Primitive Data Types (8 built-in types): Store simple raw values directly in memory (byte, short, int, long, float, double, char, boolean).
  2. Non-Primitive / Reference Data Types: Store memory references (addresses) to objects in the Heap (String, Arrays, Classes, Interfaces).
Figure 1.7: Classification of Data Types in Java
Primitive Data Types (8 Types)
  • Integers: byte (1B), short (2B), int (4B), long (8B)
  • Floating-Point: float (4B), double (8B)
  • Character: char (2B - Unicode)
  • Boolean: boolean (1 bit - true / false)
Non-Primitive (Reference) Types
  • Classes: String, Scanner, Student
  • Interfaces: Runnable, ActionListener
  • Arrays: int[], String[][]
  • Default value of any reference is null

2. The 8 Primitive Data Types in Detail

Unlike C/C++, the size of primitive data types in Java is fixed across all operating systems and hardware architectures.

Data Type Category Size Default Value Value Range Example Literal
byte Integer 1 Byte (8 bits) 0 -128 to +127 byte b = 100;
short Integer 2 Bytes (16 bits) 0 -32,768 to +32,767 short s = 25000;
int Integer 4 Bytes (32 bits) 0 -2^31 to 2^31-1 (≈ ± 2.14 billion) int pin = 452001;
long Integer 8 Bytes (64 bits) 0L -2^63 to 2^63-1 long pop = 1400000000L;
float Floating-Point 4 Bytes (32 bits) 0.0f Up to 6–7 decimal digits float pi = 3.14f;
double Floating-Point 8 Bytes (64 bits) 0.0d Up to 15 decimal digits double gpa = 8.95;
char Character 2 Bytes (16 bits) '\u0000' 0 to 65,535 (unsigned Unicode) char grade = 'A';
boolean Logical 1 bit (JVM dependent) false Only true or false (not 0/1) boolean isPass = true;

Why does char take 2 Bytes in Java (instead of 1 Byte like C/C++)? C/C++ uses the ASCII character set (which only needs 1 byte for 256 English characters). Java uses the Unicode character set (16-bit), which takes 2 bytes so it can represent characters from all world languages (Hindi, Japanese, Arabic, English, etc.).


3. Variables and Constants in Java

A. Variables

A variable is a named memory location whose value can change during program execution.

  • Syntax: dataType variableName = value; (e.g., int marks = 85;)

There are 3 types of variables in Java based on their scope and lifetime:

Feature 1. Local Variable 2. Instance Variable (Non-Static Field) 3. Static Variable (Class Variable)
Where Declared? Inside a method, constructor, or {} block. Inside a class, but outside all methods (without static). Inside a class, outside methods, with the static keyword.
Memory Location Stack memory. Heap memory (inside each object). Method Area (only one copy shared by all objects).
Default Value? No default value! Must be initialized before use. Automatically initialized (0, 0.0, false, null). Automatically initialized (0, 0.0, false, null).
Example int sum = 0; inside calculate() int rollNo; (unique per student) static String college = "DAVV"; (same for all)

B. Constants (final Keyword)

A constant is a variable whose value is fixed and cannot be modified once assigned. In Java, constants are created using the final keyword (and by convention, written in UPPER_SNAKE_CASE).

final double PI = 3.14159;
final int MAX_MARKS = 100;
// MAX_MARKS = 120; // Compile-time Error: cannot assign a value to final variable

4. Type Conversion and Type Casting

Converting a value of one primitive data type into another is done in two ways:

  1. Implicit Type Conversion (Widening / Automatic): Happens automatically when converting a smaller data type into a larger data type (no data loss).
    • Order: byte → short → int → long → float → double
    int num = 500;
    double d = num; // Automatic widening: 500 becomes 500.0
  2. Explicit Type Casting (Narrowing): Must be done manually by the programmer using (targetType) when converting a larger data type into a smaller data type (may lose fractional part or overflow).
    double price = 99.75;
    int roundedPrice = (int) price; // Explicit narrowing: becomes 99 (.75 truncated)

5. Operators and Expressions in Java

An operator is a symbol that performs a specific operation on one, two, or three operands. An expression is a valid combination of variables, constants, and operators that evaluates to a single value (e.g., int total = (marks1 + marks2) * 2;).

Summary of All 8 Categories of Java Operators

Category Operators Description & Example
1. Arithmetic +, -, *, / (Quotient), % (Remainder) 10 % 3 gives 1; 10 / 3 gives 3.
2. Unary ++ (Increment), -- (Decrement), +, -, ! (NOT) Pre-increment ++a (increment first, then use) vs Post-increment a++ (use first, then increment).
3. Assignment =, +=, -=, *=, /=, %= x += 5; is shorthand for x = x + 5;.
4. Relational (Comparison) ==, !=, >, <, >=, <= Always returns a boolean (true or false). E.g., 5 >= 3 returns true.
5. Logical (Short-Circuit) && (Logical AND), || (Logical OR), ! (Logical NOT) Combines boolean conditions: (age >= 18 && hasVoterId).
6. Ternary (Conditional) condition ? expr1 : expr2 Shorthand for if-else: String res = (marks >= 40) ? "Pass" : "Fail";
7. Bitwise & Shift &, |, ^ (XOR), ~, << (Left Shift), >> (Right Shift), >>> (Unsigned Right Shift) Operates bit-by-bit on integer values. >>> is unique to Java (fills leftmost bits with 0).
8. instanceof Operator objectRef instanceof ClassName Checks whether an object belongs to a specific class at runtime (e.g., s1 instanceof Student returns true).

Example Program Demonstrating Variables, Constants, and Operators

public class OperatorsDemo {
    static String university = "DAVV Indore"; // Static variable

    public static void main(String[] args) {
        final int PASS_MARKS = 40; // Constant
        int theory = 55;           // Local variable
        int practical = 28;

        int total = theory + practical; // Arithmetic expression
        double percentage = (double) total; // Explicit type casting

        // Relational, Logical & Ternary Operator
        boolean isEligible = (theory >= 25) && (practical >= 15);
        String result = (total >= PASS_MARKS && isEligible) ? "PASSED" : "FAILED";

        System.out.println("University : " + university);
        System.out.println("Total Marks: " + total + " (" + percentage + "%)");
        System.out.println("Final Status: " + result);
    }
}

💡 Exam Preparation Tips for DAVV BCA

  • Remember the Unsigned Right Shift operator (>>>) and the instanceof operator—they exist in Java and are frequently asked in 2-mark objective/viva questions.
  • Never write that boolean takes 1 byte in Java; its logical size is 1 bit and it only accepts true or false (unlike C++, if(1) is a compile error in Java!).