Data Types
A data type defines the kind of data a variable can store, the operations that can be performed on it, and the amount of memory allocated.
Java Primitive Data Types
Data Type | Size | Range (Approx.) | Example |
Byte | 1 byte | -128 to 127 |
|
short | 2 bytes | -32,768 to 32,767 |
|
Int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
|
Long | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
|
float | 4 bytes | ~6–7 decimal digits |
|
double | 8 bytes | ~15 decimal digits |
|
Char | 2 bytes | Single Unicode character |
|
boolean | 1 bit* |
|
|
Java Non-Primitive Data Types
Type | Description | Example |
String | Sequence of characters |
|
Array | Collection of elements |
|
Class | Blueprint for objects |
|
Object | Instance of a class |
|
Interface | Abstract type for methods |
|
A variable in Java is a name given to a memory location where data is stored and manipulated during program execution. Each variable in Java has a data type, which defines:
- The size of memory to store the value.
- The range of possible values.
- The operations that can be performed on it.
Rules for creating a variable name in Java:
- The name can contain letters (A-Z, a-z), digits (0-9), and the underscore _ or dollar sign $ (although $ is rarely used).
- The first character must be a letter, underscore, or dollar sign (not a digit).
- Java is case-sensitive (Name and name are different).
- Java keywords (like int, class, public) cannot be used as variable names.
- Variable names should be meaningful and follow camelCase convention for readability.
Syntax:
type variableName = value;
Example:
String name = "Rohit";
int age = 25;
Constants
In Java, a constant is a variable whose value cannot be changed once assigned.
It is declared using the final keyword and is usually written in uppercase letters.
A constant must be given a value when declared, for example:final double PI = 3.14;.
Java Identifiers
In Java, an identifier is the name given to program elements such as classes, methods, variables, and interfaces. They are used to uniquely identify these components.
Rules for defining identifiers in Java:
- Allowed characters: letters (A-Z, a-z), digits (0-9), underscore _, and dollar sign $.
- Cannot start with a digit.
- Cannot contain whitespace.
- Cannot be a Java keyword (class, public, static, etc.).
- Java identifiers are case-sensitive.
- Can be of any length, but extremely long names are discouraged.
- Should not contain special characters like @, #, -, or spaces.
- Unicode characters are allowed (e.g., you can technically use non-English characters, but it’s not common).
Valid identifiers:
Name _name $price studentAge
Invalid identifiers:
123name // starts with a digit
student age // contains a space
class // keyword
Keywords in Java
Keywords in Java are reserved words that have a predefined meaning in the language. They cannot be used as identifiers (variable names, class names, etc.) because they are part of the Java syntax. There are total 53 keywords in java.
E.g: abstract, boolean, break, byte, case, catch, char, class, const,continue, default, do, double, else, enum, extends, final, finally, float,for, goto, if, import, int, interface, long, new, package, private, protected, public,return, short, static, switch, this, try, void, while
Rules:
- All keywords are lowercase.
- Cannot be used for variable, method, or class names.
- Have a fixed meaning and purpose defined by Java.
Comments
Post a Comment