1.Introduction to java
1.Introduction to java
Control Statements
Control statements are used to control the flow of execution
in a program. Java provides the following control statements:
• Conditional Statements:
o if: Executes a block of code if a specified condition
is true.
o else: Executes a block of code if the condition is
false.
o else if: Used for multiple conditions.
o switch: Selects one of many code blocks to be
executed based on a variable's value.
• Looping Statements:
o for: Executes a block of code a certain number of
times.
o while: Executes a block of code as long as the
condition is true.
o do-while: Similar to while, but guarantees at least
one execution of the loop.
• Control Flow Statements:
o break: Terminates the loop or switch statement.
o continue: Skips the current iteration of the loop.
o return: Exits a method and optionally returns a
value.
Identifiers
In Java, identifiers are names used for classes, variables,
methods, and other elements in the program. Identifiers
must follow these rules:
• They must start with a letter, underscore (_), or dollar
sign ($).
• They can contain digits after the first character.
• They are case-sensitive (MyVar and myVar are different).
• Reserved keywords like int, class, and static cannot be
used as identifiers.
Arrays
An array in Java is a collection of variables of the same type
stored in contiguous memory locations. Arrays in Java are
objects and have the following characteristics:
• Arrays are zero-indexed.
• The size of the array is fixed and cannot be changed
once initialized.
• Arrays can be single-dimensional or multi-dimensional
(e.g., two-dimensional arrays).
Operators
Java provides various operators to perform operations on
variables and values. These are categorized into:
• Arithmetic Operators: +, -, *, /, % (modulus).
• Relational Operators: ==, !=, <, >, <=, >=.
• Logical Operators: &&, ||, !.
• Bitwise Operators: &, |, ^, ~, <<, >>, >>>.
• Assignment Operators: =, +=, -=, *=, /=, %=.
• Ternary Operator: ? : (Used as a shorthand for if-else).
Variables
A variable is a container for storing data values. In Java,
variables must be declared with a data type before use. The
three types of variables in Java are:
• Local Variables: Declared within a method and
accessible only within that method.
• Instance Variables: Declared inside a class but outside
any method and are specific to an instance of a class.
• Static Variables: Declared with the static keyword and
shared among all instances of a class.
Applications and Applets
Java supports two types of programs: applications and
applets.
• Java Applications: These are standalone programs that
run on the Java Virtual Machine. They can be console-
based or GUI-based using libraries like AWT and Swing.
• Java Applets: Applets are small Java programs that can
run inside a web browser. They require a browser with a
Java plugin or an applet viewer. Applets are now largely
obsolete due to security concerns and the rise of other
technologies like JavaScript.