byte Data Type
Size: 1 byte. Used for small integer values.
byte b = 10;
Learn Java identifiers, primitive data types, literals, arrays, variables, var-args, main() method and command line arguments with clear examples.
In Java, an identifier is the name given to program elements such as classes, methods, variables, and labels. Identifiers help developers uniquely recognize and access different parts of a Java program.
An identifier is a user-defined name used for:
class Test {
public static void main(String[] args) {
int x = 10;
}
}
In the above Java program:
Test is a class namemain is a method namex is a variable nameargs is also an identifier used as a parameter nameWhile creating identifiers in Java, you must follow these rules:
a-z and A-Z.
0-9, but they cannot start with a digit.
_ and dollar sign $.
Number, number, and NUMBER
are three different identifiers.
class, int, public,
static, and void cannot be used as identifiers.
int age = 25;
String studentName = "Rahul";
double total_amount = 5000.75;
int $salary = 30000;
int rollNumber1 = 101;
int 1number = 10; // Invalid: starts with a digit
int total-amount = 50; // Invalid: hyphen is not allowed
int class = 100; // Invalid: class is a reserved keyword
int @value = 20; // Invalid: @ is not allowed
Java treats uppercase and lowercase letters differently. Therefore, the following identifiers are considered different:
int number = 10;
int Number = 20;
int NUMBER = 30;
Although this is valid, using very similar names can reduce readability and may confuse developers.
studentName instead of s.totalMarks and calculateSalary().StudentDetails.$ in normal identifiers because it is mostly used by tools and generated code.a1, b2, or temp unless the context is very clear.No, reserved keywords cannot be used as identifiers in Java because they already have predefined meanings in the language.
Java identifiers are names used for classes, methods, variables, and labels. They can contain letters, digits, underscore, and dollar sign, but they cannot start with a digit. Java identifiers are case-sensitive, and reserved keywords cannot be used.
In Java, primitive data types are the most basic data types available. They are used to store simple values such as numbers, characters, and boolean values. Java provides 8 primitive data types that are predefined by the language.
Java primitive data types are broadly classified into the following categories:
byte, short, int, long
float, double
char (used to store single characters)
boolean (used to store logical values: true or false)
| Data Type | Size | Description | Example |
|---|---|---|---|
| byte | 1 byte | Stores small integer values | byte b = 10; |
| short | 2 bytes | Stores medium integer values | short s = 200; |
| int | 4 bytes | Default integer type | int x = 1000; |
| long | 8 bytes | Stores large integer values | long l = 100000L; |
| float | 4 bytes | Stores decimal values (less precision) | float f = 10.5f; |
| double | 8 bytes | Stores decimal values (high precision) | double d = 10.5; |
| char | 2 bytes | Stores a single Unicode character | char ch = 'A'; |
| boolean | JVM dependent | Stores true or false values | boolean flag = true; |
int data type always occupies 4 bytes,
regardless of the system or platform.
long data type occupies 8 bytes,
making it suitable for storing large numbers.
public class DataTypesExample {
public static void main(String[] args) {
int age = 25;
long population = 7800000000L;
float price = 99.99f;
double pi = 3.14159;
char grade = 'A';
boolean isJavaFun = true;
System.out.println(age);
System.out.println(population);
System.out.println(price);
System.out.println(pi);
System.out.println(grade);
System.out.println(isJavaFun);
}
}
int for general integer calculations.long when values exceed the range of int.double instead of float for better precision.boolean for conditions instead of integers like 0 or 1.char for single characters and String for text.Primitive data types in Java are essential for storing basic values efficiently. Understanding their types, sizes, and usage is crucial for writing optimized and platform-independent Java programs.
Java provides 8 primitive data types used to store simple values like numbers, characters, and boolean values. These are the foundation of Java programming.
Numeric data types are used to store numbers. They are divided into:
Used to store whole numbers (without decimals).
Size: 1 byte. Used for small integer values.
byte b = 10;
Size: 2 bytes. Used for medium-sized integers.
short s = 100;
Size: 4 bytes. Default integer type in Java. Fixed across platforms → ensures reliability.
int x = 1000;
Size: 8 bytes. Used for large integer values.
long l = 100000L;
Used to store decimal (real) numbers.
Size: 4 bytes. Used for decimal values (less precision).
float f = 10.5f;
Size: 8 bytes. Default for decimal values (high precision).
double d = 99.99;
The char data type stores a single character.
It uses Unicode (2 bytes) and supports international characters.
char ch = 'a';
char ch2 = '\u0061'; // Unicode for 'a'
The boolean data type stores logical values:
truefalseboolean flag = true;
boolean b = 0; // ❌ not allowed
boolean b = True; // ❌ case-sensitive
Java primitive data types include numeric types (integral and floating),
character type (char), and logical type (boolean).
Understanding these data types is essential for Java programming, interviews,
and SCJP certification.
In Java, a literal is a constant value assigned to a variable. Literals represent fixed values that cannot be changed during program execution.
A literal is a fixed value assigned directly to a variable in a Java program.
int x = 10; // 10 is a literal
Integer literals are used to represent whole numbers (without decimals).
100100x10int a = 10; // Decimal
int b = 010; // Octal
int c = 0x10; // Hexadecimal
By default, all integer literals are treated as int in Java.
To store large integer values, use L or l (recommended: uppercase L).
long l = 10L;
Floating-point literals are used to represent decimal numbers.
By default, floating-point literals are of type double.
double d = 123.45; // default double
float f = 123.45f; // float requires 'f' or 'F'
If you assign a decimal number to a float without f,
it will cause a compilation error.
Char literals represent a single character enclosed in single quotes.
char ch = 'a';
Java supports Unicode, so characters can also be represented using Unicode values.
char ch = '\u0061'; // Unicode for 'a'
Escape characters are special sequences used inside character and String literals.
| Escape Sequence | Description |
|---|---|
\n |
New line |
\t |
Tab space |
\' |
Single quote |
\" |
Double quote |
\\ |
Backslash |
char newLine = '\n';
char tab = '\t';
int x = 10;
Use integer literals for whole numbers and floating-point literals for decimals.
L for longf for floatchar ch = 'A';
Java literals are constant values assigned to variables. They include integer literals, floating-point literals, and character literals. Understanding literals helps in writing correct and efficient Java programs and is important for Java interviews and SCJP exams.
In Java, an array is a collection of elements of the same data type stored in contiguous memory locations. Arrays are widely used to store multiple values in a single variable instead of declaring separate variables.
Array declaration defines the reference variable that can hold the array object.
int[] a;
int a[];
Both syntaxes are valid, but int[] a is preferred as per Java coding standards.
After declaration, memory must be allocated using the new keyword.
a = new int[3];
This creates an array that can hold 3 integer values. By default:
Initialization means assigning values to the array at the time of creation.
int[] a = {10, 20, 30};
This creates and initializes the array in a single step.
byte, short, int, or char.
In Java, length and length() are different and used in different contexts.
| Feature | length | length() |
|---|---|---|
| Type | Property (variable) | Method (function) |
| Used For | Arrays | Strings |
| Syntax | a.length |
str.length() |
int[] a = {10, 20, 30};
System.out.println(a.length); // 3
String str = "Java";
System.out.println(str.length()); // 4
Important: Forgetting parentheses in length() will cause an error,
because it is a method, not a property.
int[] a;
a = new int[3];
a[0] = 10;
a[1] = 20;
a[2] = 30;
System.out.println(a.length);
Java arrays are objects used to store multiple values of the same type.
They require declaration, creation, and initialization. The length
property is used to find the size of arrays, while length() is used
for strings. Understanding arrays is essential for Java programming and coding interviews.
This guide explains types of variables in Java, important rules, var-args methods, main() method, and command line arguments in a simple and SEO-friendly format.
In Java, variables are classified into three types:
Instance variables are declared inside a class but outside any method, constructor, or block.
class Student {
int id; // instance variable
String name;
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(s1.id); // default value: 0
}
}
Static variables are declared using the static keyword.
They are shared among all objects of a class.
class Test {
static int count = 0;
Test() {
count++;
System.out.println(count);
}
public static void main(String[] args) {
new Test();
new Test();
new Test();
}
}
Local variables are declared inside methods, constructors, or blocks.
class Test {
public static void main(String[] args) {
int x = 10; // local variable
System.out.println(x);
}
}
Local variables must be initialized before use. Otherwise, the compiler will throw an error.
int x;
System.out.println(x); // ❌ Compile-time error
Var-args (variable arguments) were introduced in Java 1.5. They allow passing a variable number of arguments to a method.
... (ellipsis)class Test {
static void sum(int... x) {
for(int i : x) {
System.out.println(i);
}
}
public static void main(String[] args) {
sum(10, 20, 30);
}
}
The main() method is the entry point of any Java program.
The JVM starts execution from this method.
public static void main(String[] args)
The order of modifiers can change, but the method signature must remain valid.
static public void main(String[] args)
Command line arguments are passed to the Java program during execution.
They are stored in the args array.
java Test 10 20
class Test {
public static void main(String[] args) {
System.out.println(args[0]);
System.out.println(args[1]);
}
}
Output:
10
20
Java variables are categorized into instance, static, and local variables. Instance variables are object-specific, static variables are shared, and local variables must be initialized manually. Var-args methods allow flexible arguments, while the main() method is the entry point of Java programs. Command line arguments enable passing input during execution.