UNIT – 2
FEATURES OF
JAVA
CONTENTS
• Data Types
• Variables
• Operators & Expressions
• Decision making &
Branching
• if; if..else; nested if;
switch;
• ?:operator;
• Looping: while; do;
• For loop
• Arrays 2
3
Java Tokens: The tokens are the basic building blocks of the java
language. All the characters of java programs are grouped into
symbols called tokens.
There exists 6 java tokens:
• Identifier
• Keyword
• Separator
• Operators
• Literals
• Comment
4
JAVA TOKENS
Identifiers: they are used to identify variables, methods, classes and objects,
etc,. They are case sensitive.
Keywords: The keywords are words and names reserved for the java language.
Keywords appear in lowercase and cannot be used as identifiers. As of the latest
versions of Java, there are 54 reserved keywords Example: Boolean, break, class,
public, return etc.
Operators: The operators are symbols that represent operations. They operate
on operands and return results.
Separators: The separators are used by the java compiler to divide a program
into segments. Example: “,” “;”and “{ }”.
Literals: The literals are specific types of data explicitly entered into the code.
Comments: The comments are non-executable code.
5
// This is a simple Java program
public class SimpleProgram // Define a public class named SimpleProgram
{
public static void main(String[] args) // Main method - entry point of the program
{
// Declare and initialize variables
int num1 = 10; // integer literal
Int num2=20;
Int sum=num1+num2;// double literal
String message = "Hello, Java!"; // string literal
// Print the values of the variables
System.out.println(“Sum is: " + sum); // Concatenation operator (+)
System.out.println("Message: " + message); // Concatenation operator (+)
}
}
DATA TYPES
INTEGERS
The data types in Java are primarily divided into primitive and non-
primitive:
Primitive data types: Integers; Float; Characters; Boolean.
Integer: Java defines 4 integer types: byte; short; int; long.
Byte: byte is a primitive data type used to store integer values with a
small range.
Short: it is a primitive data type used to store integer values with a
slightly larger range than byte.
Int: it is a primitive data type used to store integer values.
long : it is a primitive data type used to store very large integer values.
Size: long occupies 64 bits (8 bytes) of memory.
8
Data Type Size Range Usage Example
byte 8 bits (1 byte) -128 to 127
Small ranges of
integers, memory-
efficient arrays
byte myByte = 100;
short 16 bits (2 bytes) -32,768 to 32,767
Larger ranges than
byte, more efficient
than int for large
arrays
short myShort =
10000;
int 32 bits (4 bytes)
-2,147,483,648 to
2,147,483,647
Default choice for
integer arithmetic
and calculations
int myInt = 100000;
long 64 bits (8 bytes) - 9,223,372,036,854,7
75,808 to
9,223,372,036,854,7
75,807
Very large integers,
especially useful for
calculations
requiring large
numbers
long myLong =
10000000000L;
10
Float: it is a primitive data type used to store single-precision floating-
point numbers.
Double: it is a primitive data type used to store double-precision
floating-point numbers.
FLOATING POINT TYPES
Data Type Size Range Precision Usage Example
float 32 bits (4
bytes)
Approximately
1.4e-45 to 3.4e+38
~7 decimal
digits
For single-precision
floating-point
numbers, more
memory-efficient
than double
float myFloat =
3.14f;
double 64 bits (8
bytes)
Approximately 4.9e-
324 to 1.8e+308
~15-16
decimal digits
For double-precision
floating-point
numbers, used when
higher precision is
needed
double
myDouble =
3.14159;
11
NON-NUMERIC DATA TYPES
Boolean Data Type- The Boolean data type is used to store only two possible values: true and
false.
This data type is used for simple flags that track true/false conditions.
Char Data Type - The char data type is used to store characters.
Data Type Size Range Precision Usage Example
boolean 1 byte No range; only
true or false
Not applicable For
representing
true/false
values and
making
decisions
boolean isRainy
= false;
char 2 bytes (16 bits) 0 to 65,535
(Unicode
characters)
Precise to a
single character
For storing and
manipulating
individual
characters
char grade = 'A';
VARIABLES
Variable- Variable is name of reserved area allocated in memory. It is a combination
of "vary + able" that means its value can be changed. Thr variables provide
mechanism to store data inside of a program.
Syntax: <datatype> <variablename>;
<datatype> <variablename>=value;
Eg: int data=50;
Types of Variables -There are three types of variables in java:
•local variable
•instance variable
•static variable
12
13
Example:
class A
{
int data=50; //instance variable
static int m=100; //static variable
void method()
{
int n=90; //local variable
}
}
14
TYPE CASTING
Casting is a process of converting one variable of certain data type into another
data type.
Types: Implicit casting & Explicit casting
Implicit casting: Automatic casting done by the java complier internally. It is
done to convert lower data type to higher data type(widening).
Eg: int i;
float f;
i=10;
f=i; //assign an int to float
Explicit casting: The casting is done by the java programmer or developer. It is
used for converting a higher data type to a lower data type(narrowing).
Eg: double d=6.289; double d=6.289;
int i=d; //error int i = int(d); //using explicit cast
OPERATORS
In Java, operators are symbols that perform operations on variables and values. They are
essential for manipulating data and performing calculations.
Unary operators: Unary operators in Java are operators that operate on a single operand.
They are used to perform operations such as incrementing, decrementing, negating, or
inverting values.
15
16
Arithmetic Operators: Arithmetic operators are symbols used in programming to perform
basic mathematical operations on numerical values. In Java, the arithmetic operators allow
you to carry out various calculations, such as addition, subtraction, multiplication, and
more
17
Relational Operators: Relational operators in Java are used to compare two values or
expressions. These operators return a boolean result (true or false) based on the
comparison. They are often used in conditional statements and loops to control the flow of
the program.
18
Assignment Operators: Assignment operators in Java are used to assign values to variables.
The most basic assignment operator is the single equals sign (‘=‘) which assigns the value on
the right to the variable on the left. However, Java also provides several compound
assignment operators that combine assignment with other operations.
19
Logical Operators: Logical operators in Java are used to perform logical operations on
boolean values. They are typically used in conditional statements and control flow constructs,
such as ‘if’,’while’,’for’.
20
Bitwise Operators: A bitwise operator is a type of operator that directly manipulates
bits, the basic units of data in computing, by performing operations on binary
representations of integers. Bitwise operations are commonly used in programming for
tasks such as low-level data manipulation, performance optimization, and implementing
algorithms that require direct access to bits.
21
Shift Operators: In Java, shift operators are the special type of operators that work on the
bits of the data. These operators are used to shift the bits of the numbers from left to right
or right to left depending on the type of shift operator used.
22
Ternary Operators: The ternary operator in Java is a shorthand for the ‘if-else’ statement
and is also known as the conditional operator. It takes three operands and evaluates a
boolean expression, returning one of two values based on the evaluation.
Syntax:
condition ? expression1 : expression2;
condition: A boolean expression that evaluates to true or false.
expression1: The value returned if the condition is true.
expression2: The value returned if the condition is false.
CONTROL STATEMENTS
23
24
• If- statement
The Java if statement is used to test the
condition. It checks the boolean condition:
true or false.
An ‘if’ statement allows you to run a block of
code only if a specified condition is true.
It's a fundamental part of programming that
helps you control the flow of your code.
if(condition)
{
//code to be executed
}
25
• If-else statement
The Java if-else statement is used to execute one
block of code if a condition is true and another block
of code if that condition is false.
It helps you make decisions in your code based on
certain conditions.
if(condition)
{
//code if condition is true
}
else
{
//code if condition is false
}
[Note: We can also use ternary operator (? :) to perform
the task of if...else statement. It is a shorthand way to
check the condition. If the condition is true, the result
of ? is returned. But, if the condition is false, the result of
: is returned.]
26
• If-else-if statement
The if-else-if ladder statement executes one condition
from multiple statements.
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are
false
}
27
28
• Nested if statement
Nested if statements are if statements placed inside
another if statement. Here, the inner if block
condition executes only when outer if block condition
is true.
if(condition)
{
//code to be executed
if(condition)
{
//code to be executed
}
}
29
• Switch statement
A switch statement is a control structure used in many
programming languages to execute different blocks of
code based on the value of a variable or expression.
It is often cleaner and more efficient than using multiple
if-elseif-else statements when you have a variable that
can take on a number of discrete values.
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not
matched;
}
30
LOOPS IN JAVA
31
LOOPS IN JAVA
32
33
public class ForLoopExample
{
public static void main(String[] args)
{
for (int i = 0; i < 5; i++)
{
System.out.println("Iteration: " +
i);
}
}
}
34
35
36
37
38
39
public class WhileExample
{
public static void main(String[] args)
{
int i=1;
while(i<=10)
{
System.out.println(i);
i++;
}
}
}
40
41
public class DoWhileExample
{
public static void main(String[] args)
{
int i=1;
do
{
System.out.println(i);
i++;
}while(i<=10);
}
}
42
LABELED LOOP IN JAVA
43
44
BREAK, CONTINUE
45
BREAK, CONTINUE
46
public class ContinueInForLoop
{
public static void main(String[] args)
{
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0) { // Skip even
numbers
continue; // Skip to the next
iteration
}
System.out.println(i); // Print odd
numbers
}
}
}
Output
1
3
5
7
9
47
ARRAYS
THANK YOU
48

