0% found this document useful (0 votes)
10 views13 pages

Java Programming Concepts Explained

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

Java Programming Concepts Explained

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

Date: - 28/07/25

Ques 1- Write a Java program to demonstrate the concept of class and object.

// Class definition
class Car {
// Data members (variables)
String color;
int speed;

// Method
void displayInfo() {
[Link]("Car color: " + color);
[Link]("Car speed: " + speed + " km/h");
}
}

// Main class to run the program


public class Main {
public static void main(String[] args) {
// Creating an object of class Car
Car myCar = new Car();

// Assigning values to the object's variables


[Link] = "Red";
[Link] = 120;

// Calling method using object


[Link]();
}
}

Output:-
Car color: Red
Car speed: 120 km/h
Ques 2: - Write a Java program to demonstrate the difference between
instance variables and local variables.
// Class to demonstrate instance and local variables
class Student {
// Instance variables (declared inside class, but outside methods)
String name;
int age;

// Method using local variables


void displayDetails() {
// Local variable (declared inside the method)
String college = "ABC College";

// Printing instance variables


[Link]("Student Name: " + name);
[Link]("Student Age: " + age);

// Printing local variable


[Link]("College Name: " + college);
}
}

public class Main {


public static void main(String[] args) {
// Creating object
Student s1 = new Student();

// Assigning values to instance variables


[Link] = "John";
[Link] = 21;

// Calling method
[Link]();
}
}

Output:-
Student Name: John
Student Age: 21
College Name: ABC College
Ques 3: - Write a Java program to demonstrate the use of all 8 primitive data
types in Java.

public class DataTypesExample {


public static void main(String[] args) {
// Integer types
byte b = 100; // 1 byte, range: -128 to 127
short s = 10000; // 2 bytes, range: -32,768 to 32,767
int i = 100000; // 4 bytes
long l = 10000000000L; // 8 bytes (L is required)

// Floating-point types
float f = 5.75f; // 4 bytes (f is required)
double d = 19.99; // 8 bytes

// Character type
char c = 'A'; // 2 bytes, stores a single character

// Boolean type
boolean isJavaFun = true; // 1 bit (true or false)

// Printing all values


[Link]("byte: " + b);
[Link]("short: " + s);
[Link]("int: " + i);
[Link]("long: " + l);
[Link]("float: " + f);
[Link]("double: " + d);
[Link]("char: " + c);
[Link]("boolean: " + isJavaFun);
}
}

Output:-
byte: 100
short: 10000
int: 100000
long: 10000000000
float: 5.75
double: 19.99
char: A
boolean: true
Ques 4 Write a Java program to demonstrate the use of all major types of
operators in Java, including Arithmetic, Relational, Logical, Bitwise,
Assignment, Unary, and Ternary operators.

public class OperatorsExample {


public static void main(String[] args) {
// 1. Arithmetic Operators
int a = 10, b = 5;
[Link]("Arithmetic Operators:");
[Link]("a + b = " + (a + b)); // 15
[Link]("a - b = " + (a - b)); // 5
[Link]("a * b = " + (a * b)); // 50
[Link]("a / b = " + (a / b)); // 2
[Link]("a % b = " + (a % b)); // 0

// 2. Relational (Comparison) Operators


[Link]("\nRelational Operators:");
[Link]("a == b: " + (a == b)); // false
[Link]("a != b: " + (a != b)); // true
[Link]("a > b: " + (a > b)); // true
[Link]("a < b: " + (a < b)); // false
[Link]("a >= b: " + (a >= b)); // true
[Link]("a <= b: " + (a <= b)); // false

// 3. Logical Operators
boolean x = true, y = false;
[Link]("\nLogical Operators:");
[Link]("x && y: " + (x && y)); // false
[Link]("x || y: " + (x || y)); // true
[Link]("!x: " + (!x)); // false

// 4. Bitwise Operators
[Link]("\nBitwise Operators:");
[Link]("a & b = " + (a & b)); // 0
[Link]("a | b = " + (a | b)); // 15
[Link]("a ^ b = " + (a ^ b)); // 15
[Link]("~a = " + (~a)); // -11

// 5. Assignment Operators
int c = 20;
[Link]("\nAssignment Operators:");
c += 5; // c = c + 5
[Link]("c += 5: " + c); // 25
c *= 2; // c = c * 2
[Link]("c *= 2: " + c); // 50

// 6. Unary Operators
[Link]("\nUnary Operators:");
int p = 5;
[Link]("++p = " + (++p)); // 6 (pre-increment)
[Link]("p-- = " + (p--)); // 6 (then p becomes 5)
[Link]("p = " + p); // 5

// 7. Ternary Operator
[Link]("\nTernary Operator:");
int max = (a > b) ? a : b;
[Link]("Max of a and b is: " + max); // 10
}
}
Output:-
Arithmetic Operators:
a + b = 15
a-b=5
a * b = 50
a/b=2
a%b=0

Relational Operators:
a == b: false
a != b: true
a > b: true
a < b: false
a >= b: true
a <= b: false

Logical Operators:
x && y: false
x || y: true
!x: false

Bitwise Operators:
a&b=0
a | b = 15
a ^ b = 15
~a = -11

Assignment Operators:
c += 5: 25
c *= 2: 50

