0% found this document useful (0 votes)
3 views20 pages

Java Notes

This document provides an introduction to Java programming, covering its features, development kit, program structure, data types, variables, operators, and control statements. It explains key concepts such as loops, decision-making statements, and input/output operations, along with examples for better understanding. The document serves as a comprehensive guide for beginners to grasp the fundamentals of Java programming.

Uploaded by

ajadkumarmerwal
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)
3 views20 pages

Java Notes

This document provides an introduction to Java programming, covering its features, development kit, program structure, data types, variables, operators, and control statements. It explains key concepts such as loops, decision-making statements, and input/output operations, along with examples for better understanding. The document serves as a comprehensive guide for beginners to grasp the fundamentals of Java programming.

Uploaded by

ajadkumarmerwal
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

IT (802) – Java Programming Notes

Introduction to Java to Loops

1. Introduction to Java
What is Java?
Java is a high-level, object-oriented and platform-independent programming language developed by
Sun Microsystems. It was developed by James Gosling and released in 1995.
Java is used to develop:
• Desktop applications
• Web applications
• Mobile applications
• Banking applications
• Educational software
• Games and business applications
Features of Java
1. Simple
Java is easy to learn because its syntax is similar to C and C++.
2. Object-Oriented
Java is based on objects and classes.
3. Platform Independent
Java programs can run on different operating systems such as:
• Windows
• Linux
• macOS
Java follows the principle:
Write Once, Run Anywhere (WORA)
4. Secure
Java provides security features and does not allow direct access to memory.
5. Robust
Java has features such as:
• Exception handling
• Automatic memory management
• Strong type checking
6. Portable
A Java program can be transferred from one computer system to another without major changes.

2. Java Development Kit (JDK)


The Java Development Kit (JDK) is a software package used to develop and run Java programs.
JDK contains:
• JRE
• Java compiler
• Java tools and libraries
Java Runtime Environment (JRE)
The JRE provides the environment required to run Java programs.
Java Virtual Machine (JVM)
The JVM executes the Java bytecode and makes Java platform-independent.
Java Program Execution
Java Source Code

Java Compiler

Bytecode

JVM

Output
Explanation
1. The programmer writes a Java program and saves it with the .java extension.
2. The Java compiler converts the source code into bytecode.
3. The bytecode is stored in a .class file.
4. The JVM executes the bytecode and displays the output.

3. Structure of a Java Program


Example:
public class Hello
{
public static void main(String[] args)
{
[Link]("Welcome to Java");
}
}
Explanation
public class Hello
It declares a class named Hello.
• public is an access modifier.
• class is a keyword.
• Hello is the class name.
The class name and file name should be the same.
File name:
[Link]
public static void main(String[] args)
It is the main method of a Java program.
The execution of a Java program starts from the main() method.
[Link]()
It displays the output on the screen and moves the cursor to the next line.
Example:
[Link]("Hello");
Output:
Hello

4. Tokens in Java
The smallest individual units in a Java program are called tokens.
The main types of tokens are:
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Separators

4.1 Keywords
Keywords are reserved words that have a special meaning in Java.
Examples:
Class, public, static, void, int, double, if, else, for, while, do, break
Keywords cannot be used as variable names.
Incorrect:
int class = 10;
This is incorrect because class is a keyword.

4.2 Identifiers
Identifiers are the names given to:
• Variables
• Classes
• Methods
• Objects
Examples:
int marks;
double salary;
Here:
• marks is an identifier.
• salary is an identifier.
Rules for Naming Identifiers
1. An identifier can contain letters, digits, _ and $.
2. It cannot start with a digit.
3. It cannot contain spaces.
4. It cannot be a Java keyword.
5. Java identifiers are case-sensitive.
Valid identifiers:
Total, marks1, student_Name, $amount,
Invalid identifiers:
1marks
student name
class
4.3 Literals
Literals are fixed values written directly in a program.
Examples:
int age = 16;
char grade = 'A';
double percentage = 85.5;
Here:
• 16 is an integer literal.
• 'A' is a character literal.
• 85.5 is a decimal literal.

