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

Java Mannual

The document provides an overview of Java programming, covering the fundamentals of Object-Oriented Programming (OOP), including key concepts such as classes, objects, inheritance, polymorphism, abstraction, and encapsulation. It also discusses Java's features, differences between Java and C++, and the structure of a Java program, including how to compile and execute a simple program. Additionally, it explains data types, variable naming conventions, operators, and includes examples of arithmetic operations in Java.

Uploaded by

teesa
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)
2 views12 pages

Java Mannual

The document provides an overview of Java programming, covering the fundamentals of Object-Oriented Programming (OOP), including key concepts such as classes, objects, inheritance, polymorphism, abstraction, and encapsulation. It also discusses Java's features, differences between Java and C++, and the structure of a Java program, including how to compile and execute a simple program. Additionally, it explains data types, variable naming conventions, operators, and includes examples of arithmetic operations in Java.

Uploaded by

teesa
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
You are on page 1/ 12

JAVA PROGRAMMING

Module : 1
Fundamentals of Object Oriented Programming: Introduction, Object Oriented Paradigm,
Basic concepts of OOP: Class, Object, Inheritance, Polymorphism, Abstraction, Encapsulation..
Task: introduction to Object Oriented Programming and its basic concepts.

Introduction to Object Oriented Programming:


Java is a class-based, object-oriented programming language that is designed to have as few implementation
dependencies as possible. It is intended to let application developers Write Once and Run Anywhere (WORA),
meaning that compiled Java code can run on all platforms that support Java without the need for recompilation. Java
was developed by James Gosling at Sun Microsystems Inc. in May 1995 and later acquired by Oracle
Corporation and is widely used for developing applications for desktop, web, and mobile devices.

Java is known for its simplicity and security features, making it a popular choice for enterprise-level applications.
Java applications are compiled to byte code that can run on any Java Virtual Machine. The syntax of Java is similar
to C/C++.
Java makes writing, compiling, and debugging programming easy. It helps to create reusable code and modular
programs.

Object Oriented Paradigm:

The object-oriented paradigm (OOP) is a programming paradigm that uses objects to design computer
programs. Objects are made up of data and code, and programs are built by making objects that interact with each other.

Basic concepts of OOP:

What is OOP?

Procedural programming is about writing procedures or methods that perform operations on the data, while object-
oriented programming is about creating objects that contain both data and methods.The popular object-oriented
languages are Java, C#, PHP, Python, C++, etc.

The main aim of object-oriented programming is to implement real-world entities, for example, object, classes,
abstraction, inheritance, polymorphism, etc.
OOPs (Object-Oriented Programming System)

Object means a real-world entity such as a pen, chair, table, computer, watch, etc. Object-Oriented Programming is a
methodology or paradigm to design a program using classes and objects. It simplifies software development and

maintenance by providing some concepts:

o Object

o Class

o Inheritance

o Polymorphism

o Abstraction

o Encapsulation

Object

Any entity that has state and behavior is known as an object. For example, a
chair, pen, table, keyboard, bike, etc. It can be physical or logical.

Example: A dog is an object because it has states like color, name, breed, etc.
as well as behaviors like wagging the tail, barking, eating, etc.

Class

Collection of objects is called class. It is a logical entity.A class can also be


defined as a blueprint from which you can create an individual object. Class
doesn't consume any space.

Inheritance

When one object acquires all the properties and behaviors of a parent object, it is known as inheritance. It provides
code reusability. It is used to achieve runtime polymorphism.

Polymorphism
If one task is performed in different ways, it is known as polymorphism. For example: to convince the customer
differently, to draw something, for example, shape, triangle, rectangle, etc.

In Java, we use method overloading and method overriding to


achieve polymorphism.Another example can be to speak something;
for example, a cat speaks meow, dog barkswoof, etc.

Abstraction

Hiding internal details and showing functionality is known as


abstraction. For example phone call, we don't know the internal processing. In Java, we use abstract class and interface
to achieve abstraction.

Encapsulation

Binding (or wrapping) code and data together into a single unit are known as encapsulation. For example, a capsule, it is
wrapped with different medicines.

A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are
private here.

Module : 2

Overview of Java Language: Introduction, Java features, Java program structure, parts of Java, Java
Virtual Machine-Java versus C++, How to Compile & Executing a basic java program.
Task: Differences between Java and C++, Execute “Hello welcome to java” program

Java is a popular, object-oriented programming language that can be used to create a variety of applications, including:

 Mobile apps
 Web apps
 Enterprise software
 Internet of Things (IoT) devices
 Gaming
 Big data
 Cloud-based applications

key features of Java:

 Portability: Java applications can run on any computer with a Java interpreter, regardless of the operating
