Java Lecture 1
Java Lecture 1
class GFG
{
public static void main(String[] args)
{
// Using final and int keyword
final int x = 10;
Output
Succesful Demonstration of keywords
Java Keywords List
Java contains a list of keywords or reserved words which are also
highlighted with different colors be it an IDE or editor in order to
segregate the differences between flexible words and reserved
words. They are listed below in the table with the primary action
associated with them.
Keyword Usage
Data types in Java are of different sizes and values that can be
stored in the variable that is made as per convenience and
circumstances to cover up all test cases. Java has two categories in
which data types are segregated
1. Primitive Data Type: such as boolean, char, int, short, byte,
long, float, and double. The Boolean with uppercase B is a
wrapper class for the primitive data type boolean in Java.
Example:
// Java Program to demonstrate int data-type
import java.io.*;
class GFG
{
public static void main (String[] args)
{
// declaring two int variables
int a = 10;
int b = 20;
System.out.println( a + b );
}
}
Output
30
Now, let us explore different types of primitive and non-primitive
data types.
Java Variables
Every variable has a:
Data Type – The kind of data that it can hold. For example, int,
string, float, char, etc.
Variable Name – To identify the variable uniquely within the
scope.
Value – The data assigned to the variable.
Operator Precedence
public class GFG
{
public static void main(String[] args)
{
int a = 20, b = 10, c = 0;
int d = 20, e = 40, f = 30;
1. Java if Statement
The if statement is the most simple decision-making statement. It is used
to decide whether a certain statement or block of statements will be
executed or not i.e. if a certain condition is true then a block of statements is
executed otherwise not.
Syntax:
if(condition)
{
// Statements to execute if
// condition is true
}
Here, the condition after evaluation will be either true or false. if
statement accepts boolean values – if the value is true then it will execute
the block of statements under it. If we don’t use curly braces( {} ), only the
next line after the if is considered as part of the if block
Syntax:
switch (expression)
{
case value1:
// code to be executed if expression == value1
break;
case value2:
// code to be executed if expression == value2
break;
// more cases…
default:
// code to be executed if no cases match
}
jump Statements
Java supports three jump statements: break, continue and return.
Java Loops
In Java, there are three types of Loops which are listed below:
for loop
while loop
do-while loop
1. for loop
The for loop is used when we know the number of iterations (we
know how many times we want to repeat a task). The for statement
consumes the initialization, condition, and increment/decrement in
one line thereby providing a shorter, easy-to-debug structure of
looping.
Syntax:
for (initialization; condition; increment/decrement)
{
// code to be executed
}
Example
class Geeks
{
public static void main(String[] args)
{
for (int i = 0; i <= 10; i++)
{
System.out.print(i + " ");
}
}
}
Enchanced for loop (for each)
This loop is used to iterate over arrays or collections.
Syntax:
for (dataType variable : arrayOrCollection)
{
// code to be executed
}
Example:
The below Java program demonstrates an Enhanced for loop (for each loop) to iterate
through an array and print its elements.
class Geeks
{
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 4, 5 };