0% found this document useful (0 votes)
5 views8 pages

5.java Programmingunit 1

The document provides an overview of Java programming, covering key concepts such as program structure, operators, tokens, command line arguments, data types, input handling, iterative and selection statements, type casting, and object-oriented programming principles including encapsulation, inheritance, polymorphism, and abstraction. Each section includes definitions and examples to illustrate the concepts. This serves as a foundational guide for understanding Java programming basics.
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)
5 views8 pages

5.java Programmingunit 1

The document provides an overview of Java programming, covering key concepts such as program structure, operators, tokens, command line arguments, data types, input handling, iterative and selection statements, type casting, and object-oriented programming principles including encapsulation, inheritance, polymorphism, and abstraction. Each section includes definitions and examples to illustrate the concepts. This serves as a foundational guide for understanding Java programming basics.
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

Java Programming - 10 Marks

Answers
1. Program Structure in Java
The basic structure of a Java program consists of:
1. Package Declaration (optional)
2. Import Statements
3. Class Definition
4. main() method
5. Other Methods

Example:

class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}

2. Various types of Java Operators with


Examples
Java provides several types of operators:
1. Arithmetic Operators (+, -, *, /, %)
2. Relational Operators (==, !=, >, <, >=, <=)
3. Logical Operators (&&, ||, !)
4. Assignment Operators (=, +=, -=, etc.)
5. Unary Operators (++ , --)
6. Bitwise Operators (&, |, ^, <<, >>)
7. Ternary Operator (?:)

Example:
int a = 10, b = 20;
[Link](a + b); // Arithmetic
[Link](a > b); // Relational
[Link](a < b && b > 5); // Logical

3. Tokens in Java
Tokens are the smallest elements of a Java program. They include:
1. Keywords: reserved words like int, class, public
2. Identifiers: names for variables, classes, methods
3. Literals: constants like 10, 3.14, 'A'
4. Operators: +, -, *, /, etc.
5. Separators: (), {}, [], ;, ,
6. Comments: // for single-line, /* */ for multi-line

4. Command line arguments in main()


We can pass values through command line to customize behavior of the
program.

Example:
class Test {
public static void main(String[] args) {
[Link]("Argument: " + args[0]);
}
}
Run as: java Test Hello

5. Data types in Java


Java data types are divided into:
1. Primitive Types: byte, short, int, long, float, double, char, boolean
2. Non-Primitive Types: String, Arrays, Classes, Interfaces

Example:
int age = 25;
float marks = 85.5f;
char grade = 'A';
6. Reading input using Scanner
import [Link];
class InputExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter name: ");
String name = [Link]();
[Link]("Hello " + name);
}
}

7. Iterative Statements
Types:
1. for loop
2. while loop
3. do-while loop

Example:
for(int i=1; i<=5; i++) {
[Link](i);
}

8. Type casting & Automatic Promotion


Type casting: Converting one data type into another.
Automatic Promotion: Smaller types automatically promoted to larger types.

Example:
int x = 10;
double y = x; // automatic promotion
double z = (double) x; // explicit casting
9. Selection Statements
Types: if, if-else, nested if, switch

Example:
int num = 5;
if(num > 0) {
[Link]("Positive");
} else {
[Link]("Negative");
}

10. OOP Principles


1. Encapsulation – Wrapping data & methods together

 Encapsulation is the process of binding data (variables/attributes)


and methods (functions/operations) into a single unit called a
class.

 It helps in data hiding, meaning internal object details are not directly
exposed to the outside world.

 Access to the data is controlled through getters and setters (public


methods).

 Provides security, maintainability, and modularity.

Key Points:

 Achieved using access modifiers (private, protected, public).

 Prevents unauthorized access and accidental modification of data.

Example:

class Student {

private int marks; // private data

// public method to set data


public void setMarks(int m) {

marks = m;

// public method to get data

public int getMarks() {

return marks;

2. Inheritance – Acquiring properties of another class

 Inheritance allows a child class (subclass/derived class) to acquire


the fields and methods of a parent class (superclass/base class).

 Promotes code reusability and reduces redundancy.

 Supports hierarchical relationships among classes.

 Types:

o Single Inheritance (one parent → one child)

o Multilevel Inheritance (grandparent → parent → child)

o Hierarchical Inheritance (one parent → many children)

o Multiple Inheritance (supported via interfaces in Java, directly


in C++).

Example:

class Vehicle {

void start() {

[Link]("Vehicle starts");

}
class Car extends Vehicle {

void drive() {

[Link]("Car is driving");

3. Polymorphism – One name, many forms

Polymorphism means the same method or operation behaves


differently depending on the context.
There are two main types:

a) Compile-time Polymorphism (Method Overloading)

 Multiple methods with the same name but different parameter


lists.

 Decided at compile time.

Example:

class MathOperation {

int add(int a, int b) { return a + b; }

double add(double a, double b) { return a + b; }

b) Runtime Polymorphism (Method Overriding)

 A subclass provides a new definition of a method already defined


in the parent class.

 Decided at runtime, depending on the object type.

Example:

class Animal {

void sound() { [Link]("Animal makes sound"); }

}
class Dog extends Animal {

@Override

void sound() { [Link]("Dog barks"); }

4. Abstraction – Hiding implementation details

 Abstraction is the process of hiding internal implementation and


showing only the essential details to the user.

 Focuses on what an object does, not how it does it.

 Achieved through:

o Abstract classes (partial abstraction, can have both abstract


and concrete methods).

o Interfaces (full abstraction, only method signatures without


implementation).

Advantages:

 Improves security by hiding unnecessary details.

 Makes code more flexible and maintainable.

Example:

abstract class Shape {

abstract void draw(); // abstract method (no body)

class Circle extends Shape {

void draw() { [Link]("Drawing Circle"); }

You might also like