Experiment No.
1
Introduction to Java
Experiment No. 2
Write a Hello World Program and write its syntax ?
Theory:
Java is a high-level, object-oriented, platform-independent programming language. Every
Java program is written inside a class. The execution of a Java program starts from the main()
method.
Basic Structure of a Java Program:
Class Declaration
Main Method (Entry Point)
Statements inside main method
Key Keywords:
public – Access modifier
class – Keyword to declare a class
static – Method belongs to class, not object
void – No return value
[Link]() – Used to print output on console
Program:-
Output:-
Steps to Execute:
1. VS Code.
2. Write the above program.
3. Save the file as [Link] (name must match class name).
4. Open Command Prompt / Terminal.
5. Compile the program: javac [Link]
6. Run the program: java HelloWorld
Experiment – 3
Write a program to show scope of variable.
In programming, the "scope" of a variable essentially defines the region of a program where
that variable can be accessed or used. It's about visibility and accessibility.
Core Concept:
Visibility: Scope determines which parts of your code can "see" and interact with a
particular variable.
Accessibility: It dictates where in the program you're allowed to read or modify the
variable's value.
Why Scope Matters:
Preventing Conflicts: Scope helps avoid naming conflicts. You can use the same
variable name in different parts of your program without them interfering with each
other.
Code Organization: It promotes organized and modular code by limiting the reach of
variables, making it easier to understand and maintain.
Memory Management: In some languages, scope is related to how memory is
allocated and deallocated for variables.
Common Scope Types:
Global Scope: Variables declared in the global scope are accessible from anywhere in
the program.
Local Scope: Variables declared within a specific block of code (like a function or
loop) are only accessible within that block.
In essence: Scope is a rule that governs the lifespan and accessibility of variables within a
program in any programming language.
Types of Scope of Variable
Local Static Instance
Local Variable
In Java, a local variable is a variable that is declared within a method, constructor, or block
of code.
Key Characteristics:
Scope: A local variable’s scope is limited to the block of code in which it is declared.
This means it can only be accessed within that specific method, constructor, or block.
Lifetime: A local variable’s lifetime is tied to the execution of the block where it’s
declared. It comes into existence when the block is entered and is destroyed when the
block is exited.
Initialization: Unlike instance and class variables, local variables do not have default
values. Therefore, you must explicitly initialize a local variable before you can use it.
Location of Declaration: They are declared inside of methods, constructors, or inside
of code blocks that are contained within curly braces {}.
Syntax:
void function() {
int x; // Local variable
Static Variable (Class-Level Variable)
In Java, a static variable is a variable that belongs to the class itself, rather than to any
specific instance (object) of that class.
Key Characteristics:
Class-Level Variable: A static variable is associated with the class, not with
individual objects. This means there’s only one copy of the static variable, regardless
of how many objects of the class are created.
Shared Among Instances: All instances of the class share the same static variable. If
one object changes the value of a static variable, that change is visible to all other
objects of the same class.
Memory Allocation: Memory for a static variable is allocated only once, when the
class is loaded into memory.
Access: Static variables can be accessed directly using the class name (e.g.,
[Link]). They can also be accessed through an object of the class,
but this is generally discouraged because it can obscure the fact that the variable is
static.
Syntax:
static int x;
Instance Variable
In Java, an "instance variable" is a fundamental concept in object-oriented programming.
What Instance Variables Are:
Belong to Objects: Instance variables are variables that are associated with individual
objects (instances) of a class. Each object gets its own separate copy of the instance
variables.
Defined Within a Class: They are declared within a class, but outside of any
methods, constructors, or blocks.
Store Object State: Instance variables hold the data that defines the state of an
object. Their values can vary from one object to another.
Syntax:-
int c = 30;
Program
Output:-
Experiment no. 4
Write a Java Program for Simple Calculator
Program:-
Output-
Experiment no. 5
Implicit and Explicit Type Casting
Write a program to show typecastin in java
Theory
1. Implicit Type Casting (Widening Conversion)
Done automatically by the Java compiler.
Converts a smaller data type to a larger data type.
No data loss.
Order: byte → short → int → long → float → double
2. Explicit Type Casting (Narrowing Conversion)
Done manually by the programmer.
Converts a larger data type to a smaller data type.
May result in data loss or precision loss.
Syntax: (targetType) value
Program 1: Implicit Type Casting
Java
// Experiment No. 4 - Implicit and Explicit Type Casting
// Program 1: Implicit Type Casting
public class ImplicitCasting {
public static void main(String[] args) {
[Link]("=== Implicit Type Casting (Widening) ===\n");
byte b = 50;
short s = b; // byte → short
int i = s; // short → int
long l = i; // int → long
float f = l; // long → float
double d = f; // float → double
[Link]("byte value: " + b);
[Link]("short value: " + s);
[Link]("int value: " + i);
[Link]("long value: " + l);
[Link]("float value: " + f);
[Link]("double value: " + d);
}
}
Program 2: Explicit Type Casting
Java
// Program 2: Explicit Type Casting (Narrowing)
public class ExplicitCasting {
public static void main(String[] args) {
[Link]("=== Explicit Type Casting (Narrowing) ===\n");
double d = 125.78;
float f = (float) d; // double → float
long l = (long) f; // float → long (fractional part lost)
int i = (int) l; // long → int
short s = (short) i; // int → short
byte b = (byte) s; // short → byte (may cause overflow)
[Link]("double value: " + d);
[Link]("float value: " + f);
[Link]("long value: " + l);
[Link]("int value: " + i);
[Link]("short value: " + s);
[Link]("byte value: " + b);
}
}
Combined Program (Recommended for Lab)