Pages

array in java

array in java :: futureX
In java,array is an object. Array in C & C++ is static but in Java it is dynamic.

1D Array:


Declaration:

datatype  []var = new datatype[size];
                      OR
datatype[]  var = new datatype[size];
                      OR
datatype  var[] = new datatype[size]; 

Example:


class A{
public static void main(String[] args){
int a[]=new int[5];
a[0]=11;
a[2]=33;
a[4]=1;

for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}

Output:(default value is zero)
11
0
33
0
1

Q: Write a program to find minimum from the Array?


class A{
static void printmin(int arr[]){
int min=arr[0];
for(int i=1;i<arr.length;i++)
{
if(min>arr[i])
min=arr[i];
}
System.out.println("min is ::" +min);
}

public static void main(String[] args){
int a[]=new int[5];
a[0]=12;
a[1]=1;
a[2]=22;
a[3]=5;
a[4]=32;

printmin(a);
}}

2D Array:

Declaration:

datatype[][]  var = new datatype[size][size];
                      OR
datatype  [][]var = new datatype[size][size];
                      OR
datatype var[][] = new datatype[size][size];
                      OR
datatype  []var[] = new datatype[size][size];

Example:


class A{
public static void main(String args[]){
int a[][]=new int[3][3];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++){
a[i][j]=i+j;
System.out.println(a[i][j]+" ");
}
System.out.println();
}}}

Anonymous Array:
JAVA feature of array. 
In normal array, as int a[]={} same array can't declared twice. 


class A{
static void printarray(int arr[]){
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
public static void main(String args[]){
printarray(new int[]{14,5,5,8});
printarray(new int[]{14,3,8,8});
}}


                                             JAVA TUTORIALS HOMEPAGE


access modifiers in java

access modifiers in java :: futureX

 There are 4 types of access modifiers:


  1. private
  2. default
  3. protected
  4. public



Private :
The private access modifier is accessible only within class.


example :
class Emp{
private int id;
private void show(){System.out.println(id);}
}

class Test{
public static void main(String args[]){
Emp e=new Emp();
e.id=10;
e.show();
}
}

output:compile time error


Note: A class cannot be private or protected except nested class.


Default :
If you don't use any modifier, it is treated as default modifier by default. The default modifier is accessible only within package.

example:

//save as A.java
package com.futurex;
public class A{
void msg(){System.out.println("hello");}
}

//save as Test.java
package com.techshakti;
import com.futurex.*;
class Test{
public static void main(String args[]){
A a=new A();
a.msg();
}
}

output: msg() is not public in A; cannot be accessed from outside package


Protected :
The protected access modifier is accessible within package and outside the package by only through inheritance.
The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the class.

example:


//save as A.java
package com.futurex;
public class A{
protected void msg(){System.out.println("hello");}
}

//save as Test.java
package com.techshakti;
import com.futurex.*;
class Test extends A{
public static void main(String args[]){
Test t=new Test();
t.msg();
}
}



Public:
The public access modifier is accessible everywhere. It has the widest scope among all other modiers.

example:

//save as A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}


//save as B.java
package mypack;
import pack.*;

class B{
  public static void main(String args[]){
   A obj = new A();
   obj.msg();
  }
}


                  JAVA TUTORIALS HOMEPAGE