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

Chapter No 3 Java

Chapter 3 covers the fundamentals of Java, including its object-oriented nature, variable declaration, data types, and operators. It explains control structures like if-else and switch statements, as well as loops such as while, do-while, and for. Additionally, it introduces concepts of object-oriented programming, exception handling, multithreading, and wrapper classes.

Uploaded by

padmalhadon6
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)
12 views9 pages

Chapter No 3 Java

Chapter 3 covers the fundamentals of Java, including its object-oriented nature, variable declaration, data types, and operators. It explains control structures like if-else and switch statements, as well as loops such as while, do-while, and for. Additionally, it introduces concepts of object-oriented programming, exception handling, multithreading, and wrapper classes.

Uploaded by

padmalhadon6
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

CHAPTER NO 3

JAVA
Java is a high-level, class-based, object-oriented programming language.
Java Interpreter java program to Java Bytecode. This method has the
advantage that once a Java programme has been converted to bytecode, it
can be executed on any platform (such as Windows, Linux, or Mac), provided
the platform is running the JVM. Because of this, Java programmes are
extremely portable and cross-platform.

JAVA ByteCode?

Bytecode in a java is a set of instructions for java virtual [Link] java


code is compiled the bytecode gets generated which can execute on any
machine using JVM(runtime environment in which byte code can be excuted)

What is Variable?
Variables will be used to store the program’s data. A variable is a placeholder
for information whose value may change as a result of a program’s execution.
In a computer, a variable is the name of a memory region that stores data. All
data variables in Java have to be declared and initialized before they are
used.

Int a=10;

What is Datatype?
When declaring variables, we have to specify the data type of information that
the member will hold – integer, fractional, alphanumeric, and so on. The type
of a variable tells the compiler, how much memory to reserve when storing a
variable of that type.

Java Datatype is divided into two types –

1. Primitive data types – includes byte, short, int, long, float, double,
boolean and char.
2. Non-primitive data types – such as String, Arrays and Classes.
Primitive data types
A reserved term is used to identify a primitive type, which is predefined by the
language. The Java programming language’s support eight primitive data
types.
Data Type Type of values Size

Byte Integer 8-bit

Short Integer 16-bit

Int Integer 32-bit

Long Integer 64-bit

Float Floating Point 32-bit

Double Floating Point 64-bit

Char Character 16-bit

Boolean True or False 1-bit

Java Variables Naming rules


1. Variable names can begin with either an alphabetic character, an
underscore (_), or a dollar sign ($).
2. There are some reserved words in Java that cannot be used as
variable names, for example – int.
3. Java is a case-sensitive language. Variable names written in
capital letters differ from variable names with the same spelling
but written in small letters.
String Variable
String variables, also known as alphanumeric variables or character variables,
treat their values as text. As a result, string variables may have values that are
made up of letters, numbers, or symbols.

Example –

String first_name = “Mayank”;


String last_name = “Saxena”;
Operators
In a programming language, operators are special symbols that carry out
certain operations.

1. Arithmetic Operators(=,-,/,*,%)
2. Relational Operators(==,!=,<=,>=)
3. Assignment Operators(=,=+)
4. Logical Operators(&&,II,!)

EXAMPLE:
Selection Structures
In real life, you often select your actions based on whether a condition is true
or false. Similarly in a program, you may want to execute a part of the
program based on the value of an expression. Java provides two statements

1. If else Statement
2. Switch Statement

The if Else Statement


The if else statement in Java lets us execute a block of code depending upon
whether an expression evaluates to true or false. The structure of the Java if
statement is as below –

Syntax –
if (expression)
{
statements
}
else
{
statements
}

The Switch Statement


The switch statement is used to execute a block of code matching one value
out of many possible values. The structure of the Java switch statement is as
follows –

Syntax –
switch (expression) {
case 1:
[Link](“Case 1”);
case 2:
[Link](“Case 2”);
case 3:
[Link](“Case 3”);
default:
[Link](“Default case”);
}

Repetition Structures
In real life you often do something repeatedly, for example, consider a task
such as reading a book, first you open the book, and then repeatedly – read a
page; flip the page – until you get to the end of the book, then close the book.

Difference between Entry control loop and Exit control loop

Entry Control Loop Exit Control Loop

In entry control loop condition is checked


In exit control loop condition is checked last
first

If the condition is false, loop body will not If the condition is false, loop body will execute at
execute least once

Example of entry control loop – For &