system or hardware.

 Security: Java has built-in security features to protect against malicious programs and erroneous code.

 Reliability: Java is designed to create reliable software with compile-time and run-time checking.

 Simple memory management: Java uses automatic garbage collection and a simple memory management
model that eliminates many programming errors.

 Open standard: Java is an open standard with publicly available source code.

 Syntax: Java's syntax is similar to C.

 Java virtual machine (JVM): The JVM provides the basis for platform independence.

Java was created by James Gosling in 1995. The original name for the language was Oak, but it was changed to Java
after it was discovered that Oak was already a trademark.

Structure of Java Program:

Java is an object-oriented programming, platform-independent, and secure programming language that makes it
popular. Using the Java programming language, we can develop a wide variety of applications. So, before diving in
depth, it is necessary to understand the basic structure of Java program in detail. In this section, we have discussed
the basic structure of a Java program. At the end of this section, you will able to develop the Hello world Java
program, easily.
Differences between Java and C++

C++ Java
C++ is designed to work with compiler only Java can support both compiler and interpreter
Platform dependent Platform independent
C++ uses “cin” and “cout” Complex in/out methods (System.in and System.out)
Incorporates backward compatibility with C No backward compatibility support
C++ is a combination of OOPs and Procedural Supports only Object-Oriented Programming style
type of programming
Memory management is manual, and the user is JVM manages memory without user intervention
responsible for the memory consumed
C++ can provide multiple inheritances Java cannot support multiple inheritances
C++ supports both method overloading and Java supports only method overloading
operator overloading
Source code is not portable between different Source code is portable to any operating system
operating systems
Libraries offer low-level of functionality Libraries offer high-level functionality
The programmer is responsible for run-time errors JVM is responsible for run-time errors and exceptions
and exceptions
C++ needs manual class and object management Java is completely automatic regarding class and object
using new and delete keywords management
C++ needs manual garbage memory clearance Java has an automatic garbage
Execute “Hello welcome to java” program:

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello welcome to java");

}

 }
 Save the code: Save the code in a file named HelloWorld.java.

 Compile the code: Open a terminal or command prompt, navigate to the directory where you saved the file, and run the
following command:

javac HelloWorld.java

 Run the code: After successful compilation, you can run the program using the following command:

Code

java HelloWorld

This will print "Hello welcome to java" on your console.

Module : 3
Variables-Identifiers-Literals- Data types: Integer literals-character literals-Floating point
literals- String Literals, Variables, Keywords, Data types.
Task: implementing data types with variables, find valid/invalid variables, Identifiers

Implementing data types with variables:


Implement data types with variables, you need to declare the variable, which specifies the type of data the variable
will hold:
 Data type: The type of information the variable will store, such as numbers, letters, text, decimal, or Boolean
 Variable name: The name of the variable
 Value: The information being stored, which is optional when declaring
The type of a variable determines the values it can have and the operations that can be performed on it. Using the
appropriate data type can help enhance performance and maintain the integrity of the data.
Here are some examples of data types and how they are used:
 Boolean: A data type that can only be one of two values, true or false. Booleans are used to represent truth values
in algorithms in computer science.
 Char: A data type that refers to character values enclosed in single quotes. The range of a char data type is -127 to
127.
 String: A data type for storing text.

Find valid/invalid variables:

To determine whether a variable name is valid or invalid, follow the basic rules for naming variables in programming
(these rules might slightly vary based on the language, but the general principles are consistent):

Rules for Valid Variable Names:


