Array Android
Array Android
IT LEARNING CENTER
Java Array
Overview
HALTEGH
IT LEARNING CENTER
Java Array
● Array adalah suatu koleksi , mirip dengan elemen yang memiliki memeori yang
terus bertambah.
● Java array adalah objek yang berisikan elemen data yang sama semua tipenya .
Nilai datanya diset atau ditentukan secara manual oleh kita
● Array dijava disusun berdasarkan antrian yang disebut dengan index
HALTEGH
IT LEARNING CENTER
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}}
What is the class name of java array?
In java, array is an object. For array object, an proxy class is created whose
name can be obtained by getClass().getName() method on the object.
class Testarray4{
public static void main(String args[]){
int arr[]={4,4,5};
Class c=arr.getClass();
String name=c.getName();
System.out.println(name);
}}
Copying a java array
● We can copy an array to another by the arraycopy method of System class.
class evaluate
{
public static void main(String args[])
{
int arr[] = new int[] {0 , 1, 2, 3, 4, 5,
6,7, 8, 9};
int n = 6;
n = arr[arr[n] / 2];
System.out.println(arr[n] / 2);
}
}
What is the output?
class multidimention_array
{
public static void main(String args[])
{
int arr[][] = new int[3][];
arr[0] = new int[1];
arr[1] = new int[2];
arr[2] = new int[3];
int sum = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < i + 1; ++j)
arr[i][j] = j + 1;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < i + 1; ++j)
sum + = arr[i][j];
System.out.print(sum);
}
HALTEGH
IT LEARNING CENTER
Java Interface
Java Interface
● An interface in java adalah blueprint dari suatu kelas. Dapat
memiliki satic constant dan abstract method .
● Interface di java adalah a mechanism untuk mencapai abstraction.
Hanya boleh ada abstract methods in di interface bukan di body
method. Digunakan untuk mencapai abstraction dan multiple
inheritance di Java.
● Java Interface juga representasi IS-A suatu hubungan.
● Tidak bisa diinisiasi seperti kelas abstract.
Kenapa menggunakan Java interface?
There are mainly three reasons to use interface. They are given below.
3) Abstract class can have final, non-final, Interface has only static and final variables.
static and non-static variables.
4) Abstract class can provide the Interface can't provide the implementation
implementation of interface. of abstract class.
5) The abstract keyword is used to declare The interface keyword is used to declare
abstract class. interface.
6)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
End
Penamaan di Java
Name Convention
class name should start with uppercase letter and be a noun e.g.
String, Color, Button, System, Thread etc.
interface name should start with uppercase letter and be an adjective e.g.
Runnable, Remote, ActionListener etc.
method name should start with lowercase letter and be a verb e.g.
actionPerformed(), main(), print(), println() etc.
package name should be in lowercase letter e.g. java, lang, sql, util etc.
● import package.*;
● import package.classname;
● fully qualified name.
Cara mengakses package dari package lain
● import package.*;
● import package.classname;
● fully qualified name.
1) Using packagename.*
● If you use package.* then all the classes and interfaces of this
package will be accessible but not subpackages.
● The import keyword is used to make the classes and interface of
another package accessible to the current package.
Example of package that import the
packagename.*
//save by A.java
package pack;
public class A{
public void msg()
{System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
2) Using packagename.classname
● If you import package.classname then only declared class of this
package will be accessible.
Example of package by import
package.classname
//save by A.java
package pack;
public class A{
public void msg()
{System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.A;
class B{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
3) Using fully qualified name
● If you use fully qualified name then only declared class of this
package will be accessible. Now there is no need to import. But you
need to use fully qualified name every time when you are accessing
the class or interface.
● It is generally used when two packages have same class name e.g.
java.util and java.sql packages contain Date class.
Example of package by import fully qualified
name
//save by A.java
package pack;
public class A{
public void msg()
{System.out.println("Hello");}
}
//save by B.java
package mypack;
class B{
public static void main(String args[]){
pack.A obj = new pack.A();//
using fully qualified name
obj.msg();
}
}
If you import a package, subpackages
will not be imported.
● If you import a package, all the classes and interface of that package
will be imported excluding the classes and interfaces of the
subpackages. Hence, you need to import the subpackage as well.
Sequence of the program must be package
then import then class.
Subpackage in java
● Package inside the package is called the subpackage. It should be created to
categorize the package further.
● Let's take an example, Sun Microsystem has definded a package named java
that contains many classes like System, String, Reader, Writer, Socket etc.
These classes represent a particular group e.g. Reader and Writer classes are for
Input/Output operation, Socket and ServerSocket classes are for networking etc
and so on. So, Sun has subcategorized the java package into subpackages such
as lang, net, io etc. and put the Input/Output related classes in io package,
Server and ServerSocket classes in net packages and so on.
End