[go: up one dir, main page]

0% found this document useful (0 votes)
2 views3 pages

Array in Java

An array in Java is a collection of elements of the same type, stored in contiguous memory locations and indexed starting from zero. The document explains how to declare, instantiate, and initialize an array, along with an example program that finds the maximum number in an array. Additionally, it describes Java Strings, their creation methods, and the difference between using string literals and the new keyword.

Uploaded by

Ankita Joshi
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)
2 views3 pages

Array in Java

An array in Java is a collection of elements of the same type, stored in contiguous memory locations and indexed starting from zero. The document explains how to declare, instantiate, and initialize an array, along with an example program that finds the maximum number in an array. Additionally, it describes Java Strings, their creation methods, and the difference between using string literals and the new keyword.

Uploaded by

Ankita Joshi
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/ 3

Array in Java:

Array is a collection of elements of same type. For example an int


array contains integer elements and a String array contains String
elements.

The elements of Array are stored in contiguous locations in the


memory. Arrays in Java are based on zero-based index system, which
means the first element is at index 0.

This is how an array looks like:

int number[] = new int[10]

Declaration, Instantiation and Initialization of Array in Java


This is how we declare, instantiate and initialize an array. I have
covered this in separate tutorial as well: Declaration and initialization
of an Array.

int number[]; //array declaration


number[] = new int[10]; //array instantiation
number[0] = 10; //array Initialization
number[1] = 20; //array Initialization

Example:

import java.util.Scanner;

public class Maxno {

public static void main(String[] args) {


//int []a = {23,20,10,11,18};
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of an array");
int []a=new int [sc.nextInt()];
int size = a.length;
for(int i=0;i<size;i++)
{
System.out.println("Enter elements in an array");
a[i] = sc.nextInt();
}
int max = a[0];
for(int i=0;i<size;i++) {
if(a[i]>max)
{
max=a[i];
}
}
System.out.println("Enter maxno in an array =>"+max);

}
}

Java Strings:

In Java, a String is the type of object that can store a sequence of


characters enclosed by double quotes, and every character is stored
in 16 bits.
A string acts the same as an array of characters. Java provides a
robust and flexible API for handling strings, allowing for various
operations such as concatenation, comparison, and manipulation.

Ways of Creating a Java String


There are two ways to create a string in Java:
 String Literal
 Using new Keyword

Syntax:

<String_Type> <string_variable> = “<sequence_of_string>”;


1. String literal (Static Memory)
To make Java more memory efficient (because no new objects are
created if it exists already in the string constant pool).

Example:

String demoString = “GeeksforGeeks”;

2. Using new keyword (Heap Memory)

 String s = new String(“Welcome”);

 In such a 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 the heap (non-pool)

You might also like