Example of exit control loop – Do-while
While

The While Statement


The while statement evaluates the test before executing the body of a loop.
The structure of the Java while statement is as shown –

Syntax –
while (expression)
{
statements
}

Question > Write a Java program to print the number from 1 to 5 using while
statement.
public class WhileDemo
{
public static void main (String[ ] args)
{
int number = 1;
while (number <= 5) {
[Link] (“number);
number++;
}
}
}
The Do While Statement
The do while statement evaluates the test after executing the body of a loop.
The structure of the Java do while statement is as shown –

Syntax –
do
{
statements
} while (expression);

Question > Write a Java program to print the number from 1 to 5 using do-
while statement.
public class DowhileDemo
{
public static void main (String[ ] args) {
int number = 1;
do {
[Link] (“number);
number++;
} while (number <= 5);
}

The for Statement


The for loop is the most widely used Java loop construct. The structure of the
Java for statement is as below:

Syntax –
for (counter=initial_value; test_condition;change counter)
{
statements
}
Question > Write a Java program to print the number from 1 to 5 using for
statement.
public class WhileDemo {
public static void main (String[ ] args) {
int number;
for (number=1; number <= 5; number++)
{
[Link] (“number);
}
}
}
Arrays in Java
Arrays are variables that can hold more than one value, they can hold a list of
values of the same type.

User Defined Methods


A method in Java is a block of statements grouped together to perform a
specific task. A method has a name, a return type, an optional list of
parameters, and a body. The structure of a Java method is as below –

Syntax –
return_type method_name(list of parameters separated by commas)
{
statements
return statement
}

Example – Let us write a method that given the length and breadth of a
rectangle as parameters returns the area of the rectangle.
static double rectangle_area (double length, double breadth)
{
return (length * breadth);
}
Object Oriented Programming
A computer programming paradigm known as object-oriented programming
(OOP) arranges the architecture of software around data or objects rather
than functions and logic. An object is a data field with particular characteristics
and behaviour.

What is Class?
A class is a collection of objects with similar characteristics. It serves as a
model or blueprint from which things can be made. It makes sense as a
whole. It cannot be bodily.

A class in Java can contain:

• Fields
• Methods
• Constructors
• Blocks
• Nested class and interface

Syntax to declare a class:


class <class_name>
{
field;
method;
}

What is Object?
An object is an entity with state and behaviour, such as a chair, bike, marker,
pen, table, or car. It could be intellectual or physical (tangible and intangible).
The banking system is an illustration of an intangible entity.

Object Definitions:
• An object is a real-world entity.
• An object is a runtime entity.
• The object is an entity which has state and behavior.
• The object is an instance of a class.
Constructors
A special method member called the constructor method is used to initialize
the data members of the class (or any other initialization is to be done at time
of object creation).
Access Modifiers
In object-oriented languages, the accessibility of classes, methods, and other
members is controlled through the use of access modifiers . Access modifiers
are a particular type of syntax used in programming languages that make it
easier to encapsulate components

Getter and Setter Methods


A class’s private data members cannot be accessed from outside the class,
but you can grant getter and setter methods controlled access to data
members outside the class. The value of a data member is returned by a
getter method.

For example we could define a getter method in the Bookclass for the price
data member as given below –

double getPrice ( )
{
return price;
}

Exception Handling
Runtime issues such as ClassNotFoundException, IOException,
SQLException, RemoteException, etc. can be handled through the Java
Exception Handling mechanism.

try – A try block surrounds the part of the code that can generate exception(s).
catch – The catch blocks follow a try block. A catch block contains the
exception handler – specific code that is executed when the exception occurs

Threads
A multithreaded programme can execute numerous tasks simultaneously for
the best possible resource utilisation on the computer. A multithreaded
programme is made up of two or more threads, each of which is capable of
carrying out a particular task independently and concurrently.

In Java, threads can be created in two ways


1. By extending the Thread class
2. By implementing the Runnable interface

Example:
Public class ExtendThread extends thread
{
Public void main()
{
[Link](“created a thread”);
}
For (int count=1;count<=3;count++)
[Link](“count=”+count);
}
}

Wrapper Classes
Java’s primitive datatypes, including int, float, and others, are typically
supplied by value rather than via reference. Primitive datatypes may
occasionally need to be passed by reference. When that happens, you can
use the Java wrapper classes.

ASSERATION
An asseration is a useful mechanism for effective identifying/detecting
and correcting logical errors in a program

You might also like