4.4 Separators
Separators are symbols used to separate different parts of a Java program.
Examples:
( ), { }, [ ], ;,
,
.

5. Variables
A variable is a named memory location used to store data.
Example:
int marks = 85;
Here:
• int is the data type.
• marks is the variable name.
• 85 is the value.
The value of a variable can be changed during program execution.
Example:
int x = 10;
x = 20;

[Link](x);
Output:
20

6. Declaration and Initialisation of Variables


Variable Declaration
The process of specifying the data type and variable name is called variable declaration.
Syntax:
dataType variableName;
Example:
int age;
Variable Initialisation
The process of assigning a value to a variable is called initialisation.
Example:
age = 17;
Declaration and Initialisation Together
int age = 17;

7. Data Types in Java


A data type specifies the type of value that a variable can store.
Java data types are mainly divided into:
1. Primitive data types
2. Non-primitive data types

7.1 Primitive Data Types


Primitive data types store simple values.
Data Type Size Example
byte 1 byte byte x = 10;
short 2 bytes short y = 500;
int 4 bytes int marks = 85;
long 8 bytes long population = 100000L;
float 4 bytes float price = 25.5f;
double 8 bytes double percentage = 89.75;
char 2 bytes char grade = 'A';
boolean 1 bit boolean result = true;

7.2 Non-Primitive Data Types


Non-primitive data types are used to store complex data.
Examples:
• String
• Arrays
• Classes
• Objects
Example:
String name = "Riya";

8. Constants
A constant is a value that cannot be changed after it is assigned.
The final keyword is used to declare a constant.
Example:
final double PI = 3.14;
The value of PI cannot be changed.
Incorrect:
PI = 3.14159;
9. Input and Output in Java
Output Using print()
The print() method displays output on the screen but does not move the cursor to the next line.
Example:
[Link]("Hello ");
[Link]("Students");
Output:
Hello Students

Output Using println()


The println() method displays output and moves the cursor to the next line.
Example:
[Link]("Hello");
[Link]("Students");
Output:
Hello
Students

10. Taking Input Using Scanner Class


The Scanner class is used to accept input from the user.
To use the Scanner class, import the following package:
import [Link];
Create a Scanner object:
Scanner sc = new Scanner([Link]);
Example:
import [Link];

public class InputDemo


{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);

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


int age = [Link]();

[Link]("Age = " + age);


}
}

Important Scanner Methods


Method Used to Input
nextInt() Integer value
nextDouble() Decimal value
Method Used to Input
nextFloat() Float value
nextLong() Long value
next() One word
nextLine() Complete line
next().charAt(0) One character
nextBoolean() Boolean value
Example:
String name = [Link]();
This accepts a complete line of text.

11. Type Conversion


Type conversion means converting a value from one data type to another.
There are two types:
1. Implicit type conversion
2. Explicit type conversion

11.1 Implicit Type Conversion


It is performed automatically by Java when a smaller data type is converted into a larger data type.
Example:
int x = 25;
double y = x;

[Link](y);
Output:
25.0

11.2 Explicit Type Conversion


It is performed manually by the programmer.
Example:
double x = 25.75;
int y = (int)x;

[Link](y);
Output:
25
The decimal part is removed.

12. Operators in Java


Operators are special symbols used to perform operations on values and variables.
The main types of operators are:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Unary operators
6. Conditional operator

13. Arithmetic Operators


Arithmetic operators are used to perform mathematical calculations.
Operator Meaning Example
+ Addition a+b
- Subtraction a-b
* Multiplication a * b
/ Division a/b
% Remainder a%b
Example:
int a = 10;
int b = 3;

[Link](a + b);
[Link](a - b);
[Link](a * b);
[Link](a / b);
[Link](a % b);
Output:
13
7
30
3
1

14. Relational Operators


Relational operators compare two values and return either true or false.
Operator Meaning
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Example:
int a = 10;
int b = 20;
[Link](a < b);
Output:
true

15. Logical Operators


