0% found this document useful (0 votes)
2 views9 pages

Unit 1 Sample Programs

adwestrdytfuiykgulih;o

Uploaded by

bhuviprasanth26
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Unit 1 Sample Programs

adwestrdytfuiykgulih;o

Uploaded by

bhuviprasanth26
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Local Variable
A variable declared inside a method, constructor, or block.

public class Demo {


public void show() {
int x = 10; // Local variable
[Link](x);
}

public static void main(String[] args) {


Demo d = new Demo();
[Link]();
}
}

Output:

10

2. Instance Variable
A variable declared inside a class but outside any method. Each object gets its own copy.

public class Student {


String name = "John"; // Instance variable

public static void main(String[] args) {


Student s1 = new Student();
Student s2 = new Student();

[Link]([Link]);
[Link]([Link]);
}
}

Output:

John
John

3. Static Variable
A variable declared with the static keyword. It is shared by all objects of the class.

public class Student {


static String college = "ABC College"; // Static variable

public static void main(String[] args) {


Student s1 = new Student();
Student s2 = new Student();
[Link]([Link]);
[Link]([Link]);
}
}

Output:

ABC College
ABC College

Comparison Table

Variable
Declaration Place Scope Memory
Type
Local Only within the Created when method
Inside a method/block
Variable method is called
Instance Inside class, outside Accessible through Separate copy for each
Variable methods objects object
Static Inside class with static Single copy for entire
Shared by all objects
Variable keyword class

Short Definitions (Exam Point of View)

● Local Variable: Declared inside a method and accessible only within that method.
● Instance Variable: Declared inside a class but outside methods; each object has its
own copy.
● Static Variable: Declared using the static keyword and shared among all objects of
the class.

1. Type Conversion (Implicit/Widening)


Java automatically converts a smaller data type into a larger data type.

public class TypeConversion {

Output:

Integer value: 100


Converted double value: 100.0
2. Type Casting (Explicit/Narrowing)
Java requires manual casting when converting a larger data type into a smaller data type.

public class TypeCasting {

Output:

Double value: 99.99


Casted integer value: 99

Short Definitions

● Type Conversion: Automatic conversion of a smaller data type to a larger data type
(e.g., int double).
● Type Casting: Manual conversion of a larger data type to a smaller data type using
the cast operator (e.g., double int).

Java Console Input: Buffered Input (BufferedReader)


Definition
Buffered input in Java is a method of reading data from the keyboard efficiently using the
BufferedReader class. It reads characters from an input stream and stores them in a buffer,
reducing the number of I/O operations and improving performance.

Why Use BufferedReader?


● Provides faster input than simple character-by-character reading.

● Reads an entire line of text at once.

● Efficient for handling large amounts of input.

● Uses buffering to improve performance.


Classes Used
● InputStreamReader – Converts byte streams into character streams.

● BufferedReader – Reads text efficiently using a buffer.

Syntax
BufferedReader br = new BufferedReader(
new InputStreamReader([Link]));

Example Program
import [Link].*;

public class BufferedInputDemo {


public static void main(String[] args) throws IOException {

BufferedReader br =
new BufferedReader(new InputStreamReader([Link]));

[Link]("Enter your name: ");


String name = [Link]();

[Link]("Hello, " + name);


}
}
Output
Enter your name: John
Hello, John

Reading an Integer
import [Link].*;

public class BufferedInputNumber {


public static void main(String[] args) throws IOException {

BufferedReader br =
new BufferedReader(new InputStreamReader([Link]));

[Link]("Enter a number: ");


int num = [Link]([Link]());

[Link]("Number = " + num);


}
}

Advantages
1. Fast and efficient input handling.
2. Reads complete lines of text.
3. Suitable for large input data.
4. Reduces I/O overhead using buffering.

Disadvantages
1. Requires exception handling (IOException).
2. Data entered as text must be converted to numeric types manually.

Arithmetic Operators
Definition
Arithmetic operators are used to perform mathematical operations such as addition,
subtraction, multiplication, division, and modulus.

Example
class ArithmeticDemo {
public static void main(String[] args) {
int a = 10, b = 5;
[Link](a + b);
}
}

2. Increment & Decrement Operators


Definition
Increment (++) increases a variable's value by 1, while Decrement (--) decreases a variable's
value by 1.

Example
class IncDecDemo {
public static void main(String[] args) {
int a = 5;
a++;
[Link](a);
}
}

3. Relational Operators
Definition
Relational operators compare two values and return true or false.

Example
class RelationalDemo {
public static void main(String[] args) {
int a = 10, b = 20;
[Link](a < b);
}
}

4. Bitwise Operators
Definition
Bitwise operators perform operations on individual bits of integer values.

Example
class BitwiseDemo {
public static void main(String[] args) {
int a = 5, b = 3;
[Link](a & b);
}
}

5 = 0101
3 = 0011 ---- & 0001 = 1

o/p
1

5. Logical Operators
Definition
Logical operators combine or invert boolean expressions.

Example
class LogicalDemo {
public static void main(String[] args) {
boolean a = true, b = false;
[Link](a && b);
}
}
How it works:
The && (logical AND) operator returns true only if both operands are true.
Here, a is true but b is false, so the result is false.

Output:
false

6. Assignment Operator
Definition
Assignment operators are used to assign values to variables.

Example
class AssignmentDemo {
public static void main(String[] args) {
int a = 10;
a += 5;
[Link](a);
}
}
How it works:
a += 5 is shorthand for a = a + 5, so a = 10 + 5 = 15.
Output:
15

7. Conditional (Ternary) Operator


Definition
The conditional operator (?:) selects one of two values based on a condition.

Example
class ConditionalDemo {
public static void main(String[] args) {
int a = 10, b = 20;
int max = (a > b) ? a : b;
[Link](max);
}
}

How it works:
The condition a > b checks if 10 > 20, which is false. Since the condition
is false, the ternary operator returns the value after the colon, which is
b (20).
Output:
20

One-Line Definitions for Exams


● Arithmetic Operators: Perform mathematical calculations.

● Increment & Decrement Operators: Increase or decrease a value by 1.

● Relational Operators: Compare two values and return a boolean result.

● Bitwise Operators: Operate on individual bits of data.

● Logical Operators: Combine or negate boolean expressions.

● Assignment Operator: Assign values to variables.

● Conditional Operator: Chooses one value from two based on a condition.


Definition

A static variable is a variable that belongs to the class itself rather than to any individual object
created from that class. It is declared using the static keyword, and only one copy of it exists in
memory, no matter how many objects of the class are created. All objects of the class share this
same copy — so if one object changes its value, the change is reflected for all other objects too.

Key points:

 Declared with the static keyword (e.g., static int count;)

 Memory is allocated once, when the class is loaded — not each time an object is created

 Shared across all instances of the class

 Can be accessed using the class name ([Link]) without needing to create
an object

 Commonly used for things like counters, constants, or shared configuration values

Syntax:

java

static dataType variableName;

Example:

java

static int count = 0;


example programs

class StaticDemo {

static int count = 0; // static variable - shared across all objects

StaticDemo() {

count++; // increment count every time an object is created

[Link]("Object number: " + count);

public static void main(String[] args) {

StaticDemo obj1 = new StaticDemo();

StaticDemo obj2 = new StaticDemo();

StaticDemo obj3 = new StaticDemo();

[Link]("Total objects created: " + count);

Output

Object number: 1

Object number: 2

Object number: 3

Total objects created: 3

You might also like