Java Notes
Java Notes
Variables:
Non-primitive data types are called reference types because they refer to
objects.
The main differences between primitive and non-primitive data types are:
• Primitive types in Java are predefined and built into the language,
while non-primitive types are created by the programmer (except
for String).
• Non-primitive types can be used to call methods to perform certain
operations, whereas primitive types cannot.
• Primitive types start with a lowercase letter (like int), while non-
primitive types typically starts with an uppercase letter (like String).
• Primitive types always hold a value, whereas non-primitive types can
be null.
• Examples of non-primitive types are Strings, Arrays, Classes etc.
Literals
Binary literals are a combination of 0’s Octal literals must be prefixed with a
and 1’s. zero and only digits from 0 to 7 are
Binary literals must be prefixed with allowed.
0b or 0B (zerob or zeroB). For example,
For example,
int x = 011;//value in x is 9
int bin1 = 0b1010001; // value in bin1
will be 81
Hexadecimal literals are prefixed with Integer literals are of type int, by
0x or 0X; the digits 0 through 9 and a default. To define them as long, we can
through f (or A through F) are only place a suffix of L or l after the number.
allowed. Example:
Example: long l = 2345678998L;
int y = 0x0001; //value in y is 1
Floating literals are of type double, by default. To define them as float literals,
we need to attach the suffix F or f. For double literals, D or d are suffixed at the
end; however, it is optional.
Example:
float f = 23.6F;
double d = 23.6;
➢ Boolean Literals: Boolean literals can only have two values: true or
false. By default, it takes the value false.
Examples:
booleanisJavaFun = true;
booleanisTired = false;
➢ char literals: is single character is enclosed in single quotes.
Example:
char sample = 'A';
char example = 'a';
Operators
1. Binary Operator:
• Assignment Operator:
• Arithmetic Operator:
• Relational Operator:
• Logical Operator:
➢ Assignment Operator:
Example:
➢ Arithmetic Operator:
➢ Logical Operator:
• Logical Operators are primarily used in decision-making and control
flow, allowing multiple conditions to be combined or negated.
o Logical AND (&&):
o Logical OR (||):
o Logical NOT (!):
2. Logical OR (||):
• Returns true if at least one condition is true.
• If both conditions are false, the result is false.
Example:
2. Unary Operator:
3. Ternary Operator:
Syntax:
variable = (condition) ? expression1 : expression2
Example:
package First_Package;
public class New_class {
public static void main(String[] args) {
1. Conditional Statements:
a) If…else:
if (condition1)
{
// Lines of code
}
else if(condition2)
{
// Lines of code
}
………
else
{
// Lines of code
}
Example:
package First_Package;
public class First_Program {
int number=5;
if(number > 0)
{
System.out.println(number + " is positive");
}
else if (number < 0)
{
System.out.println(number + " is negative");
}
else
{
System.out.println("The Number is Zero");
}
}
}
if (condition)
{
if (condition)
{
//Lines of code
}
}
b) Switch-case:
• Java has a shorthand for multiple if statement—the switch-case
statement.
• Here is how we can write the above program using a switch-case:
Syntax:
switch (x)
{
case 0:
// Lines of code
doSomething0();
break;
case 1:
// Lines of code
doSomething1();
break;
...
case n:
// Lines of code
doSomethingN();
break;
default:
doSomethingElse();
}
Example:
package First_Package;
public class First_Program {
public static void main(String[] args) {
}
}
}
2. Loops:
• The purpose of loop statements is to execute Java statements many
times.
• There are three types of loops in Java—
a) For,
b) While, and
c) Do-while.
a) For Loop:
• The for loop groups the following three common parts together into
one statement:
a) Initialization
b) Condition
c) Increment or decrement
Syntax:
Example:
b) While Loop:
• The while loop is used to repeatedly execute a block of statements
until the specified condition is true.
• Once the condition becomes false, the loop automatically stops.
Syntax:
while (condition)
{
//code to be executed
Increment / decrement
}
Example:
c) Do-while Loop:
Syntax:
do{
//code to be executed
}while (condition);
Example:
for-each Loop:
• The for-each loop in Java also called the enhanced for loop which
was introduced in Java 5.
• It provides an alternative approach to traverse the array or
collection in Java.
• The advantage of the for-each loop is that it makes the code more
readable.
• The drawback of the enhanced for loop is that it cannot traverse the
elements in reverse order.
• Here, you do not have the option to skip any element because it
does not work on an index basis.
Syntax:
for (type variableName: arrayName)
{
// Statements to repeat
}
Example:
package First_Package;
public class New_class {
}
}
3. Branching Mechanism:
a) break Statement:
• The break statement is used to jump out of a loop.
• In other words, break is used to exit from the loop or switch case.
• When break statement is encountered inside a loop, the loop is
immediately terminated and the Program control moves to the next
statement after the loop.
Syntax:
break;
Example:
package First_Package;
public class First_Program {
}
}
b) Continue Statement:
• Continue statement is used to skip the current iteration and
continues with the next iteration in the loop.
• We can use Java continue statement in all types of loops such as for
loop, while loop and do-while loop.
Syntax:
continue;
Example:
package First_Package;
public class First_Program {