0% menganggap dokumen ini bermanfaat (0 suara)
20 tayangan5 halaman

Penggunaan Array dan String di Java

Dokumen ini membahas tentang inheritance, array, dan string dalam bahasa pemrograman Java. Dokumen ini memberikan contoh penggunaan inheritance, array satu dan dua dimensi, serta manipulasi string.

Diunggah oleh

eca eca
Hak Cipta
© Attribution Non-Commercial (BY-NC)
Kami menangani hak cipta konten dengan serius. Jika Anda merasa konten ini milik Anda, ajukan klaim di sini.
Format Tersedia
Unduh sebagai DOC, PDF, TXT atau baca online di Scribd
0% menganggap dokumen ini bermanfaat (0 suara)
20 tayangan5 halaman

Penggunaan Array dan String di Java

Dokumen ini membahas tentang inheritance, array, dan string dalam bahasa pemrograman Java. Dokumen ini memberikan contoh penggunaan inheritance, array satu dan dua dimensi, serta manipulasi string.

Diunggah oleh

eca eca
Hak Cipta
© Attribution Non-Commercial (BY-NC)
Kami menangani hak cipta konten dengan serius. Jika Anda merasa konten ini milik Anda, ajukan klaim di sini.
Format Tersedia
Unduh sebagai DOC, PDF, TXT atau baca online di Scribd

INHERITANCE, ARRAY DAN STRING

Obyektif :
1. Mengetahui dan memahami penurunan sifat dalam java
2. Mengetahui peggunaan array dan string
3. Dapat membuat program dengan menggunakan inheritance, array
dan string dalam java secara sederhana

ARRAY
Array merupakan suatu struktur data yang sangat penting dalam
suatu bahasa pemrograman . Suatu larik atau array merupakan suatu
kumpulan data yang memiliki tipe yang sama. Misal suatu array bertipe
string maka tidak boleh ada tipe lain didalamnya.

Ada 2 cara untuk mendeklarasikan array dalam bahasa java yaitu :


a. Dengan menggunakan operator New :
Bentuk Umum : tipe-array nama-array[ ] = new tipe-array[ ukuran
array ]
Contoh : int nama [] = new int[10]

b. Dengan memberikan nilai awal array dengan menggunakan “{“ dan


“}” Contoh :
String [ ] jurusan = {“ MI ”,” TI “,” TK “,” AKUNTANSI “ }

Untuk bentuk pertama ini array belum memiliki nilai awal sehingga
sebelum digunakan kita harus memberikan nilai awal kepada array
tersebut.

28
Contoh Program :

