0% found this document useful (0 votes)
9 views4 pages

Java 2

A variable is a named memory location for storing data, enabling value storage, reuse, and dynamic programming. In Java, variables must follow specific naming rules, such as starting with a letter and being case-sensitive. Data types in Java are categorized into primitive types, which include byte, short, int, long, float, double, char, and boolean, and non-primitive types, which include objects like strings and arrays.

Uploaded by

rojasri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Java 2

A variable is a named memory location for storing data, enabling value storage, reuse, and dynamic programming. In Java, variables must follow specific naming rules, such as starting with a letter and being case-sensitive. Data types in Java are categorized into primitive types, which include byte, short, int, long, float, double, char, and boolean, and non-primitive types, which include objects like strings and arrays.

Uploaded by

rojasri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. What is a Variable?

A variable is a named memory location used to store data during program execution.

Why variables are needed?

 To store values

 To reuse data

 To make programs dynamic

Example:

int age = 20;

 int → data type

 age → variable name

 20 → value

2. Rules for Naming Variables (Identifiers)

In Java, variable names are called identifiers.

Rules:

1. Must begin with a letter, _, or $

2. Cannot begin with a digit

3. Cannot use keywords (int, class, public, etc.)

4. No spaces allowed

5. Case-sensitive

Valid identifiers

int age;

int _count;

int $price;

int studentMarks;

Invalid identifiers

int 2num; // starts with digit

int total marks; // space not allowed

int class; // keyword

👉 Teaching tip: Emphasize camelCase for variable names.


3. Declaration, Initialization & Assignment

Declaration

int x;

Initialization

x = 10;

Declaration + Initialization

int x = 10;

4. What is a Data Type?

A data type specifies:

 What kind of data a variable can store

 How much memory it occupies

 What operations can be performed

5. Types of Data Types in Java

Java has two main categories:

1. Primitive Data Types

2. Non-Primitive (Reference) Data Types

6. Primitive Data Types

Primitive data types store simple values.

List of Primitive Data Types

Data Type Size Default Value Example

byte 1 byte 0 byte b = 10;

short 2 bytes 0 short s = 100;

int 4 bytes 0 int i = 1000;

long 8 bytes 0L long l = 100000L;

float 4 bytes 0.0f float f = 10.5f;

double 8 bytes 0.0 double d = 99.99;


Data Type Size Default Value Example

char 2 bytes '\u0000' char c = 'A';

boolean 1 bit false boolean flag = true;

7. Integer Data Types

Used to store whole numbers.

byte b = 100;

short s = 20000;

int i = 100000;

long l = 999999999L;

👉 int is the most commonly used integer type.

8. Floating-Point Data Types

Used to store decimal values.

float f = 10.25f;

double d = 10.25;

⚠️ Important Note:

 float must end with f

 double is default for decimals

9. Character Data Type (char)

 Stores single character

 Uses single quotes

 Internally stores Unicode value

char ch = 'A';

char num = '1';

10. Boolean Data Type

 Stores only true or false

 Used in decision-making
boolean isJavaEasy = true;

11. Non-Primitive (Reference) Data Types

Used to store objects and complex data.

Examples:

 String

 Arrays

 Classes

 Interfaces

1432 789

You might also like