Set2 JAVA Q&A
Set2 JAVA Q&A
A Java variable is a piece of memory that can contain a data value. A variable thus has a data
type. A data type is a classification of data, which can store a specific type of information.
An array is a data structure that contains a group of elements. Typically these elements are all of
the same data type, such as an integer or string. Arrays are commonly used in computer
programs to organize data so that a related set of values can be easily sorted or searched.
For example, a search engine may use an array to store Web pages found in a search performed
by the user. When displaying the results, the program will output one element of the array at a
time. This may be done for a specified number of values or until all the values stored in the array
have been output. While the program could create a new variable for each result found, storing
the results in an array is much more efficient way to manage memory.
Arrays can have more than one dimension. A one-dimensional array is called a vector ; a two-
dimensional array is called a matrix.
One dimensional array is a list of variables of same type that are accessed by a common
name. An individual variable in the array is called an array element. Arrays forms a way
to handle groups of related data.
type varName[];
Where type is valid data type in java and varName is a name of an array.
To allocate space for an array element use below general form or syntax.
Where varName is the name of the array and type is a valid java type and size specifies
the number of elements in the array.
Above statement will create an integer of an array with ten elements that can be
accessed by arr.
Structure of one dimensional array
arr[0]
arr[1]
arr[2]
arr[3]
arr[4]
arr[5]
arr[6]
arr[7]
arr[8]
arr[9]
Note that array indexes begins with zero. That means if you want to access 1st element
of an array use zero as an index. To access 3rd element refer below example.
Where varName is the name of the array and type is a valid data type and v0, v1 to v10
are the elements of an array. In this case new is not used and memory for the array is
automatically provided.
Example : This example shows how to declare initialize and display an array.
// Initializing elements
iarr[0] = 1;
iarr[1] = 2;
iarr[2] = 3;
Multi dimensional arrays are nothing but arrays of arrays. You can create arrays of two or more
dimensions.
type varName[][];
Where type is valid data type in java and varName is a name of an array. Note that the two sets of
brackets indicates that there are two dimensions for this array.
To allocate space for multi dimensional array element use below general form or syntax.
Where varName is the name of the array and type is a valid java type and dimensions are
specified by size1 and size2 in the array.
Above statement will create 2*3 element array of an integer values that can by accessed by iarr.
Note that array index begins with zero. That means if you want to access 1st element of an array
use zero as an index. To access 3rd element refer below example.
java also allows an abbreviated syntax to declare an array. General form or syntax of is as shown
below.
Where varName is the name of the array and type is a valid data type and v00, v01 to v12 are the
elements of an array. In this case new is not used and memory for the array is automatically
provided.
Example : This example shows how to declare initialize and display multi dimensional array.
// Initializing elements
iarr[0][0] = 1;
iarr[0][1] = 2;
iarr[0][2] = 3;
iarr[1][0] = 4;
iarr[1][1] = 5;
iarr[1][2] = 6;
String class is encapsulated under java.lang package. In java, every string that you create is actually
an object of type String. One important thing to notice about string object is that string objects are
immutable that means once a string object is created it cannot be altered.
String can be created in number of ways; here are a few ways of creating string object.
String literal is a simple string enclosed in double quotes " ". A string literal is treated as a String
object.
5. What are the different methods used in String, name any few?
The following methods are some of the most commonly used methods of String class.
charAt() - charAt() function returns the character located at the specified index.
String str = "Testing";
System.out.println(str.charAt(2));
Output: s
Output: true
Output: 8
replace() - replace() method replaces occurances of character with a specified new character.
Output : replace
substring() - substring() method returns a part of the string. substring() method has two
forms,
public String substring(int begin);
But, if the call to substring() method has two arguments, the second argument specify the end point of
substring.
Output : 456789
System.out.println(str.substring(4,7));
Output : 456
Output : abcdef
toUpperCase() - This method returns string with all lowercase character changed to
uppercase.
System.out.println(str.toUpperCase());
Output : ABCDEF
trim() - This method returns a string from which any leading and trailing whitespaces has been
removed.
System.out.println(str.trim());
Output : hello
6. How to define Method in Java?
Method describes behaviour of an object. A method is a collection of statements that are group
together to perform an operation.
Syntax:
//body of method
Return Type: A method may return value. Data type of value return by a method is declared in
method heading.