0% found this document useful (0 votes)
2 views

Java Lecture 1

The document provides an overview of Java identifiers, keywords, data types, variables, decision-making statements, and loops. It outlines the rules for naming identifiers, lists reserved words, and explains the types of data and variables in Java. Additionally, it covers control flow mechanisms including if statements, switch cases, and various loop structures.

Uploaded by

Cornelius
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Lecture 1

The document provides an overview of Java identifiers, keywords, data types, variables, decision-making statements, and loops. It outlines the rules for naming identifiers, lists reserved words, and explains the types of data and variables in Java. Additionally, it covers control flow mechanisms including if statements, switch cases, and various loop structures.

Uploaded by

Cornelius
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Java Identifiers

Last Updated : 20 Nov, 2024



An identifier in Java is the name given to Variables, Classes,


Methods, Packages, Interfaces, etc. These are the unique names and
every Java Variables must be identified with unique names.
Example:
public class Test
{
public static void main(String[] args)
{
int a = 20;
}
}

Rules For Naming Java Identifiers


There are certain rules for defining a valid Java identifier. These rules must
be followed, otherwise, we get a compile-time error. These rules are also
valid for other languages like C, and C++.
 The only allowed characters for identifiers are all alphanumeric
characters([A-Z],[a-z],[0-9]), ‘$‘(dollar sign) and ‘_‘ (underscore). For
example “geek@” is not a valid Java identifier as it contains a ‘@’ a
special character.
 Identifiers should not start with digits([0-9]). For example “123geeks” is
not a valid Java identifier.
 Java identifiers are case-sensitive.
 There is no limit on the length of the identifier but it is advisable to use an
optimum length of 4 – 15 letters only.
 Reserved Words can’t be used as an identifier. For example “int while =
20;” is an invalid statement as a while is a reserved word. There
are 53 reserved words in Java.
Reserved Words in Java
Any programming language reserves some words to represent
functionalities defined by that language. These words are called
reserved words. They can be briefly categorized into two
parts: keywords(50) and literals(3). Keywords define functionalities
and literals define value. Identifiers are used by symbol tables in various
analyzing phases(like lexical, syntax, and semantic) of a compiler
architecture.

abstract continue For protected transient

Assert Default Goto public Try

Boolean Do If Static throws

break double implements strictfp Package

byte else import super Private

case enum Interface Short switch


Java Keywords
Last Updated : 09 Dec, 2024



In Java, Keywords are the Reserved words in a programming


language that are used for some internal process or represent some
predefined actions. These words are therefore not allowed to use
as variable names or objects.
Example :
// Java Program to demonstrate Keywords

class GFG
{
public static void main(String[] args)
{
// Using final and int keyword
final int x = 10;

// Using if and else keywords


if(x>10)
{
System.out.println("Failed");
}
Else
{
System.out.println("Succesful Demonstration " +" of keywords");
}
}
}

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

Specifies that a class or method will be


abstract
implemented later, in a subclass

Assert describes a predicate placed in a Java


Assert program to indicate that the developer thinks that
the predicate is always true at that place.

A data type that can hold True and False values


boolean
only

Break A control statement for breaking out of loops.

Byte A data type that can hold 8-bit data values

Case Used in switch statements to mark blocks of text

Catch Catches exceptions generated by try statements

A data type that can hold unsigned 16-bit Unicode


Char
characters

Class Declares a new class

continue Sends control back outside a loop

Specifies the default block of code in a switch


default
statement
Keyword Usage

Do Starts a do-while loop

Java Data Types


Java is statically typed and also a strongly typed language because,
in Java, each type of data (such as integer, character, hexadecimal,
packed decimal, and so forth) is predefined as part of the
programming language and all constants or variables defined for a
given program must be described with one of the Java data types.

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.

2. Non-Primitive Data Type or Object Data type: such as


String, Array, etc.

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.

There are three types of variables in Java – Local, Instance,


and Static.
Example:
int age = 27; // integer variable having value 27

String name = "gfg" // string variable

Types of Java Variables


Now let us discuss different types of variables which are listed as follows:
1. Local Variables
2. Instance Variables
3. Static Variables

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;

// precedence rules for arithmetic operators.


// (* = / = %) > (+ = -)
// prints a+(b/d)
System.out.println("a+b/d = " + (a + b / d));

// if same precedence then associative


// rules are followed.
// e/f -> b*d -> a+(b*d) -> a+(b*d)-(e/f)
System.out.println("a+b*d-e/f = "
+ (a + b * d - e / f));
}
}

Decision Making in Java (if, if-else, switch,


break, continue, jump)


Decision-making statements in Java execute a block of code based on


a condition. Decision-making in programming is similar to decision-
making in real life. In programming, we also face situations where we
want a certain block of code to be executed when some condition is
fulfilled.
A programming language uses control statements to control the flow of
execution of a program based on certain conditions. These are used to
cause the flow of execution to advance and branch based on changes to
the state of a program.

Types of Decision-Making Statements


 if
 if-else
 nested-if
 if-else-if
 switch-case
 jump – break, continue, return

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

Java Switch Case


The switch statement is a multiway branch statement. It provides an easy way to
dispatch execution to different parts of code based on the value of the expression.

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
}

// Java program to demonstrates the working of switch statements


import java.io.*;
class Geeks {
public static void main(String[] args)
{
int num = 20;
switch (num) {
case 5:
System.out.println("It is 5");
break;
case 10:
System.out.println("It is 10");
break;
case 15:
System.out.println("It is 15");
break;
case 20:
System.out.println("It is 20");
break;
default:
System.out.println("Not present");
}
}
}

jump Statements
Java supports three jump statements: break, continue and return.
Java Loops

Looping in programming languages is a feature that facilitates the


execution of a set of instructions/functions repeatedly while some
condition evaluates to true. Java provides three ways for executing the
loops. While all the ways provide similar basic functionality, they differ in
their syntax and condition-checking time.

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

// Java program to demonstrates the working of for loop


import java.io.*;

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.

// Java program to demonstrates the working of for each loop


import java.io.*;

class Geeks
{
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 4, 5 };

for (int i : arr)


{
System.out.print(i + " ");
}
}
}

You might also like