Java M1-P2
Java M1-P2
Module-1
1
Declaring Objects in Java
• Arrays are objects in Java, we can find their length using member length.
• A Java array variable can also be declared like other variables with [] after the data
type.
• The variables in the array are ordered and each have an index beginning from 0.
• Java array can be also be used as a static field, a local variable or a method parameter.
Array in Java
• The size of an array must be specified by an int value and not long or
short.
• The direct superclass of an array type is Object.
Object class is present in java.lang package. Every class in Java is
directly or indirectly derived from the Object class. If a Class does
not extend any other class then it is direct child class
of Object and if extends other class then it is an indirectly derived.
Hence Object class acts as a root of inheritance hierarchy in any Java
Program.
• and implements the Serializable as well as Cloneable interfaces.
Disadvantages
• Size Limit: We can store only the 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 which grows automatically.
class Testarray1{
public static void main(String args[]){
class Testarray{ int a[]={33,3,4,5};
public static void main(String args[]){ for(int i=0;i<a.length;i++)
System.out.println(a[i]);
int a[]=new int[5]; //declaration
}}
a[0]=10;//initialization
a[1]=20;
FOR-EACH Loop
a[2]=70;
a[3]=40; class Testarray1{
a[4]=50; public static void main(String args[])
for(int i=0;i<a.length;i++) {
int arr[]={33,3,4,5};
System.out.println(a[i]);
for(int i:arr)
}} System.out.println(i);
}}
Passing Array to a member Anonymous Array in Java
function • Java supports the feature of an anonymous
array.
• No need need to declare the array while
class Testarray2{ passing an array to the method.
static void min(int arr[]){
int min=arr[0]; public class TestAnonymousArray
for(int i=1;i<arr.length;i++) {
if(min>arr[i]) static void getdata(int arr[]){
min=arr[i]; for(int i=0;i<arr.length;i++)
System.out.println(min); } System.out.println(arr[i]);
public static void main(String args[]) }
{ public static void main(String args[])
int a[]={33,3,4,5}; {
min(a);//passing array to method getdata(new int[]{10,22,44,66});
//passing anonymous array to method
}}
}}
Array Index Out Of Bounds Exception
Java Virtual Machine (JVM) throws an Array Index Out Of Bounds
Exception if length of the array in negative, equal to the array size or
greater than the array size while traversing the array.
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
Somethimes in this code compiler does not accept string. Because when we store
int value like 1 cursor remains after 1 and nextLine() function starts reading the
string from the current position of the cursor, which is just a new line. To overcome
this problem, we can use like this:
int a=Integer.parseInt(obj.nextLine());
2. Using Command Line Argument
class A
{ class Main
public static void main(String args[]) {
{ public static void main(String args[])
{
for(int i=0;i<args.length;i++)
int a=Integer.parseInt(args[0]);
System.out.println(args[i]); int b=Integer.parseInt(args[1]);
} int c=a+b;
System.out.println(c);
} }
}
Javac A. java
Java A 20 30 37
O/P: 20
30
37
Using DataInputStream
import java.io.*;
class Myclass
{
public static void main(String args[])throws IOException
{
DataInputStream obj=new DataInputStream(System.in);
int a,b,c;
System.out.println("Enter any two number");
a=Integer.parseInt(obj.readLine());
b=Integer.parseInt(obj.readLine());
c=a+b;
System.out.println(c);
}
}
BufferedReader class
• BufferedReader is a Java class that reads text from the input stream. It buffers the
characters so that it can get the efficient reading of characters, arrays, etc.
• It inherits the reader class and makes the code efficient since we can read the data
line-by-line with the readline() method.
• We may have to specify the buffer size even though the default is large enough for
any purpose.
Constructor Description
import java.io.*;
public class Main{
Buffer Scanner has little buffer of 1 KB char BufferReader has large buffer of 8KB
2 Memory buffer. byte Buffer as compared to Scanner.
Methods Scanner has methods like nextInt(), BufferReader has methods like
4 nextShort() etc. parseInt(), parseShort() etc.
Read Line Scanner has method nextLine() to BufferReader has method readLine()
5
read a line. to read a line.
19