Basic Java
Basic Java
↓
Java is a high-level, object-oriented, platform-
Bytecode (.class)
independent programming language developed by
Oracle Corporation (originally by Sun Microsystems).
↓
High-Level Language
Interview Meaning Its slogan is:
↓
Strong Type Checking
args[0] = "Rahul";
Features of Java args[1] = "20";
args[2] = "Hyderabad";
Then
public → Accessible from anywhere. You need the whole kitchen to make tea, but only the
class → Blueprint for creating objects. stove and burner to heat it.
Main → Class name (must match the file name
[Link]). Similarly, to develop Java programs, you need the
JDK. To run Java programs, you only need the JRE
(which contains the JVM).
public static void main(String[] args)
[Link]("Hello, World!");
Output:
Hello, World!
Feature JVM JRE JDK Using int also works, but byte uses less memory.
Full form Java Java Runtime Java
Virtual Environment Development In real projects, byte is used less often.
Machine Kit
Purpose Runs Java
bytecode
Provides the
runtime
Develops and
runs Java
2. short
environment programs
What is it?
Contains ❌ No ❌ No ✅ Yes
short stores medium-sized integer values.
compiler
Memory
(javac)? 2 bytes = 16 bits
Contains — ✅ Yes ✅ Yes Range
JVM? -32,768 to 32,767
Contains ❌ No ✅ Yes ✅ Yes
libraries?
Used by End users End users and Developers What developers actually use
and developers Data Used in Real When
developers Type Projects?
byte ❌ Rarely Binary data, files, networking
short ❌ Very rarely Special cases, memory-
Types of Data Types constrained systems
int ✅ Very Most whole numbers
Java data types are divided into two categories: frequently
long ✅ Frequently Very large numbers (IDs,
Data Types timestamps, large counts)
│
├── Primitive Data Types (8)
│ ├── byte Java Data Types
│ ├── short
│ ├── int There are 8 primitive data types and many non-
│ ├── long primitive (reference) types.
│ ├── float
│ ├── double 1. byte
│ ├── char Stores small integers.
byte age = 100; Memory: 8 bytes
Memory: 1 byte Real-time usage: ✅ Very common
Range: -128 to 127 Examples:
Real-time usage: ❌ Very rare double price = 99.99;
double percentage = 87.5;
2. short
Stores medium-sized integers. 7. char
short marks = 30000; Stores a single character.
Memory: 2 bytes char grade = 'A';
Range: -32,768 to 32,767 char gender = 'M';
Real-time usage: ❌ Rare Memory: 2 bytes
Real-time usage: ⚠️Occasional
Usually used when only one character is needed.
3. int ⭐ (Most Used)
Stores whole numbers.
int age = 25; 8. boolean
int salary = 50000; Stores only two values.
int quantity = 10; boolean isLoggedIn = true;
Memory: 4 bytes boolean isAdmin = false;
Range: -2,147,483,648 to 2,147,483,647 Possible values:
Real-time usage: ✅ Very common true
Examples: false
int age = 22; Real-time usage: ✅ Very common
int marks = 95; Examples:
int quantity = 5; boolean isActive = true;
boolean isDeleted = false;
4. long
Stores very large whole numbers. float vs double
long mobileNumber = 9876543210L;
long population = 8000000000L; Float double
Memory: 8 bytes 4 bytes 8 bytes
Range: Less precision More precision
-9,223,372,036,854,775,808 About 6–7 decimal digits About 15–16 decimal digits
to Needs f suffix No suffix needed
9,223,372,036,854,775,807
Used less often Used most often
Real-time usage: ✅ Common for large values
For small values also mostly we use double..
Examples:
long userId = 1234567890123L; Why?
long timestamp = [Link]();
Because:
5. float
1. double is the default decimal type
Stores decimal numbers.
When you write:
float price = 99.99f;
double value = 10.5;
Memory: 4 bytes
it works directly.
Real-time usage: ❌ Rare
With float, you must write:
Most developers prefer double.
float value = 10.5f;
Without the f:
float value = 10.5;
6. double ⭐ (Most Used for Decimals)
❌ Compilation error.
Stores decimal numbers with higher precision.
double salary = 55000.75; 2. Better precision
double pi = 3.14159; Even if the number is small:
double percentage = 87.5; <= Less than or equal a <= b true
double stores it more accurately.
1. Arithmetic Operators
5. Unary Operators
Operator Name Example
+ Addition 10 + 5 Assume:
- Subtraction 10 - 5
* Multiplication 10 * 5 int a = 10;
/ Division 10 / 5 Operator Meaning Example Output
% Modulus (Remainder) 10 % 3 + Unary Plus +a 10
- Unary Minus -a -10
++ ++a 11
2. Assignment Operators --
Increment
Decrement --a 9
! Logical !true false
Assume: NOT
int a = 10;
Operator Meaning Equivalent Result
=
+=
Assign a
a
=
=
10
a + 5
10
15
6. Increment & Decrement
Add and assign
-= Subtract and assign a = a - 5 5
Assume:
*= Multiply and assign a = a * 5 50
/= Divide and assign a = a / 5 2 int a = 10;
%= Modulus and a = a % 3 1 Operator Meaning Example Output
assign ++a Pre ++a 11
Increment
a++ Post a++ Uses 10, then
Increment becomes 11
3. Relational --a Pre --a 9
int a = 10;
int b = 20;
7. Ternary Operator
Operator Meaning Example Outpu
Syntax
t condition ? value1 : value2;
== Equal to a == b false
!= Not equal a != b true
Example:
> Greater than a > b false
< Less than a < b true int age = 20;
>= Greater than or equal a >= b false
String result = age >= 18 ? "Adult" : ├── Uses one operand
"Minor"; │ ↓
│ Unary Operator
│
. Bitwise Operators └── Performs logical NOT
↓
int a = 8;
Operator Meaning Example Output
<< Left Shift a << 1 16
>> Right Shift a >> 1 4
>>> Unsigned a >>> Depends on the binary
Right Shift 1 representation (mainly
useful with negative Different Data Types with
numbers)
Scanner (for Learning)
[Link](average);
Type Casting means:
Output
47.5
Converting a value from one data type to another. Without casting:
double average = totalMarks / subjects;
Example: Output
47.0
int → double
double → int
char → int
long → int
Think of it like pouring water into different-sized 1. Implicit Type Casting (Automatic)
containers. Also called Widening.
Java automatically converts a smaller data type into a larger
Small Glass (int) → Large Bottle (double) ✅ data type.
Easy byte
Large Bottle (double) → Small Glass (int) ❗ ↓
Some water may be lost short
↓
int
Types of Type Casting long
↓
↓
There are 2 types: float
↓
double
1. Implicit Type Casting (Widening / Automatic)
No data is lost.
2. Explicit Type Casting (Narrowing / Manual)
Example 1
public class Main {
Term Meaning public static void main(String[] args) {
Type Any conversion from one type to
Conversion another int a = 100;
Type A conversion where you explicitly
Casting specify the target type using (type) double b = a;
Are type casting and type conversion the same?
Type conversion is the general process of changing one data [Link](a);
type to another. It includes both automatic (implicit) [Link](b);
conversion and manual (explicit) conversion. Type casting }
usually refers specifically to the manual conversion where }
we use (type). Output
Why do we use Type Casting in real-time applications? 100
Answer: 100.0
We use type casting when different data types need to work
together. It helps convert data from one type to another Example 2
safely, especially while performing calculations, processing char ch = 'A';
user input, handling database values, reading files, or
working with APIs. Java performs automatic conversion int value = ch;
when it's safe (widening), and requires explicit casting when
there is a possibility of data loss (narrowing). [Link](value);
Output
65
Real-Time Use Cases
Because the ASCII/Unicode value of 'A' is 65.
1. Calculations (⭐⭐⭐⭐⭐ Most Common)
Example 3 [Link](value);
int num = 25; Output
-1294967296
long value = num; Why?
Because 3000000000 is larger than the maximum int
[Link](value); value (2147483647).
Output The value overflows, resulting in an incorrect number.
25
Integer Division Example
Why does Java allow this automatically? int a = 10;
Because there is no risk of losing data. int b = 3;
int
100
double result = a / b;
↓
double
100.0 [Link](result);
Nothing is lost. Output
3.0
Why?
2. Explicit Type Casting (Manual)
Because a / b is evaluated first as integer division, giving 3,
Also called Narrowing.
and then 3 is assigned to a double as 3.0.
Java converts a larger type into a smaller type.
Since data may be lost, you must tell Java explicitly.
Correct Way
Syntax:
int a = 10;
(smallerDataType) value
int b = 3;
Example 1
double result = (double) a / b;
double price = 99.99;
[Link](result);
int value = (int) price;
Output
3.3333333333333335
[Link](value);
Output
99
The decimal part is removed (truncated).
Return Values
Another example
Return Value Meaning int[] a = {5,10,2};
0 Both arrays are equal int[] b = {5,9,100};
Negative (< 0) First array is smaller
Positive (> 0) First array is larger [Link]([Link](a,b));
[Link](a, b); Output
It compares element by element from left to right. 1
It stops at the first difference. Because
10 > 9 Instead of writing
Java doesn't even compare 2 and 100. String name = "Rahul";
It already found the first difference. every time,
we can simply run
Different Length Arrays java Main Rahul
Example 1 Tomorrow
int[] a = {1,2}; java Main Ravi
int[] b = {1,2,3}; The same program works for different inputs.
Syntax
public static void main(String[] args)
Interview Point
Here,
String[] args toString() does not mean:
stores the command-line arguments.
"Convert an object into a nice String."
Example
Suppose you run your program from the terminal: It simply means:
java Main Rahul 21 Hyderabad
The JVM automatically creates: "Return some String representation of this object."
args[0] = "Rahul";
args[1] = "21"; The quality of that representation depends on the class:
args[2] = "Hyderabad";
Now your program can use these values. [Link]() → Default representation
public class Main { ([I@36baf30c)
public static void main(String[] args) { [Link]() → Human-readable
representation ([10, 20, 30])
[Link](args[0]);
[Link](args[1]); Arrays are also objects in Java. When we
[Link](args[2]); print an array reference,
[Link]() calls the array's
}
} toString() method. Since arrays do not
Output provide a human-readable implementation of
Rahul toString(), the default representation (like
21 [I@36baf30c) is printed. To display the array
Hyderabad
elements, Java provides the utility method
[Link](), which returns a readable
Why do we use Command Line Arguments?
string containing all the elements.
They allow us to pass input to the program before it starts,
without changing the source code.
🔥 One line to remember
Example:
Every array is an object, but not every object is an array. volatile
int[] arr = {10,20,30}; transient
native
[Link](arr);
strictfp
Internally:
Output:
[I@36baf30c
[Link]([Link](arr));
Execution:
ACCESS SPECIFIERS(ACCESS
MODIFIERS)