0% found this document useful (0 votes)
10 views2 pages

Java to Python Conversion Guide

The document provides a comparison of Java and Python programming syntax across various topics, including variable declaration, conditional statements, nested conditions, loops, and arrays/lists. It illustrates how to convert Java code snippets into equivalent Python code. Each section includes examples for clarity and understanding of the differences between the two languages.

Uploaded by

haluya23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Java to Python Conversion Guide

The document provides a comparison of Java and Python programming syntax across various topics, including variable declaration, conditional statements, nested conditions, loops, and arrays/lists. It illustrates how to convert Java code snippets into equivalent Python code. Each section includes examples for clarity and understanding of the differences between the two languages.

Uploaded by

haluya23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Konversi Program Java ke Python - PBO II

1. Variabel dan Konstanta

Java:
int usia = 20;
final double PI = 3.14;
[Link]("Usia: " + usia);
[Link]("PI: " + PI);

Python:
usia = 20
PI = 3.14 # Konstanta
print("Usia:", usia)
print("PI:", PI)

2. Kondisi (IF, IF-Else, Nested IF)

Java:
int nilai = 85;
if (nilai >= 90) {
[Link]("A");
} else if (nilai >= 80) {
[Link]("B");
} else {
[Link]("C");
}

Python:
nilai = 85
if nilai >= 90:
print("A")
elif nilai >= 80:
print("B")
else:
print("C")

3. Nested IF

Java:
int nilai = 75;
if (nilai >= 70) {
if (nilai >= 90) {
[Link]("Nilai Sangat Baik");
} else {
[Link]("Nilai Cukup Baik");
}
} else {
[Link]("Nilai Kurang");
}

Python:
nilai = 75
if nilai >= 70:
if nilai >= 90:
print("Nilai Sangat Baik")
else:
print("Nilai Cukup Baik")
else:
print("Nilai Kurang")

4. Looping

Java:
for (int i = 0; i < 5; i++) {
[Link](i);
}

int j = 0;
while (j < 5) {
[Link](j);
j++;
}

Python:
for i in range(5):
print(i)

j = 0
while j < 5:
print(j)
j += 1

5. Array / List

Java:
int[] angka = {1, 2, 3, 4, 5};
for (int i = 0; i < [Link]; i++) {
[Link](angka[i]);
}

Python:
angka = [1, 2, 3, 4, 5]
for i in range(len(angka)):
print(angka[i])
# atau
for a in angka:
print(a)

Common questions

Powered by AI

Both Java and Python ensure that code within loops or conditions is executed through structural markers. Java uses braces '{}' to define code blocks, ensuring all enclosed statements execute together ('for (int i = 0; i < 5; i++) { System.out.println(i); }'). Python, however, relies on consistent indentation to define execution blocks ('for i in range(5): print(i)'). While Java provides explicit block definition through braces, which can prevent logical errors with complex nesting, Python's indentation offers more concise code but demands consistent visual alignment for accuracy .

Both Java and Python execute nested if statements in a similar hierarchical manner, evaluating from the outer condition to the innermost one. In Java, this requires braces to encapsulate each condition ('if (nilai >= 70) { if (nilai >= 90) { System.out.println("Nilai Sangat Baik"); } else { System.out.println("Nilai Cukup Baik"); }'). Python, using indentation, provides clearer visual hierarchy ('if nilai >= 70: if nilai >= 90: print("Nilai Sangat Baik") else: print("Nilai Cukup Baik")'). Python’s indentation contributes to greater readability and ease of maintenance, reducing the chance of mismatched braces .

Python’s dynamic typing allows for flexibility in variable declaration, where types are inferred at runtime ('usia = 20'). This can lead to runtime errors if unexpected types are used, necessitating careful management of variable assignments. Java’s static typing ('int usia = 20;') provides compile-time type checking, reducing chances of runtime type-related errors, but adds overhead as developers must explicitly declare types. Developers need to weigh the advantages of Java's robust type safety against Python's developer agility and speed of writing code .

Python simplifies the syntax of control flow statements, such as if-else conditions, by using indentation instead of braces to define blocks of code. In Java, conditions require curly braces to denotate the block ('if (nilai >= 90) { System.out.println("A"); }'), while Python uses indentation to signify block scope ('if nilai >= 90: print("A")'). Both languages evaluate conditions using similar logic, but Python offers a more concise and readable format .

In Java, variable declarations require specifying the data type and can include the use of the 'final' keyword for constants, such as 'int usia = 20;' or 'final double PI = 3.14;'. In Python, variables are dynamically typed so you can declare them without specifying the type ('usia = 20'). Constants in Python can be indicated by convention using uppercase letters ('PI = 3.14'). Additionally, Java requires the use of '+ operator' to concatenate strings ('System.out.println("Usia: " + usia);'), whereas Python uses ',' ('print("Usia:", usia)').

Java supports method overloading natively, allowing multiple methods with the same name but different parameter lists. This feature enhances flexibility in API design ('public int sum(int a, int b) {...}' vs. 'public double sum(double a, double b)'). Python does not natively support function overloading in the same way; instead, it relies on default arguments or *args and **kwargs to handle variable input scenarios while maintaining function simplicity ('def sum(a, b=0):'). Python's approach reduces the syntactical complexity found in Java but may require additional logic to handle varied inputs .

The 'range' function in Python streamlines loop declarations by eliminating the need for separate initialization and incrementing syntax found in Java. A Python loop such as 'for i in range(5):' provides a straightforward, concise approach that conveys the loop's range clearly. Java's equivalent requires more verbose setup ('for (int i = 0; i < 5; i++)'), which can obscure the loop's core purpose amid additional syntax. The internal handling by 'range' improves readability and minimizes errors in loop setup, although runtime efficiency is similar as both are interpreted to generate equivalent loops .

Java and Python both offer 'for' and 'while' loops, but their syntax varies significantly. In Java, a 'for' loop involves declaration and initialization ('for (int i = 0; i < 5; i++)'), whereas Python's 'for' loop simplifies this by using the 'range' function ('for i in range(5):'). Similarly, 'while' loops in Java use similar braces for code blocks ('while (j < 5) { System.out.println(j); j++; }'), whereas Python again uses indentation ('while j < 5: print(j); j += 1'). Python's syntax reduces boilerplate code, promoting readability .

Java uses arrays with fixed-size and type ('int[] angka = {1, 2, 3, 4, 5};'), requiring the use of loops or enhanced for-loops for iteration ('for (int i = 0; i < angka.length; i++)'). Python's equivalent, the list, is dynamically sized and typed ('angka = [1, 2, 3, 4, 5]'). Python allows straightforward iteration using 'for a in angka:', which directly accesses list elements, or indexed iteration using 'range(len(angka))'. Java's strict typing increases initial setup complexity compared to Python’s flexible lists .

Python lists are implemented as dynamic arrays that are more versatile than Java’s static arrays due to Python's ability to handle elements dynamically without a fixed size ('angka = [1, 2, 3, 4, 5]'). This flexibility supports operations like appending and removing elements easily. In contrast, Java arrays are fixed in size upon initialization ('int[] angka = {1, 2, 3, 4, 5};'), which limits flexibility and requires developers to manage array resizing through alternative data structures like ArrayLists. Python’s lists thus simplify handling dynamic data volumes and are more adaptable for general programming tasks .

You might also like