Java Api
Java Api
Flow Control“
• Creating and using if, if-else, ternary, and switch constructs
• Creating and using loops: while, do-while, for, and enhanced for
• Creating nested constructs for selection and iteration statements
• Comparing the do-while, while, for, and enhanced for loop constructs
• Using break and continue statements
Prof. Nilesh Gambhava
Computer Engineering Department,
Darshan Institute of Engineering & Technology, Rajkot
Java API
Introduction to JAVA API(Application Programming Interface):
JDK is for development purpose whereas JRE is for running the java programs.
Introduction to JAVA API(Application Programming Interface):
JDK is for development purpose whereas JRE is for running the java programs.
Introduction to JAVA API(Application Programming Interface):
The Java API is a set of classes, interfaces, and methods provided by the Java
When you refer to "Selected classes from the Java API," it could mean any classes
Some commonly used packages and classes from the Java API include:
java.lang: Provides fundamental classes such as Object, String, and basic data types.
java.util: Offers collections (ArrayList, HashMap, etc.), date and time utilities, and other utility
classes.
When working with the Java API, developers can import these classes and use their methods to build
applications.
Introduction to JAVA API
Flow Control“
• Creating and using if, if-else, ternary, and switch constructs
• Creating and using loops: while, do-while, for, and enhanced for
• Creating nested constructs for selection and iteration statements
• Comparing the do-while, while, for, and enhanced for loop constructs
• Using break and continue statements
Creating and manipulating String and StringBuilder objects
In Java, the String class is used to represent sequences of characters, and it is immutable,
meaning once a String object is created, its content cannot be changed. On the other hand,
the StringBuilder class is mutable, allowing you to modify the content of the string without
creating a new object each time.
a comparison of the String reference variables str1 and str2 prints false.
The operator == compares the addresses of the objects referred to by the variables str1 and
str2. Even though these String objects store the same sequence of characters, they refer to
separate objects stored at separate locations
String str3 = "Harry";
String str4 = "Harry";
System.out.println(str3 == str4);
// Creating a String
String str1 = "Harry";
// Concatenating Strings
String str2 = " Paul";
String result1 = str1 + str2; // Concatenation using the + operator
// Substring
String substring = str1.substring(7); // Extracts substring starting from index
7
// Comparing Strings
boolean isEqual = str1.equals("Hello, World!");
2. Performance:
•String:
•Since strings are immutable, concatenating or modifying strings using the + operator or methods like concat() or substring()
creates new string objects.
•This can be inefficient in terms of memory and performance, especially when dealing with a large number of string modifications.
•StringBuilder:
•StringBuilder is more efficient for building and modifying strings because it allows in-place modifications.
•It avoids the creation of unnecessary intermediate string objects, making it faster and more memory-efficient when dealing with
concatenation or modifications.
Creating StringBuilder objects
class CreateStringBuilderObjects
{
public static void main(String args[])
{
StringBuilder sb1 = new StringBuilder //no characters in it and an initial capacity of 16 characters
StringBuilder sb2 = new StringBuilder(sb1); //same as sb1
StringBuilder sb3 = new StringBuilder(50); //object with no characters and an initial capacity of 50 characters.
StringBuilder sb4 = new StringBuilder("Shreya Gupta"); //object sb4 with the value Shreya Gupta.
}
}
public class Main{ public static void main(String[] args)
{
// Creating a StringBuilderStringBuilder stringBuilder = new StringBuilder("Hello");
System.out.println(stringBuilder);
int rollno[];
rollno = new int[100]
String objArray[] = new String[] {"Harry", "Shreya", "Paul", "Selvan"}; //Array of objects
}
}
Types of Array
Creating an array involves three steps, as follows:
• Declaring the array
• Allocating the array
• Initializing the array elements
public class ArrayExample {
ArrayList is one of the most widely used classes from the Collections
framework. It offers the best combination of features offered by an array
and the List data structure. The most commonly used operations with a list
are add items to a list, modify items in a list, delete items from a list, and
iterate over the items.
Flow control
I’ll cover the following topics in this chapter:
Creating and using if, if-else, ternary, and switch constructs to execute statements
selectively
Creating and using loops: while, do-while, for, and enhanced for
Creating nested constructs for selection and iteration statements
Comparing the do-while, while, for, and enhanced for loop constructs
Using break and continue statements
The if construct and its flavors
An if construct enables you to execute a set of statements in your code based on
the result of a condition. This condition must always evaluate to a boolean or a
Boolean value. You can specify a set of statements to execute when this condition
evaluates to true or
“Hello” 5
Output
System.out.println("Hello\n");
Hello
System.out.println("Hello\n"); loop(condition)
Hello {
System.out.println("Hello\n"); //statements
Hello
System.out.println("Hello\n"); }
Hello
System.out.println("Hello\n");
Hello
if v/s while
False False
condition condition
True True
… …
Looping or Iterative Statements
Looping Statements are
Entry Controlled Loop: while, for
Exit Controlled Loop: do…while
Virtual Loop: goto
While loop
While Loop
while is an entry controlled loop
Statements inside the body of while are repeatedly executed till the condition is
true
while is keyword
Syntax
while(condition)
{
// Body of the while
// true part
}
for loop
for Loop
for is an entry controlled loop
Statements inside the body of for are repeatedly executed till the condition is true
for is keyword
Syntax
for (initialization; condition; updateStatement)
{
// statements
}
do while loop
do while Loop
do while is an exit controlled loop.
Statements inside the body of do while are repeatedly executed till the condition
is true.
Do and while are keywords. Syntax
do
{
// statement
}
while (condition);
goto statement
goto Statement
goto is an virtual loop
The goto statement allows us to transfer control of the program to the specified
label.
goto is keyword
Syntax Syntax
goto label; label:
. .
. .
. .
label: goto label;
The label is an identifier. When the goto statement is encountered, the control of
the program jumps to label: and starts executing the code.
Thank you
patidar.gopal27@gmail.com