Unary Operators:
++p = 6
p-- = 6
p=5

Ternary Operator:
Max of a and b is: 10
Ques 5 Write a Java program to demonstrate the use of all conditional
statements in Java including if, if-else, if-else-if ladder, nested if, and switch
statement.

public class ConditionalStatementsExample {


public static void main(String[] args) {
int number = 10;

// 1. if statement
if (number > 0) {
[Link]("The number is positive.");
}

// 2. if-else statement
if (number % 2 == 0) {
[Link]("The number is even.");
} else {
[Link]("The number is odd.");
}

// 3. if-else-if ladder
if (number < 0) {
[Link]("The number is negative.");
} else if (number == 0) {
[Link]("The number is zero.");
} else {
[Link]("The number is positive.");
}

// 4. Nested if statement
if (number > 0) {
if (number < 100) {
[Link]("The number is positive and less than 100.");
}
}

// 5. switch statement
int day = 2;
[Link]("Day " + day + " of the week is:");
switch (day) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
case 3:
[Link]("Wednesday");
break;
default:
[Link]("Another day");
}
}
}

Output:-

The number is positive.


The number is even.
The number is positive.
The number is positive and less than 100.
Day 2 of the week is:
Tuesday

Common questions

Powered by AI

A nested if statement allows for a more sophisticated decision-making process by enabling a secondary condition to be assessed only if the primary condition is met. In the given Java example, after determining that 'number > 0', the nested if checks whether the 'number < 100'. This layering of conditions allows developers to handle complex logic with multiple, dependent criteria, leading to more precise control over the program's execution path based on multiple factors .

The Java program integrates arithmetic operations and logical condition evaluations to demonstrate basic computing logic. Arithmetic operations like addition, subtraction, multiplication, and division are used with integers 'a' and 'b' to perform basic computations. Logical conditions are evaluated using relational and logical operators (e.g., 'a > b', 'x && y') to establish or test conditions like equality or conjunction. This integration allows for dynamic alteration of control flow based on computed values and logical assessments .

Logical operators such as AND (&&), OR (||), and NOT (!) enrich the decision-making process by enabling the combination and inversion of multiple boolean expressions in a concise manner. This enhanced capability allows a program to perform complex condition evaluations that consider multiple factors simultaneously, as seen with 'x && y' (false) and 'x || y' (true) in the examples. Logical operators thus facilitate robust condition handling, making programs more versatile in executing conditional logic based on various scenarios and combinations of true/false values .

Assignment operators simplify coding tasks by allowing compound assignments that combine arithmetic operations and assignment in a single step, reducing the need for more verbose expressions. In the example, operations like 'c += 5' and 'c *= 2' streamline calculations by directly applying arithmetic operators in conjunction with the assignment, enhancing code efficiency. These operators minimize redundancy and potential for errors, as they reduce the number of separate statements needed to achieve the same computational result .

The ternary operator provides a concise and efficient way to perform simple conditional checks in Java, which can improve readability and simplify decision-making logic. In the example, the ternary operator is used to determine the larger of two numbers 'a' and 'b' with the expression 'int max = (a > b) ? a : b'. This single line replaces a multi-line if-else statement, streamlining the code and reducing potential errors caused by additional complexity. Its use is particularly advantageous for uncomplicated conditions where the logic fits naturally in a compact format .

The Java program exemplifies key principles of object-oriented programming, specifically encapsulation and class-object structure. In the provided code, the 'Car' class encapsulates data members like 'color' and 'speed', and provides a method 'displayInfo()' to print these values. An object 'myCar' of type 'Car' is created in the 'main' class, demonstrating instantiation in OOP. This encapsulation of attributes and functions within a class, and the creation of an object to access them, highlights the practical implementation of OOP concepts .

Unary operators, such as increment '++' and decrement '--', impact Java programming by allowing developers to conveniently increase or decrease a variable's value by one. As demonstrated, these operators can perform operations like pre-increment (++p) or post-decrement (p--), significantly simplifying code that requires frequent and repetitive updates to a variable's value. The use of unary operators enhances the succinctness and clarity of loops and iterative processes, making them a fundamental tool in efficient coding practice .

The switch statement provides a cleaner and more readable way to execute one out of many code blocks based on the value of a variable, particularly when there are many conditions to check. In the example, the variable 'day' determines which case statement is executed, simplifying the process compared to using multiple if-else statements. The use of the switch statement improves code readability and maintenance, especially when handling a large set of values, by organizing these checks into a structured format .

Type safety in Java ensures that operations on primitive data types are restricted according to their specific data type, preventing errors such as type casting between incompatible types. The Java program exemplifies this by explicitly declaring variables of different primitive types (e.g., 'int i', 'double d', 'char c'). This strong type checking at compile time prevents misassignments, such as trying to assign a 'float' value to an 'int' without casting, thus maintaining type safety across operations on these types .

Instance variables, like 'name' and 'age' in the 'Student' class, are declared within a class but outside any method, meaning they can be accessed by any method within the class and retain their value throughout the lifetime of the object. In contrast, local variables, such as 'college' declared inside the 'displayDetails()' method, are only accessible within the method they are declared in and exist only for the duration of that method's execution. This highlights the scope and lifetime differences between instance and local variables .

You might also like