0% found this document useful (0 votes)
4 views8 pages

Ch7array and Stringinjava

The document provides an overview of Java arrays, including single and multidimensional arrays, their syntax, and examples of declaration, instantiation, and traversal. It also covers Java Strings, Vectors, and wrapper classes, explaining how to create and manipulate these data structures and their conversions between primitives and objects. Key concepts such as autoboxing and unboxing are highlighted, along with practical code examples.

Uploaded by

yadavvicky0859
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

Ch7array and Stringinjava

The document provides an overview of Java arrays, including single and multidimensional arrays, their syntax, and examples of declaration, instantiation, and traversal. It also covers Java Strings, Vectors, and wrapper classes, explaining how to create and manipulate these data structures and their conversions between primitives and objects. Key concepts such as autoboxing and unboxing are highlighted, along with practical code examples.

Uploaded by

yadavvicky0859
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Java Arrays

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.

There are two types of array.

o Single Dimensional Array


o Multidimensional Array

Single Dimensional Array in Java


Syntax to Declare an Array in Java

1. dataType arr[];

Instantiation of an Array in Java

1. arrayRefVar=new datatype[size];

Example of Java Array


Let's see the simple example of java array, where we are going to declare, instantiate,
initialize and traverse an array.

1. //Java Program to illustrate how to declare, instantiate, initialize


2. //and traverse the Java array.
3. class Testarray{
4. public static void main(String args[]){
5. int a[]=new int[5];//declaration and instantiation
6. a[0]=10;//initialization
7. a[1]=20;
8. a[2]=70;
9. a[3]=40;
10. a[4]=50;
11. //traversing array
12. for(int i=0;i<a.length;i++)//length is the property of array
13. System.out.println(a[i]);
14. }}

Output
10
20
70
40
50

Multidimensional Array in Java


In such case, data is stored in row and column based index (also known as matrix form).

Example to instantiate Multidimensional Array in Java

1. int[][] arr=new int[3][3];//3 row and 3 column

2. import java.util.Scanner;
3. publicclass TwoDArray {
4. publicstaticvoid main(String[] args) {
5. int[][] arr = newint[3][3];
6. Scanner sc = new Scanner(System.in);
7. for (inti =0;i<3;i++)
8. {
9. for(intj=0;j<3;j++)
10. {
11. System.out.print("Enter Element");
12. arr[i][j]=sc.nextInt();
13. System.out.println();
14. }
15. }
16. System.out.println("Printing Elements...");
17. for(inti=0;i<3;i++)
18. {
19. System.out.println();
20. for(intj=0;j<3;j++)
21. {
22. System.out.print(arr[i][j]+"\t");
23. }
24. }
25. }
26. }

String in Java
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.

There are two ways to create String object:

1. By string literal
2. By new keyword

String Literal
Java String literal is created by using double quotes. For Example:

1. String s="welcome";

By new keyword
1. String s=new String("Welcome");//creates two objects

Java Vector
Vector is like the dynamic array which can grow or shrink its size. Unlike array, we can store n-
number of elements in it as there is no size limit. It is a part of Java Collection framework since
Java 1.2. It is found in the java.util package and implements the List interface, so we can use all
the methods of List interface here.

import java.util.*;
public class VectorExample1

public static void main(String args[]) {

//Create an empty vector with initial capacity 4


Vector<String> vec = new Vector<String>(4);
//Adding elements to a vector
vec.add("Tiger");
vec.add("Lion");
vec.add("Dog");
vec.add("Elephant");
//Check size and capacity
System.out.println("Size is: "+vec.size());
System.out.println("Default capacity is: "+vec.capacity());
//Display Vector elements
System.out.println("Vector element is: "+vec);
vec.addElement("Rat");
vec.addElement("Cat");
vec.addElement("Deer");
//Again check size and capacity after two insertions
System.out.println("Size after addition: "+vec.size());
System.out.println("Capacity after addition is: "+vec.capacity());
//Display Vector elements again
System.out.println("Elements are: "+vec);
//Checking if Tiger is present or not in this vector
if(vec.contains("Tiger"))
{
System.out.println("Tiger is present at the index " +vec.indexOf("Tiger"));
}
else
{
System.out.println("Tiger is not present in the list.");
}
//Get the first element
System.out.println("The first animal of the vector is = "+vec.firstElement());
//Get the last element
System.out.println("The last animal of the vector is = "+vec.lastElement());
}
}

Output:

Size is: 4
Default capacity is: 4
Vector element is: [Tiger, Lion, Dog, Elephant]
Size after addition: 7
Capacity after addition is: 8
Elements are: [Tiger, Lion, Dog, Elephant, Rat, Cat, Deer]
Tiger is present at the index 0
The first animal of the vector is = Tiger
The last animal of the vector is = Deer

Wrapper classes in Java


The wrapper class in Java provides the mechanism to convert primitive into object and
object into primitive.

Since J2SE 5.0, autoboxing and unboxing feature convert primitives into objects and
objects into primitives automatically. The automatic conversion of primitive into an object
is known as autoboxing and vice-versa unboxing.

The eight classes of the java.lang package are known as wrapper classes in Java. The list
of eight wrapper classes are given below:
Primitive Type Wrapper class

boolean Boolean

char Character

byte Byte

short Short

int Integer

long Long

float Float

double Double

Wrapper class Example: Primitive to Wrapper

1. public class WrapperExample1{


2. public static void main(String args[]){
3. //Converting int into Integer
4. int a=20;
5. Integer i=Integer.valueOf(a);//converting int into Integer explicitly
6. Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally
7.
8. System.out.println(a+" "+i+" "+j);
9. }}

Output:

20 20 20
Wrapper class Example: Wrapper to Primitive

1. //Java program to convert object into primitives


2. //Unboxing example of Integer to int
3. public class WrapperExample2{
4. public static void main(String args[]){
5. //Converting Integer to int
6. Integer a=new Integer(3);
7. int i=a.intValue();//converting Integer to int explicitly
8. int j=a;//unboxing, now compiler will write a.intValue() internally
9.
10. System.out.println(a+" "+i+" "+j);
11. }}

Output:

3 3 3
1. wrapper objects and vice-versa
2. public class WrapperExample3{
3. public static void main(String args[]){
4. byte b=10;
5. short s=20;
6. int i=30;
7. long l=40;
8. float f=50.0F;
9. double d=60.0D;
10. char c='a';
11. boolean b2=true;
12.
13. //Autoboxing: Converting primitives into objects
14. Byte byteobj=b;
15. Short shortobj=s;
16. Integer intobj=i;
17. Long longobj=l;
18. Float floatobj=f;
19. Double doubleobj=d;
20. Character charobj=c;
21. Boolean boolobj=b2;
22.
23. //Printing objects
24. System.out.println("---Printing object values---");
25. System.out.println("Byte object: "+byteobj);
26. System.out.println("Short object: "+shortobj);
27. System.out.println("Integer object: "+intobj);
28. System.out.println("Long object: "+longobj);
29. System.out.println("Float object: "+floatobj);
30. System.out.println("Double object: "+doubleobj);
31. System.out.println("Character object: "+charobj);
32. System.out.println("Boolean object: "+boolobj);
33.
}

Printing object values---


Byte object: 10
Short object: 20
Integer object: 30
Long object: 40
Float object: 50.0
Double object: 60.0
Character object: a
Boolean object: true

You might also like