Unit 1
Unit 1
Unit 1
Objects
javac filename.java
java filename
import java.util.Scanner;
This will create a new Person object with the name "John" and
age 30, and assign it to the person1.
Methods in Java classes
int x = MyClass.myStaticVariable;
Access Specifiers and Static Members
To declare a static method in a Java class, you
can use the "static" keyword in the method
declaration. For example:
MyClass.myStaticMethod();
/*
This is a multi-line comment
It can span multiple lines
and is often used to provide detailed explanations of
code
*/
int x = 5; // This line declares a variable and assigns it the value 5
It is important to use comments in your code to make it
easier to understand and maintain.
Comments, Data Types and Variables in Java
Understanding data types in Java:
Primitive data types:
byte: 8-bit integer.
short: 16-bit integer.
int: 32-bit integer.
long: 64-bit integer.
float: 32-bit floating-point number.
double: 64-bit floating-point number.
char: 16-bit Unicode character.
boolean: true/false value
Comments, Data Types and Variables in Java
For example,
if you want to declare and initialize an integer variable
named age with an initial value of ’25’, you would use
the following code:
int age = 25;
If you want to declare and initialize a string variable
named name with an initial value of "John", you would
use the following code:
String name = "John";
Comments, Data Types and Variables in Java
int x = 10;
double y = 3.14;
char c = 'a';
boolean b = true;
System.out.println(x); // output: 10
System.out.println(y); // output: 3.14
System.out.println(c); // output: a
System.out.println(b); // output: true
Comments, Data Types and Variables in Java
int a = 10;
int b = 20;
boolean result = (a == b); // false
result = (a != b); // true
result = (a < b); // true
result = (a > b); // false
result = (a <= b); // true
result = (a >= b); // false
Operators in Java
boolean a = true;
boolean b = false;
boolean result = (a && b); // false
result = (a || b); // true
result = !a; // false
Operators in Java
Bitwise Operators: Bitwise operators are used to
perform operations on individual bits of a number. They
are used in low-level programming and are not
commonly used in everyday programming. The
following are the bitwise operators available in Java:
Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise Complement
<< Left Shift (Use 0)
>> Right Shift (Signed use 1)
>>> Unsigned Right Shift (Signed use 0)
Operators in Java
int a = 3; // 0011
int b = 6; // 0110
int result = (a & b); // 0010 (AND)
result = (a | b); // 0111 (OR)
result = (a ^ b); // 0101 (XOR)
result = ~a; // 1100 (Complement)
result = (a << 1); // 0110 (Left Shift)
result = (a >> 1); // 0001 (Right Shift)
result = (a >>> 1); // 0001 (Unsigned Right Shift)
Operators in Java
•selection statements
•iteration statements
•jump statements
Control Flow
Selection statements:
Selection statements in Java allow you to make decisions
based on certain conditions. There are two types of
selection statements in Java: the if-else statement and the
switch statement.
The if-else statement allows you to execute a block of
code if a certain condition is true, and another block of
code if it is false. For example
int x = 5;
if(x > 0) {
System.out.println("x is positive");
} else {
System.out.println("x is not positive");
}
Control Flow
The switch statement allows you to execute different
blocks of code based on the value of a variable. For
example:
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
}
Control Flow
Iteration statements:
Iteration statements in Java allow you to execute a
block of code repeatedly. There are three types of
iteration statements in Java: the while loop, the do-
while loop, and the for loop.
The while loop executes a block of code repeatedly as
long as a certain condition is true. For example:
int i = 0;
while(i < 10) {
System.out.println(i);
i++;
}
Control Flow
The do-while loop is similar to the while loop, but it
always executes the block of code at least once, even if
the condition is false. For example:
int i = 0;
do {
System.out.println(i);
i++;
} while(i < 10);
Control Flow
The for loop is used to execute a block of code
repeatedly a fixed number of times. For example:
•break statement
•continue statement
•return statement
Control Flow
The break statement is used to terminate a loop or
switch statement. For example:
// if statement
if (x < 10) {
System.out.println("x is less than 10");
}
Control Flow
// if-else statement
if (x > 10)
{
System.out.println("x is greater than 10");
}
else
{
System.out.println("x is not greater than 10");
}
Control Flow
// switch statement
switch (x) {
case 1:
System.out.println("x is 1");
break;
case 2:
System.out.println("x is 2");
break;
case 3:
System.out.println("x is 3");
break;
default:
System.out.println("x is not 1, 2, or 3");
break;
}
Control Flow
// while loop
int y = 0;
while (y < x) {
System.out.println("y is " + y);
y++;
}
// for loop
for (int i = 0; i < x; i++) {
System.out.println("i is " + i);
}
Control Flow
// do-while loop
int z = 0;
do {
System.out.println("z is " + z);
z++;
} while (z < x);
}
}
Control Flow
• This program initializes a variable x to 5, and then demonstrates several
control flow statements:
• if statement: checks if x is less than 10, and prints a message if it is.
• if-else statement: checks if x is greater than 10, and prints a message if it is,
or a different message if it is not.
• switch statement: checks the value of x, and prints a message based on the
case it matches (or the default case if it doesn't match any).
• while loop: initializes a variable y to 0, and repeatedly prints its value while
it is less than x.
• for loop: initializes a variable i to 0, and repeatedly prints its value while it is
less than x.
• do-while loop: initializes a variable z to 0, and repeatedly prints its value
while it is less than x. Note that the loop body is executed at least once,
even if the condition is false initially.
Java Array
In Java, an array is a data structure that allows you to
store a collection of elements of the same type in a
contiguous block of memory. It is a fundamental
concept in programming and is used extensively in Java
programs.
Declaring an array in Java
To declare an array in Java, you need to specify the type
of the elements and the number of elements in the
array. The syntax for declaring an array is as follows:
To initialize an array when you declare it, you can use the
following syntax:
type[] arrayName = { element1, element2, ..., elementN };
Java Array
Here, type is the data type of the elements in the array,
arrayName is the name of the array, and element1,
element2, ..., elementN are the initial values of the
elements in the array.
int[] numbers = { 1, 2, 3, 4, 5 };
Java Array
Accessing elements in an array
To access an element in an array in Java, you use the
index of the element. The index of the first element in
the array is 0, and the index of the last element is
arraySize-1.
numbers[2] = 10;
Java Array
Iterating over an array
To iterate over an array in Java, you can use a loop such
as a for loop.
int[][] matrix = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };