Java Course
I. What is a Literal?
A literal is a fixed value that is directly written into the code.
Java supports several types of literals:
- Integer literals: e.g., 10, 0xA (hexadecimal), 012 (octal)
- Floating-point literals: e.g., 3.14, 2.0e5
- Character literals: e.g., 'a', '\n'
- String literals: e.g., "Hello, World!"
- Boolean literals: true, false
II. What is a Variable?
Variables are named storage locations in memory that hold data. They need to be declared before use.
For example,
int age;
III. What is an Assignment?
Assigning a value to a variable is done using the = operator.
For example,
int age=39;
IV. What is a Data type?
Data types specify the different sizes and values that can be stored in the variable.
There are two types of data types in Java:
- Primitive : boolean, char, byte, short, int, long, float and double.
- Non-primitive : Classes, Interfaces, and Arrays.

Primitive Data Types
In Java language, primitive data types are the building blocks of data manipulation.

Let's see some example,
package com.praveen.datatypes; public class DataTypesDemo { public static void main(String[] args) { // byte a =130; // compiler error as byte range is -128 to 127 byte a = -50; short b = 150; int c = 10000; long d = 100000; char e = 'A'; // float f = 1.23; // compiler error as it needs to differntiate between float and double float f = 1.23f; double g = 12345.6789; byte h = 20; // byte result = a + h; // compiler error as sum of byte may be out of range int result = a + h; } }
V. Type Casting
-
Conversion of data from one type to another type is called Type Casting.
-
Java supports Primitive as well as Non-primitive casting.
-
Type Casting can be implicit or explicit.
-
Implicit casting means it happens automatically.
For Primitive Data Type, lower -> higher for example, byte -> int
For Non-primitive Data Type, child class -> parent class
-
Explicit casting means it needs to be manually done by the developer.
For Primitive Data Type, higher -> lower for example, int -> byte
For Non-primitive Data Type, parent class -> child class
Let's see an example for Primitive Implicit casting,
package com.praveen.datatypes; public class ImplicitCastDemo { public static void main(String[] args) { byte b = 100; int i = b; System.out.println(i); // 100 long l = i; System.out.println(l); // 100 char ch = 'A'; int x = ch; System.out.println(x); // 65 } }
Let's see an example for Primitive Explicit casting,
package com.praveen.datatypes; public class ExplicitCastDemo { public static void main(String[] args) { int i = 100; byte b = (byte) i; System.out.println(b); // 100 int x = 97; char ch = (char) x; System.out.println(ch); // a int y = 130; byte z = (byte) y; System.out.println(z); // -126, it is resulting as -126 as byte range is from -128 to 127 and if its beyond range i.e.. 127 then it goes to negative value. int m = 612; byte n = (byte) m; char o =(char) n; System.out.println(o); // d, it is resulting as d as 256*2 = 512 and 612-512 = 100 so a is 97 that means 100 is d. } }
VI. Using a Variable That Is Uninitialized and Unassigned
- Local Variables: In Java, local variables (variables declared inside methods) must be initialized before use. Using an uninitialized local variable will result in a compile-time error.
For example,
public class Test { public static void main(String[] args) { int x; // Declaration // System.out.println(x); // Error: variable x might not have been initialized x = 10; // Initialization System.out.println(x); // Works fine now } }
- Instance and Class Variables: Instance variables (fields of a class) and class variables (static fields) are automatically initialized to default values if not explicitly initialized:
- byte, short, int, long:
0 - float, double:
0.0 - char:
'\u0000' (null character) - boolean:
false - Object references:
null
- byte, short, int, long:
For example,
public class Test { int instanceVar; // Default value is 0 for int public void print() { System.out.println(instanceVar); // Outputs 0 } }
VII. Local (Stack, Automatic) Primitives and Objects
Local Variables: They are allocated on the stack and their lifetime is limited to the method or block in which they are defined. They are automatically destroyed when the method or block finishes executing.
- Primitives: For local variables, primitives are directly stored on the stack.
For example,
public class Test { public void method() { int num = 5; // Local primitive variable // num is on the stack and exists only within this method } }
- Objects: Objects are created on the heap, but references to these objects are stored on the stack.
For example,
public class Test { public void method() { // Creating a new String object and assigning its reference to the local variable 'text' String text = "Hello"; // Local variable 'text' holds a reference to the String object on the heap // The object "Hello" itself is stored on the heap, but 'text' (the reference) is on the stack System.out.println(text); // Outputs "Hello" // Changing the reference to a new object text = "World"; // Now 'text' references a new String object "World" // The original "Hello" String object still exists on the heap as strings are immutable, // but the local variable 'text' now references the new "World" String object System.out.println(text); // Outputs "World" } public static void main(String[] args) { Test test = new Test(); // create an object for Test class test.method(); } }
You can know more about Stack and Heap in JVM Architecture topic.
VIII. Passing Variables into Methods
When passing variables to methods, Java uses pass-by-value semantics:
- Primitive Types: The method receives a copy of the value.
- Object References: The method receives a copy of the reference (i.e., the address of the object). Although the reference itself is passed by value, changes to the object's state are reflected in the original object because both the caller and the callee refer to the same object.
IX. Does Java Use Pass-By-Value Semantics?
Yes, Java uses pass-by-value semantics:
- Primitive Data: The actual value is passed.
- Object References: The value of the reference (address of the object) is passed, not the actual object. Modifying the object's state affects the original object.
X. Passing Primitive Variables
When you pass a primitive variable to a method, a copy of the variable's value is made. Any changes to this copy do not affect the original variable.
For example,
public void modifyValue(int x) { x = x + 10; } int number = 5; modifyValue(number); // number is still 5