class larik {
String [] nama = {“JATI”,”SANTI”,”NISA”,”RINTO”};
String [] kelas = new String[[Link]];
Void cetakNama() {
Int I = 0;
[Link](nama[i] + ” “ + kelas[i];
i++;
[Link](nama[i] + ” “ + kelas[i];
i++;
[Link](nama[i] + ” “ + kelas[i];
i++;
[Link](nama[i] + ” “ + kelas[i];
}
public static void main (String[] args) {
larik a = new larik ();
[Link](“ “);
[Link](“------------------------“);
[Link]();
[Link](-------------------------“);
[Link][0] = “3IA03”;
[Link][1] = “4IA01”;
[Link][2] = “2IA01”;
[Link][0] = “3IA07”;
[Link]();
[Link](“--------------------“);
}
}

29
Untuk deklarasi array multi dimensi :
Type array nama array [] [] = new type array[ukuran-array]
[ukuran array]
Contoh program :
import [Link].*;
class Array2D
{
public static void main(String[]args) {
DataInputStream entry = new DataInputStream([Link]);
try {
int[][]angka = new int[3][3];
for(int i = 0;i<[Link];i++) {
for(int j = 0;j<angka[i].length;j++) {
[Link]("Matrik [ " + (i+1)+ "] [ " + (j+1) + " ] = ");
angka[i][j] = [Link]([Link]());
}
}
[Link]("Data array 2 Dimaensi : ");
for(int i = 0; i<[Link];i++) {
for(int j = 0;j<angka[i].length;j++) {
[Link](angka[i][j]+ " ");
}
[Link]();
}
}
catch(Exception e) {
[Link]("Wah salah input tuh ");
}
}
}

30
INHERITANCE (PENURUNAN SIFAT )
Inheritance adalah pewarisan atribut-atribut dan method pada
sebuah class yang diperoleh dari class yang telah terdefinisi tersebut.
Contoh Program :

// contoh inheritanace sederhana


// file disimpan dengan nama [Link]
class A {
int i;
int j;
void show_ij() {
[Link](“I dan j = “ + I + “” + j);
}
}
class B extends A {
int K ;
void show_k () {
[Link](“k = “ +k);
}
void sum_all() {
[Link](“I + j + k = “ + (i+j+k));
}
}
class penurunansederhana {
public static void main (String args[]) {
A objekBapak = new A();
B objekAnak = new B();
objekBapak.i = 13;
objekBapak.j = 17;
[Link](“Objek A -> objek superclass dari B : “)
objekBapak.show_ij();

31
objekAnak.i = 9;
objekAnak.j = 10;
objekAnak.k = 11;
[Link](“Objek A -> objek superclass dari B : “)
objekAnak.show_ij();
objekAnak.show_k();
objekAnak.sum_all();
}
}

STRING
Java string merupakan salah satu kelas dasar yang disediakan oleh java
untuk manipulasi karakter. Kelas string diguinakan untuk mendefinisikan
string yang constant(tidak bias berubah).
Contoh Program :

class panjang_string {
public static void main(String [] args) {
String s1 = “Perkenalan “;
String s2 = new String (“ Pertama “ );
Int pjg;
Pjg = [Link];
[Link](“panjang String s1 = \” +s1+”\”= “ + pjg );
Pjg = [Link]();
[Link](“panjang String s2 = \” +s2+”\”= “ + pjg );
}
}

32

Common questions

Didukung oleh AI

In Java, arrays can be declared in two primary ways. The first method uses the 'new' operator, allowing for the specification of the array's size without initializing its values. For example, 'int[] array = new int[10];' declares an integer array with a size of 10 but leaves it uninitialized . The second method involves directly initializing the array with values using curly brackets '{}'. For example, 'String[] subjects = {"Math", "Science", "English"};' declares and initializes a string array with specified initial values . The key difference is that the first method requires explicitly assigning values later, whereas the second method includes initialization at the time of declaration.

Two-dimensional arrays are preferred in Java applications when representing data in a matrix-like format, such as tabular data or grids, is necessary. They allow for organized storage and access of data that inherently revolves around two varying indices, like rows and columns. A two-dimensional array in Java is constructed by specifying the data type followed by brackets for both dimensions. For example, 'int[][] matrix = new int[3][3];' initializes a 3x3 integer matrix . This format is beneficial for applications dealing with spreadsheet data, game boards, or any composite structures that necessitate multi-indexing for data retrieval and manipulation.

When manipulating strings in Java, one must be mindful of a few key considerations and syntactical elements. Strings are immutable in Java, which means once a string is created, it cannot be changed. Every modification creates a new string object. This immutability affects how strings are managed in terms of performance and memory. Methods such as 'length()' are used to determine the number of characters in a string. For instance, using 's.length()' will return the length of string variable 's' . Additionally, string concatenation can be performed using the '+' operator, and methods from the String class, such as 'substring()', 'indexOf()', and 'toUpperCase()', allow for functional manipulations of string data without altering the original string object itself . These considerations are crucial for efficient string handling and ensuring intended data processing.

Constructors in Java play a crucial role in initializing new objects and setting up initial states. When dealing with inheritance, constructors help ensure that the superclass properties are properly initialized before the subclass adds its specific behaviors. In an inheritance hierarchy, the constructor of the superclass is called first, either automatically if there is no explicit call to 'super()' or explicitly using it. This process guarantees that a subclass object starts with a well-defined state, which includes the inherited attributes. For example, when a subclass 'B' extends a superclass 'A', the constructor of 'A' is executed first through the call 'super()' inside 'B's' constructor, thereby initializing 'A's' attributes before any specific initialization in 'B' occurs . This ensures stable behavior and consistency across object setups in subclass instances.

In Java, arrays are objects whose memory allocation is managed by the Java Virtual Machine (JVM), which also leverages garbage collection for memory management. When an array is declared with an initial capacity, memory is allocated accordingly. However, Java throws exceptions to handle common issues arising from improper array operations. 'ArrayIndexOutOfBoundsException' occurs when an attempt is made to access an array element with an illegal index, i.e., one that is negative or exceeds the array's length. Additionally, attempting to access a null-initialized array object results in a 'NullPointerException' . These exceptions ensure robust error handling, prompting developers to write preventive checks for bounds and initialization status to maintain safe and effective memory utilization.

Java's inheritance mechanism allows a new class, known as a subclass, to inherit fields and methods from an existing class, known as a superclass. This facilitates both code reuse and better organization by allowing common properties and behaviors to be shared across multiple classes, avoiding redundancy and enhancing maintainability. For instance, if class A contains common functionalities used by various other classes, those classes can extend class A (e.g., 'class B extends A'), thus inheriting its methods and fields . This is illustrated in the example where class A has methods show_ij() and class B, which extends A, can utilize these methods and also add its method show_k(), thereby enriching its functionality without duplicating code . This organizational strategy enables the inclusion of shared logic once and utilization across multiple components, thereby supporting efficient and organized code development.

Consider a program dealing with different types of vehicles, where each vehicle type shares common properties like make, model, and year but also has specific characteristics. One can define a superclass 'Vehicle' with common properties and methods such as 'start()', 'stop()', and 'displayInfo()'. Each specific vehicle type like 'Car', 'Truck', or 'Motorcycle' would extend 'Vehicle', inheriting its attributes and behaviors while introducing its specific features. For example, 'class Car extends Vehicle' could add methods specific to cars like 'openSunroof()'. In this way, classes like 'Car', 'Truck', and 'Motorcycle' avoid redundantly declaring those common fields and methods, thereby reducing code replication and enhancing maintainability . This pattern effectively demonstrates the avoidance of redundancy and facilitates centralized management of shared behaviors.

In Java, private fields in a superclass cannot be accessed directly by a subclass due to encapsulation. To allow subclasses to utilize these fields, the superclass can provide protected accessor (getter) and mutator (setter) methods. For instance, if a superclass 'A' has a private field 'int privateVar', it could provide a protected method 'getPrivateVar()' that returns the value of 'privateVar'. The subclass 'B' that extends 'A' can now safely call 'getPrivateVar()' to access the value without directly violating encapsulation principles . This approach keeps private data hidden from external access while enabling controlled interaction through subclass methods, preserving the encapsulation and data integrity.

The use of multi-dimensional arrays in Java provides structured data representation, particularly beneficial for handling complex datasets like matrices or tables. They allow efficient data organization and easy access to elements involving multiple indices, which suits applications like scientific computing, game boards, and image processing. However, challenges associated with multi-dimensional arrays include their increased complexity over single-dimensional arrays, such as higher memory usage and more intricate iteration logic. Further, manipulation can become error-prone, with index management being a frequent source of runtime errors . Proper understanding and careful implementation can mitigate these issues, but due diligence in handling their complexity is essential to leverage their advantages effectively without introducing bugs or logical flaws.

String immutability in Java ensures thread safety and consistent hashcode generation, but it impacts performance during repetitive modifications, as each change results in the creation of a new string object. This can lead to increased memory consumption and slower execution times in cases that involve numerous concatenations or alterations. To handle such situations efficiently, Java provides the 'StringBuilder' and 'StringBuffer' classes. These classes offer mutable alternatives, enabling modification operations like append and delete without creating new objects. 'StringBuilder' is preferable in a single-threaded context due to its non-synchronized nature, offering faster performance, while 'StringBuffer' can be used when thread safety is necessary due to its synchronized methods . Utilizing these classes can substantially improve runtime efficiency when dealing with extensive string manipulation.

Anda mungkin juga menyukai