Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

SPPU Principles of Programming Notes

(Lecture 6)

Fundamentals of JAVA
Java is a high-level, general-purpose, object-oriented, and secure programming
language developed by James Gosling at Sun Microsystems, Inc. in 1991. It is
formally known as OAK. In 1995, Sun Microsystem changed the name to Java.
In 2009, Sun Microsystem takeover by Oracle Corporation.

Editions of Java
Each edition of Java has different capabilities. There are three editions of Java:

o Java Standard Editions (JSE): It is used to create programs for a desktop


computer.
o Java Enterprise Edition (JEE): It is used to create large programs that
run on the server and manages heavy traffic and complex transactions.
o Java Micro Edition (JME): It is used to develop applications for small
devices such as set-top boxes, phone, and appliances.

Types of Java Applications


There are four types of Java applications that can be created using Java
programming:

o Standalone Applications: Java standalone applications uses GUI


components such as AWT, Swing, and JavaFX. These components contain
buttons, list, menu, scroll panel, etc. It is also known as desktop alienations.
o Enterprise Applications: An application which is distributed in nature is
called enterprise applications.
o Web Applications: An application that run on the server is called web
applications. We use JSP, Servlet, Spring, and Hibernate technologies for
creating web applications.
o Mobile Applications: Java ME is a cross-platform to develop mobile
applications which run across smartphones. Java is a platform for App
Development in Android.
PPL NOTES YOUTUBE CHANNEL- UR ENGINEERING FRIEND
Array
Normally, an array is a collection of similar type of elements which has
contiguous memory location.

Java array is an object which contains elements of a similar data type.


Additionally, the elements of an array are stored in a contiguous memory
location. It is a data structure where we store similar elements. We can store only
a fixed set of elements in a Java array.

In Java, array is an object of a dynamically generated class. Java array inherits


the Object class, and implements the Serializable as well as Cloneable interfaces.
We can store primitive values or objects in an array in Java. Like C/C++, we can
also create single dimensional or multidimensional arrays in Java.

Types of Arrays in java


There are two types of arrays.

o One Dimensional Array


o Multidimensional Array

1) One Dimensional Array

o It is a collection of variables of same type which is used by a common


name.
Examples:
One dimensional array declaration of variable:
PPL NOTES YOUTUBE CHANNEL- UR ENGINEERING FRIEND
o import java.io. *;
o
o class GFG {
o public static void main (String[] args)
o {
o int [] a; // valid declaration
o int b []; // valid declaration
o int [] c; // valid declaration
o }
o }
We can write it in any way.
Now, if you declare your array like below:

import java.io. *;

class GFG {
public static void main(String[] args)
{
// invalid declaration -- If we want to assign
// size of array at the declaration time, it
// gives compile time error.
int a[5];

// valid declaration
int b[];
}
}

Two-Dimensional Array

Suppose, you want to create two-dimensional array of int type data. So you
can declare two dimensional array in many of the following ways:
Example:
import java.io. *;

class GFG {
public static void main (String[] args)
{

PPL NOTES YOUTUBE CHANNEL- UR ENGINEERING FRIEND


int a [][]; // valid
int[][] b; // valid
int[][] c; // valid
int[] d[]; // valid
int[][] e; // valid
int[] f[]; // valid
[][] int g; // invalid
[] int[] h; // invalid
}
}

String Handling: String class methods


A string is a collection of characters. In Java, a string is an object that represents
a collection of objects. A string is a predefined class used to create string objects.
It is an immutable object, which means it cannot be updated once created.
The string class has a set of built-in-methods, defined below.

• char At (): It returns a character at a specified position.


• equals (): It compares the two given strings and returns a Boolean, that
is, True or False.
• concept (): Appends one string to the end of another.
• length (): Returns the length of a specified string.
• to Lowercase(): Converts the string to lowercase letters.
• to Uppercase (): Converts the string to uppercase letters.
• index Of(): Returns the first found position of a character.
• substring (): Extracts the substring based on index values, passed as an
argument.

Example:

• class Main{
• public static void main(String []args)
• {
• String s1="Adithya";
• String s2="Adithya";
• String s3="Adi";
• boolean x=s1.equals(s2);
• System.out.println("Compare s1 and s2:"+x);
• System.out.println("Character at given position is:"+s1.charAt(5));
• System.out.println(s1.concat(" the author"));

PPL NOTES YOUTUBE CHANNEL- UR ENGINEERING FRIEND


• System.out.println(s1.length());
• System.out.println(s1.toLowerCase());
• System.out.println(s1.toUpperCase());
• System.out.println(s1.indexOf('a'));
• System.out.println(s1.substring(0,4));
• System.out.println(s1.substring(4));
• }
• }

Code explanation
• Lines 4-6: We create three strings s1, s2, and s3.
• Line 7: We compare two strings s1 and s2 using equals () function.
• Line 9: We find the character at position 5 in string s1 using char
At() function and print it.
• Line 10: We concatenate two strings using the concept () function.
• Line 11: We find the length of string s1 using length () function.
• Line 12: We convert the string s1 to lowercase letters using to
Lowercase() function.
• Line 13: We convert the string s1 to uppercase letters using to
Uppercase() function.
• Line 14: We find the index of a in string s1 using index Of() function.
• Lines 15-16: We find the substring by passing indexes as parameters to
the substring () function.

PPL NOTES YOUTUBE CHANNEL- UR ENGINEERING FRIEND


PPL NOTES YOUTUBE CHANNEL- UR ENGINEERING FRIEND

You might also like