Lecture 2
Lecture 2
• Variable
• Arrays
• String
Java Variable Types
class B extends A{
public static void main(String args[]){
B obj = new B();
obj.msg();
}
} Output:Hello
public access modifier
• The public access modifier is accessible
everywhere. It has the widest scope among all
other modifiers.
Non Access Modifiers:
Java provides a number of non-access modifiers
to achieve many other functionality.
• The static modifier for creating class methods
and variables
• The final modifier for finalizing the
implementations of classes, methods, and
variables.
• The abstract modifier for creating abstract
classes and methods.
Java Array
• Array is a collection of similar type of elements
that have contiguous memory location.
• Java array is an object that contains elements
of similar data type.
• It is a data structure where we store similar
elements.
• We can store only fixed set of elements in a
java array.
• Array in java is index based, first element of
the array is stored at 0 index.
Advantage of Java Array
• Code Optimization: It makes the code
optimized, we can retrieve or sort the data
easily.
• Random access: We can get any data located
at any index position.
Disadvantage of Java Array
• Size Limit: We can store only fixed size of
elements in the array. It doesn't grow its size
at runtime. To solve this problem, collection
framework is used in java.
Types of Array in java
There are two types of array.
• Single Dimensional Array
• Multidimensional Array
webtech.xcellenttutorial.com
Single Dimensional Array in java
Syntax to Declare an Array in java
dataType[] arr; (or)
dataType arr[];
Instantiation of an Array in java
Array RefVar=new datatype[size];
Example
int a[]=new int[5];
webtech.xcellenttutorial.com
Example of single dimensional java array
class Testarray{
public static void main(String args[]){
int a[]=new int[5];
a[0]=10;
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}} webtech.xcellenttutorial.com
Example of single dimensional java array
class Testarray
{
public static void main(String args[])
{
int size,i;
Scanner r=new Scanner(System.in);
System.out.print(“Enter the size of array”);
size=r.nextInt();
Int a[] =new int[size];
For(i=0;i<size;i++)
{
a[i] =r.nextInt();
}
S.O.P(“printed array elements”)
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
webtech.xcellenttutorial.com
}}
Declaration, Instantiation and Initialization
of Java Array
We can declare, instantiate and initialize the java array
together by:
int a[]={33,3,4,5};//declaration, instantiation and initialization
class Testarray1{
public static void main(String args[])
{
int a[]={33,3,4,5};//declaration, instantiation and initialization
//printing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}
webtech.xcellenttutorial.com
Multidimensional array in java
In such case, data is stored in row and column
based index (also known as matrix form).
Syntax to Declare Multidimensional Array in java.
• dataType[][] arrayRefVar; (or)
• dataType arrayRefVar[][]; (or)
• dataType []arrayRefVar[];
webtech.xcellenttutorial.com
Example to instantiate Multidimensional Array in java
int[][] arr=new int[3][3];//3 row and 3 column
Example to initialize Multidimensional Array in java
arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
arr[1][0]=4;
arr[1][1]=5;
arr[1][2]=6;
arr[2][0]=7;
arr[2][1]=8;
arr[2][2]=9; webtech.xcellenttutorial.com
Example of Multidimensional java array
class Testarray3{
public static void main(String args[]){
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}} webtech.xcellenttutorial.com
class Testarray5{
public static void main(String args[]){
int a[][]={{1,3,4},{3,4,5}};
int b[][]={{1,3,4},{3,4,5}};
int c[][]=new int[2][3];
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();//new line
}
}} webtech.xcellenttutorial.com
Java String
• Generally, String is a sequence of characters. But in Java, string is an object
that represents a sequence of characters. The java.lang.String class is used
to create a string object.
• How to create a string object?
• There are two ways to create String object:
– By string literal
– By new keyword
• 1) String Literal
• Java String literal is created by using double quotes. For Example:
• String s="welcome";
• 2) By new keyword
• String s=new String("Welcome");//
creates two objects and one reference variable
• In such case, JVM will create a new string object in normal (non-pool)
heap memory, and the literal "Welcome" will be placed in the string
constant pool. The variable s will refer to the object in a heap (non-pool).
• webtech.xcellenttutorial.com
Java String Example
// Java Program to demonstrate
// String
public class StringExample {
// Main Function
public static void main(String args[])
{
String str = new String("example");
// creating Java string by new keyword
// this statement create two object i.e
// first the object is created in heap
// memory area and second the object is
// created in String constant pool.
System.out.println(str);
}
webtech.xcellenttutorial.com
}
Java String Example
public class S
{
public static void main(String args[])
{
String a = "example";
System.out.println(a);
String b = "example";
System.out.println(b);
a.concat(“Kumar”);
System.out.println(a);
}
} webtech.xcellenttutorial.com
Java String Example
public class S
{
public static void main(String args[])
{
String a = new String("example");
System.out.println(a);
String b = new String("example");
System.out.println(b);
a.concat(“Kumar”);
System.out.println(a);
}
} webtech.xcellenttutorial.com
Java String Example
public class A
{
public static void main(String args[])
{
String a = "example";
String b = new String("example");
if(a.equals(b))
{
System.out.println(“True”);
}
else
{
System.out.println(“False”);
}
}
webtech.xcellenttutorial.com
}
Java String Example
public class A
{
public static void main(String args[])
{
String a = “ankit";
String b = “rahul";
System.out.println(“a.toLowercase”);
System.out.println(“a.toUppercase”);
System.out.println(“b.length”);
String c = “ ankush ";
String b = “ ";
System.out.println(c.trim());
System.out.println(d.isEmpty”);
System.out.println(b.charAt(2));
System.out.println(a.indexOf(“k”));
System.out.println(b.equals(a));
System.out.println(d.replace(‘r’,’e’);
}
webtech.xcellenttutorial.com
}
Java String Example
public class A
{
public static void main(String args[])
{
String buffer r =new buffer(“Coading in java”)
System.out.println(r.reverse());
}
}
webtech.xcellenttutorial.com