1. Starts with a letter or underscore: Variable names must begin with a letter (a-z, A-Z) or an underscore _. They
cannot start with a number.
2. Only contains letters, numbers, or underscores: No spaces, special characters (e.g., @, #, $, etc.), or operators
are allowed.
3. Cannot be a reserved keyword: Variable names cannot use words that the programming language has reserved
for its syntax (e.g., if, else, while).
4. Case-sensitive: In most languages, variable names are case-sensitive (myVariable and MyVariable are different).
5. Reasonable length: While not a strict rule, it's good practice to use descriptive names that aren't too long.

Examples of Valid and Invalid Variable Names:

Variable Name Valid/Invalid Reason


myVar Valid Follows all naming rules.
_my_var Valid Starts with an underscore.
var1 Valid Contains only letters and numbers.
1var Invalid Cannot start with a number.
my-var Invalid Hyphens are not allowed.
my var Invalid Spaces are not allowed.
while Invalid Reserved keyword.
$myVar Invalid $ is not allowed in many languages.
__init__ Valid Special name, but valid in Python.
myVariable123 Valid Follows all naming rules.

Identifiers in Java

public class IdentifierExample {

public static void main(String[] args) {

// Valid identifiers

int myVar = 10; // Starts with a letter

int _value = 20; // Starts with an underscore

int $amount = 30; // Starts with a dollar sign

int value123 = 40; // Contains letters and digits

// Print valid identifiers

System.out.println("Valid Identifiers:");

System.out.println("myVar = " + myVar);

System.out.println("_value = " + _value);

System.out.println("$amount = " + $amount);

System.out.println("value123 = " + value123);

// Invalid identifiers (Uncomment to see compilation errors)

// int 123value = 50; // Cannot start with a digit

// int my-var = 60; // Hyphens are not allowed

// int class = 70; // Cannot use reserved keywords

// Note: Uncommenting the above lines will result in compilation errors

Explanation of the Program:


1. Valid Identifiers:

o myVar, _value, $amount, value123 follow all the rules.

2. Invalid Identifiers (commented out in the program):

o 123value: Starts with a digit.

o my-var: Contains a hyphen, which is not allowed.

o class: Uses a reserved keyword.

Output:

Valid Identifiers:

myVar = 10

_value = 20

$amount = 30

value123 = 40
Module : 4
Operators: Arithmetic operators, Relational operators, Assignment operators, Conditional
operators, Type casting/Type Conversion in java.
Task: Perform all arithmetic operators using a single program, program using typecast/type
conversion

JAVA OPERATORS:

 In Java Variables are Value Container used to store some value.


 In order to Perform some operation on the Data we use different operators such as arithmetic
Operator,Logical Operator etc.
Types of Operators:

1. Assignment Operator
2. Arithmetic Operator
3. Relational Operator
4. Conditional Operators

Operators in programming and mathematics are symbols or keywords used to perform operations on
operands.

1. Arithmetic Operators

Perform basic mathematical operations.

 Addition (+): Adds two operands (e.g., a + b)

 Subtraction (-): Subtracts one operand from another (e.g., a - b)

 Multiplication (*): Multiplies two operands (e.g., a * b)

 Division (/): Divides one operand by another (e.g., a / b)

 Modulus (%): Returns the remainder of a division (e.g., a % b)

 Exponentiation ( or pow()): Raises one operand to the power of another (e.g., a ** b or pow(a, b))

2. Relational (Comparison) Operators

Compare two values and return a Boolean (true or false).

 Equal to (==): Checks if two values are equal.


 Not equal to (!= or <>): Checks if two values are not equal.

 Greater than (>): Checks if the left operand is greater than the right.

 Less than (<): Checks if the left operand is less than the right.

 Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right.

 Less than or equal to (<=): Checks if the left operand is less than or equal to the right.

3. Assignment Operators

Assign values to variables.

 Basic Assignment (=): Assigns a value (e.g., x = 10).

 Add and assign (+=): Adds and assigns (e.g., x += 5 means x = x + 5).

 Subtract and assign (-=): Subtracts and assigns.

 Multiply and assign (*=): Multiplies and assigns.

 Divide and assign (/=): Divides and assigns.

 Modulus and assign (%=): Applies modulus and assigns.

4. Conditional (Ternary) Operator

A shorthand for if-else statements.

 Syntax: condition ? value_if_true : value_if_false

Other Operators

 String Concatenation (+): Joins strings (e.g., "Hello " + "World").

 Type Casting Operators: Convert data types (e.g., int(), str()).

 Null Coalescing (?? or or): Returns the first non-null value (varies by language).

Perform all arithmetic operators using a single program?

// Program to demonstrate all arithmetic operators in Java

public class ArithmeticOperators {


public static void main(String[] args) {
// Define operands
int a = 10;
int b = 3;

System.out.println("Operands: a = " + a + ", b = " + b);


// Addition
System.out.println("Addition (a + b): " + (a + b));

// Subtraction
System.out.println("Subtraction (a - b): " + (a - b));

// Multiplication
System.out.println("Multiplication (a * b): " + (a * b));

// Division
System.out.println("Division (a / b): " + ((double)a / b));

// Modulus
System.out.println("Modulus (a % b): " + (a % b));

// Exponentiation
System.out.println("Exponentiation (Math.pow(a, b)): " + Math.pow(a, b));

// Floor Division (simulated using integer division)


System.out.println("Floor Division (a // b): " + (a / b));
}
}

Output:

Operands: a = 10, b = 3

Addition (a + b): 13

Subtraction (a - b): 7

Multiplication (a * b): 30

Division (a / b): 3.3333333333333335

Modulus (a % b): 1

Exponentiation (Math.pow(a, b)): 1000.0

Floor Division (a // b): 3

You might also like