Logical operators are used to combine two or more conditions.
Operator Meaning
&& Logical AND
`
! Logical NOT
Logical AND &&
The result is true only when both conditions are true.
Example:
int age = 20;

[Link](age >= 18 && age <= 60);


Output:
true
Logical OR ||
The result is true when at least one condition is true.
Example:
int marks = 40;

[Link](marks < 33 || marks > 90);


Output:
false
Logical NOT !
It changes true into false and false into true.
Example:
boolean result = true;

[Link](!result);
Output:
false

16. Assignment Operators


Assignment operators are used to assign values to variables.
Operator Example Equivalent To
= x = 10 Assign 10 to x
+= x += 5 x=x+5
-= x -= 5 x=x-5
*= x *= 5 x=x*5
Operator Example Equivalent To
/= x /= 5 x=x/5
%= x %= 5 x=x%5
Example:
int x = 10;

x += 5;

[Link](x);
Output:
15

17. Unary Operators


Unary operators work with only one operand.
The main unary operators are:
++
--
!
Increment Operator ++
It increases the value of a variable by 1.
Example:
int x = 5;

x++;

[Link](x);
Output:
6
Decrement Operator --
It decreases the value of a variable by 1.
Example:
int x = 5;

x--;

[Link](x);
Output:
4

18. Pre-Increment and Post-Increment


Pre-Increment
Syntax:
++x;
The value is increased first and then used.
Example:
int x = 5;

[Link](++x);
Output:
6
Post-Increment
Syntax:
x++;
The current value is used first and then increased.
Example:
int x = 5;

[Link](x++);
[Link](x);
Output:
5
6

19. Conditional Operator


The conditional operator is also called the ternary operator.
Syntax:
condition ? expression1 : expression2;
Example:
int age = 20;

String result = (age >= 18) ? "Eligible" : "Not Eligible";

[Link](result);
Output:
Eligible

20. Operator Precedence


Operator precedence determines the order in which operators are evaluated.
Example:
int result = 10 + 5 * 2;

[Link](result);
Output:
20
Multiplication is performed before addition.
Calculation:
10 + 5 × 2
10 + 10
20

21. Decision-Making Statements


Decision-making statements are used to make decisions based on conditions.
The main decision-making statements are:
1. if
2. if-else
3. Nested if
4. else-if ladder
5. switch

22. The if Statement


The if statement executes a block of code only when the condition is true.
Syntax:
if(condition)
{
statement;
}
Example:
int age = 20;

if(age >= 18)


{
[Link]("Eligible to Vote");
}
Output:
Eligible to Vote

23. The if-else Statement


The if-else statement is used when there are two choices.
Syntax:
if(condition)
{
statement1;
}
else
{
statement2;
}
Example:
int marks = 30;

if(marks >= 33)


{
[Link]("Pass");
}
else
{
[Link]("Fail");
}
Output:
Fail

24. Nested if Statement


An if statement inside another if statement is called a nested if statement.
Example:
int age = 20;
boolean citizen = true;

if(age >= 18)


{
if(citizen == true)
{
[Link]("Eligible to Vote");
}
}

25. else-if Ladder


The else-if ladder is used when there are multiple conditions.
Example:
int marks = 75;

if(marks >= 90)


{
[Link]("Grade A+");
}
else if(marks >= 75)
{
[Link]("Grade A");
}
else if(marks >= 60)
{
[Link]("Grade B");
}
else
{
[Link]("Grade C");
}
Output:
Grade A

26. switch Statement


The switch statement is used to select one block of code from multiple options.
Syntax:
switch(expression)
{
case value1:
statement;
break;

case value2:
statement;
break;

default:
statement;
}
Example:
int day = 2;

switch(day)
{
case 1:
[Link]("Monday");
break;

case 2:
[Link]("Tuesday");
break;

case 3:
[Link]("Wednesday");
break;

default:
[Link]("Invalid Day");
}
Output:
Tuesday
Role of break
The break statement terminates the switch block.
Without break, the control moves to the next case. This is called fall-through.
27. Loops in Java
A loop is used to execute a block of statements repeatedly until a given condition becomes false.
Loops help avoid writing the same statement again and again.
Example without a loop:
[Link]("Hello");
[Link]("Hello");
[Link]("Hello");
Using a loop:
for(int i = 1; i <= 3; i++)
{
[Link]("Hello");
}

Types of Loops
Java provides three main loops:
1. for loop
2. while loop
3. do-while loop

28. for Loop


The for loop is used when the number of repetitions is known.
Syntax:
for(initialisation; condition; update)
{
statements;
}
Example:
for(int i = 1; i <= 5; i++)
{
[Link](i);
}
Output:
1
2
3
4
5
Parts of a for Loop
for(int i = 1; i <= 5; i++)
Initialisation
int i = 1
It is executed only once.
Condition
i <= 5
The loop continues while the condition is true.
Update
i++
It increases the value of i by 1 after every iteration.

29. while Loop


The while loop checks the condition before executing the loop body.
It is called an entry-controlled loop.
Syntax:
while(condition)
{
statements;
}
Example:
int i = 1;

while(i <= 5)
{
[Link](i);
i++;
}
Output:
1
2
3
4
5

30. do-while Loop


The do-while loop executes the loop body first and checks the condition afterwards.
It is called an exit-controlled loop.
Syntax:
do
{
statements;
}
while(condition);
Example:
int i = 1;

do
{
[Link](i);
i++;
}
while(i <= 5);
Output:
1
2
3
4
5
Important Point
The do-while loop executes at least one time even if the condition is false.
Example:
int i = 10;

do
{
[Link](i);
}
while(i < 5);
Output:
10

31. Difference Between while and do-while


while Loop do-while Loop
Condition is checked first. Condition is checked after execution.
It may execute zero times. It executes at least one time.
It is an entry-controlled loop. It is an exit-controlled loop.
No semicolon is used after the condition. A semicolon is required after while(condition);

32. Nested Loop


A loop inside another loop is called a nested loop.
Example:
for(int i = 1; i <= 3; i++)
{
for(int j = 1; j <= 2; j++)
{
[Link]("i = " + i + " j = " + j);
}
}
Output:
i=1j=1
i=1j=2
i=2j=1
i=2j=2
i=3j=1
i=3j=2

33. break Statement


The break statement immediately terminates the loop.
Example:
for(int i = 1; i <= 5; i++)
{
if(i == 4)
{
break;
}

[Link](i);
}
Output:
123
When i becomes 4, the loop terminates.

34. continue Statement


The continue statement skips the current iteration and moves to the next iteration.
Example:
for(int i = 1; i <= 5; i++)
{
if(i == 3)
{
continue;
}

[Link](i);
}
Output:
1245
The value 3 is skipped.

35. Important Programs


Program 1: Print Numbers from 1 to 10
public class Numbers
{
public static void main(String[] args)
{
for(int i = 1; i <= 10; i++)
{
[Link](i);
}
}
}

Program 2: Print Even Numbers from 1 to 20


public class EvenNumbers
{
public static void main(String[] args)
{
for(int i = 2; i <= 20; i = i + 2)
{
[Link](i);
}
}
}

Program 3: Find the Sum of Numbers from 1 to 10


public class Sum
{
public static void main(String[] args)
{
int sum = 0;

for(int i = 1; i <= 10; i++)


{
sum = sum + i;
}

[Link]("Sum = " + sum);


}
}
Output:
Sum = 55

Program 4: Print a Multiplication Table


import [Link];

public class Table


{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();

for(int i = 1; i <= 10; i++)


{
[Link](n + " × " + i + " = " + n * i);
}
}
}

Quick Revision
• Java is a high-level, object-oriented and platform-independent language.
• Java follows Write Once, Run Anywhere.
• JDK is used to develop Java programs.
• JVM executes Java bytecode.
• A variable stores data in memory.
• Java has primitive and non-primitive data types.
• Scanner is used to accept input.
• print() displays output on the same line.
• println() displays output and moves to the next line.
• if is used for one condition.
• if-else is used for two choices.
• else-if ladder is used for multiple conditions.
• switch is used to select one option from many choices.
• for loop is generally used when the number of repetitions is known.
• while is an entry-controlled loop.
• do-while is an exit-controlled loop.
• break terminates a loop.
• continue skips the current iteration.

You might also like