answersLogoWhite

0


Best Answer

If you refering to Object then String[] something=new String[2]; Here you have to remember that something only allocated space for 2 String object but it did not created them yet. By default Objects are intantiated to null so if you try to print the content of an array System.out.println(something[0]);//print null System.out.println(something[0].toLowerCase()); Throws NullPointerException Couple other ways to create Arrays In java String[] something=new String[]{"Me","You"}; String[] something={"Me", "You"};

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

// Declaring an array

int[] a;

// Instantiating an array

a = new int[10];

// Initializing an array

for(int i = 0; i < a.length; ++i) {

a[i] = 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Creating an array of Object in Java

SYNTAX:

class_name[] obj_name=new class_name[obj_size];

Here,

class_name: it is a valid java identifier

obj_name: it is a valid java identifier

new: Key word

obj_size: number of objetcts

Example:

student[] stud=new student[5];

Explanation:

when this statement is encountered it will create array of 5 elements of the type of class.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Yes, in Java you can create an array of objects.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

int a[10]={'s','a'};

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can you initialize an array in Java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How does one initialize a byte array in Java?

One can get information about how to initialize a byte array in java on the website stackoverflow dot com. That website can learn one a lot about java.


Can you initialize an object without constructor in java?

No. if you wish to create an object that you plan on using in a java program then the answer is NO. You cannot initialize an object of a Java class without calling the constructor.


Write a java program to initialize array of string with the days of the week?

final String[] days = new String[] {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};


How do you use arrays in biginteger?

In Java, you would create an array of type BigInteger, then initialize each object (each array element) with the newoperator. I believe it would be something like this: BigInteger[] myArray = new BigInteger[5]; for (int i = 0; i


How do you initialize an array variable in java program?

An array in java is a collection of items stored into a single unit. The array has some number of slots (elements), each slot in the array can hold an object or a primitive value. Arrays in java are objects that can be treated just like other objects in the languageArrays can contain any type of element value , but we can't store different types in a single array. We can have an array of integers or an array of strings or an array of arrays.To create an array in java ,use three steps1. Declare a variable to hold the array2. Create a new array object and assign it to the array variable3. Store things in that array

Related questions

How does one initialize a byte array in Java?

One can get information about how to initialize a byte array in java on the website stackoverflow dot com. That website can learn one a lot about java.


How do you initialize variable?

initialize simple types: int i = 0; initialize objects: Object o = null; (in java)


Can you initialize an object without constructor in java?

No. if you wish to create an object that you plan on using in a java program then the answer is NO. You cannot initialize an object of a Java class without calling the constructor.


What are benefits of array in java?

array example in java


What is an error in java?

An error or more commonly known as an Exception is a situation where the java program behaves in a way it is not supposed to do so. It is a problem in the code that is causing the JVM to terminate or throw error messages in the console. Ex: When you initialize an array list with 10 elements and try to access the 11th element in the array list you will get an array index out of bounds exception.


Write a java program to initialize array of string with the days of the week?

final String[] days = new String[] {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};


How do you use arrays in biginteger?

In Java, you would create an array of type BigInteger, then initialize each object (each array element) with the newoperator. I believe it would be something like this: BigInteger[] myArray = new BigInteger[5]; for (int i = 0; i


How do you initialize an array variable in java program?

An array in java is a collection of items stored into a single unit. The array has some number of slots (elements), each slot in the array can hold an object or a primitive value. Arrays in java are objects that can be treated just like other objects in the languageArrays can contain any type of element value , but we can't store different types in a single array. We can have an array of integers or an array of strings or an array of arrays.To create an array in java ,use three steps1. Declare a variable to hold the array2. Create a new array object and assign it to the array variable3. Store things in that array


Could array may have elements that are numbers and strings?

In Java:Not as primitives; but I believe you could create an array of Objects, and then initialize the elemnets as subtypes of the Object class, i.e., any class. I don't think this would be very practical (in Java); if (for example) you need to store information about people's names with their ages, create a class called "Person" that has those two attributes, then create an array of Persons.In Java:Not as primitives; but I believe you could create an array of Objects, and then initialize the elemnets as subtypes of the Object class, i.e., any class. I don't think this would be very practical (in Java); if (for example) you need to store information about people's names with their ages, create a class called "Person" that has those two attributes, then create an array of Persons.In Java:Not as primitives; but I believe you could create an array of Objects, and then initialize the elemnets as subtypes of the Object class, i.e., any class. I don't think this would be very practical (in Java); if (for example) you need to store information about people's names with their ages, create a class called "Person" that has those two attributes, then create an array of Persons.In Java:Not as primitives; but I believe you could create an array of Objects, and then initialize the elemnets as subtypes of the Object class, i.e., any class. I don't think this would be very practical (in Java); if (for example) you need to store information about people's names with their ages, create a class called "Person" that has those two attributes, then create an array of Persons.


Why can't java compiler initialize local variables?

Its not that the compiler can't initialize local variables; its that the compiler does not initialize local variables.This is by design and language specification. If you want to initialize local variables, you must explicitly do so.


Which condition is not necessary in dynamic stack?

in dynamic stack we don't have to initialize the size of array while in static stack we have 2 initialize it ......


How you can initialize an array with data values during declaration?

int myArray[] = {1,2,3,4,5};