UNIT – 2 Features of java- (Shilpa R).pptx

  • 1.
  • 2.
    CONTENTS • Data Types •Variables • Operators & Expressions • Decision making & Branching • if; if..else; nested if; switch; • ?:operator; • Looping: while; do; • For loop • Arrays 2
  • 3.
    3 Java Tokens: Thetokens are the basic building blocks of the java language. All the characters of java programs are grouped into symbols called tokens. There exists 6 java tokens: • Identifier • Keyword • Separator • Operators • Literals • Comment
  • 4.
    4 JAVA TOKENS Identifiers: theyare used to identify variables, methods, classes and objects, etc,. They are case sensitive. Keywords: The keywords are words and names reserved for the java language. Keywords appear in lowercase and cannot be used as identifiers. As of the latest versions of Java, there are 54 reserved keywords Example: Boolean, break, class, public, return etc. Operators: The operators are symbols that represent operations. They operate on operands and return results. Separators: The separators are used by the java compiler to divide a program into segments. Example: “,” “;”and “{ }”. Literals: The literals are specific types of data explicitly entered into the code. Comments: The comments are non-executable code.
  • 5.
    5 // This isa simple Java program public class SimpleProgram // Define a public class named SimpleProgram { public static void main(String[] args) // Main method - entry point of the program { // Declare and initialize variables int num1 = 10; // integer literal Int num2=20; Int sum=num1+num2;// double literal String message = "Hello, Java!"; // string literal // Print the values of the variables System.out.println(“Sum is: " + sum); // Concatenation operator (+) System.out.println("Message: " + message); // Concatenation operator (+) } }
  • 6.
  • 8.
    INTEGERS The data typesin Java are primarily divided into primitive and non- primitive: Primitive data types: Integers; Float; Characters; Boolean. Integer: Java defines 4 integer types: byte; short; int; long. Byte: byte is a primitive data type used to store integer values with a small range. Short: it is a primitive data type used to store integer values with a slightly larger range than byte. Int: it is a primitive data type used to store integer values. long : it is a primitive data type used to store very large integer values. Size: long occupies 64 bits (8 bytes) of memory. 8
  • 9.
    Data Type SizeRange Usage Example byte 8 bits (1 byte) -128 to 127 Small ranges of integers, memory- efficient arrays byte myByte = 100; short 16 bits (2 bytes) -32,768 to 32,767 Larger ranges than byte, more efficient than int for large arrays short myShort = 10000; int 32 bits (4 bytes) -2,147,483,648 to 2,147,483,647 Default choice for integer arithmetic and calculations int myInt = 100000; long 64 bits (8 bytes) - 9,223,372,036,854,7 75,808 to 9,223,372,036,854,7 75,807 Very large integers, especially useful for calculations requiring large numbers long myLong = 10000000000L;
  • 10.
    10 Float: it isa primitive data type used to store single-precision floating- point numbers. Double: it is a primitive data type used to store double-precision floating-point numbers. FLOATING POINT TYPES Data Type Size Range Precision Usage Example float 32 bits (4 bytes) Approximately 1.4e-45 to 3.4e+38 ~7 decimal digits For single-precision floating-point numbers, more memory-efficient than double float myFloat = 3.14f; double 64 bits (8 bytes) Approximately 4.9e- 324 to 1.8e+308 ~15-16 decimal digits For double-precision floating-point numbers, used when higher precision is needed double myDouble = 3.14159;
  • 11.
    11 NON-NUMERIC DATA TYPES BooleanData Type- The Boolean data type is used to store only two possible values: true and false. This data type is used for simple flags that track true/false conditions. Char Data Type - The char data type is used to store characters. Data Type Size Range Precision Usage Example boolean 1 byte No range; only true or false Not applicable For representing true/false values and making decisions boolean isRainy = false; char 2 bytes (16 bits) 0 to 65,535 (Unicode characters) Precise to a single character For storing and manipulating individual characters char grade = 'A';
  • 12.
    VARIABLES Variable- Variable isname of reserved area allocated in memory. It is a combination of "vary + able" that means its value can be changed. Thr variables provide mechanism to store data inside of a program. Syntax: <datatype> <variablename>; <datatype> <variablename>=value; Eg: int data=50; Types of Variables -There are three types of variables in java: •local variable •instance variable •static variable 12
  • 13.
    13 Example: class A { int data=50;//instance variable static int m=100; //static variable void method() { int n=90; //local variable } }
  • 14.
    14 TYPE CASTING Casting isa process of converting one variable of certain data type into another data type. Types: Implicit casting & Explicit casting Implicit casting: Automatic casting done by the java complier internally. It is done to convert lower data type to higher data type(widening). Eg: int i; float f; i=10; f=i; //assign an int to float Explicit casting: The casting is done by the java programmer or developer. It is used for converting a higher data type to a lower data type(narrowing). Eg: double d=6.289; double d=6.289; int i=d; //error int i = int(d); //using explicit cast
  • 15.
    OPERATORS In Java, operatorsare symbols that perform operations on variables and values. They are essential for manipulating data and performing calculations. Unary operators: Unary operators in Java are operators that operate on a single operand. They are used to perform operations such as incrementing, decrementing, negating, or inverting values. 15
  • 16.
    16 Arithmetic Operators: Arithmeticoperators are symbols used in programming to perform basic mathematical operations on numerical values. In Java, the arithmetic operators allow you to carry out various calculations, such as addition, subtraction, multiplication, and more
  • 17.
    17 Relational Operators: Relationaloperators in Java are used to compare two values or expressions. These operators return a boolean result (true or false) based on the comparison. They are often used in conditional statements and loops to control the flow of the program.
  • 18.
    18 Assignment Operators: Assignmentoperators in Java are used to assign values to variables. The most basic assignment operator is the single equals sign (‘=‘) which assigns the value on the right to the variable on the left. However, Java also provides several compound assignment operators that combine assignment with other operations.
  • 19.
    19 Logical Operators: Logicaloperators in Java are used to perform logical operations on boolean values. They are typically used in conditional statements and control flow constructs, such as ‘if’,’while’,’for’.
  • 20.
    20 Bitwise Operators: Abitwise operator is a type of operator that directly manipulates bits, the basic units of data in computing, by performing operations on binary representations of integers. Bitwise operations are commonly used in programming for tasks such as low-level data manipulation, performance optimization, and implementing algorithms that require direct access to bits.
  • 21.
    21 Shift Operators: InJava, shift operators are the special type of operators that work on the bits of the data. These operators are used to shift the bits of the numbers from left to right or right to left depending on the type of shift operator used.
  • 22.
    22 Ternary Operators: Theternary operator in Java is a shorthand for the ‘if-else’ statement and is also known as the conditional operator. It takes three operands and evaluates a boolean expression, returning one of two values based on the evaluation. Syntax: condition ? expression1 : expression2; condition: A boolean expression that evaluates to true or false. expression1: The value returned if the condition is true. expression2: The value returned if the condition is false.
  • 23.
  • 24.
    24 • If- statement TheJava if statement is used to test the condition. It checks the boolean condition: true or false. An ‘if’ statement allows you to run a block of code only if a specified condition is true. It's a fundamental part of programming that helps you control the flow of your code. if(condition) { //code to be executed }
  • 25.
    25 • If-else statement TheJava if-else statement is used to execute one block of code if a condition is true and another block of code if that condition is false. It helps you make decisions in your code based on certain conditions. if(condition) { //code if condition is true } else { //code if condition is false } [Note: We can also use ternary operator (? :) to perform the task of if...else statement. It is a shorthand way to check the condition. If the condition is true, the result of ? is returned. But, if the condition is false, the result of : is returned.]
  • 26.
    26 • If-else-if statement Theif-else-if ladder statement executes one condition from multiple statements. if(condition1){ //code to be executed if condition1 is true }else if(condition2){ //code to be executed if condition2 is true } else if(condition3){ //code to be executed if condition3 is true } ... else{ //code to be executed if all the conditions are false }
  • 27.
  • 28.
    28 • Nested ifstatement Nested if statements are if statements placed inside another if statement. Here, the inner if block condition executes only when outer if block condition is true. if(condition) { //code to be executed if(condition) { //code to be executed } }
  • 29.
    29 • Switch statement Aswitch statement is a control structure used in many programming languages to execute different blocks of code based on the value of a variable or expression. It is often cleaner and more efficient than using multiple if-elseif-else statements when you have a variable that can take on a number of discrete values. switch(expression){ case value1: //code to be executed; break; //optional case value2: //code to be executed; break; //optional ...... default: code to be executed if all cases are not matched; }
  • 30.
  • 31.
  • 32.
  • 33.
    33 public class ForLoopExample { publicstatic void main(String[] args) { for (int i = 0; i < 5; i++) { System.out.println("Iteration: " + i); } } }
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
    39 public class WhileExample { publicstatic void main(String[] args) { int i=1; while(i<=10) { System.out.println(i); i++; } } }
  • 40.
  • 41.
    41 public class DoWhileExample { publicstatic void main(String[] args) { int i=1; do { System.out.println(i); i++; }while(i<=10); } }
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
    46 public class ContinueInForLoop { publicstatic void main(String[] args) { for (int i = 1; i <= 10; i++) { if (i % 2 == 0) { // Skip even numbers continue; // Skip to the next iteration } System.out.println(i); // Print odd numbers } } } Output 1 3 5 7 9
  • 47.
  • 48.