Table of Contents
Array
- Java arrays are of fixed size, and adding an element requires creating a new array or using a
List
. - Go uses slices for dynamic arrays, and arrays are fixed in size like Java.
- Python lists are dynamic and act like arrays but have more flexible operations.
Integer Array Operations Comparison
Operation | Java | Go (Golang) | Python 3 |
---|---|---|---|
Array Declaration | int[] arr; |
var arr [size]int |
arr = [] |
Array Initialization with Length | int[] arr = new int[5]; |
var arr = make([]int, 5) |
arr = [None]*5 |
Array Initialization without Length | int[] arr = {1, 2, 3}; |
arr := []int{1, 2, 3} |
arr = [1, 2, 3] |
Get Length of Array | arr.length |
len(arr) |
len(arr) |
Add New Element at End | Not applicable directly (arrays are fixed size) | arr = append(arr, 4) |
arr.append(4) |
Add New Element at Certain Position/Index | Not applicable directly (requires a list or manual shifting of elements) | No direct method (requires slices or manual shifting of elements) | arr.insert(index, value) |
Change the Value at Certain Position/Index | arr[index] = newValue; |
arr[index] = newValue |
arr[index] = newValue |
Access Element at Certain Position/Index | arr[index] |
arr[index] |
arr[index] |
Other Common Array Operations | Arrays.sort(arr) |
sort.Ints(arr) |
arr.sort() |
String
String Operations Comparison
Operation | Java | Go (Golang) | Python 3 |
---|---|---|---|
String Declaration | String inputString; |
var inputString string |
inputString := "" |
String Initialization with Value | String inputString = "hello"; |
inputString := "hello" |
inputString = "hello" |
String Length | inputString.length() |
len(inputString) |
len(inputString) |
Concatenate Strings | String result = inputString + " world"; |
result := inputString + " world" |
result = inputString + " world" |
Substring Extraction | String sub = inputString.substring(start, end); |
sub := inputString[start:end] |
sub = inputString[start:end] |
Replace Substring | String newString = inputString.replace("old", "new"); |
newString := strings.Replace(inputString, "old", "new", -1) |
newString = inputString.replace("old", "new") |
Convert to Uppercase | String upper = inputString.toUpperCase(); |
upper := strings.ToUpper(inputString) |
upper = inputString.upper() |
Convert to Lowercase | String lower = inputString.toLowerCase(); |
lower := strings.ToLower(inputString) |
lower = inputString.lower() |
Check if String Contains Substring | boolean contains = inputString.contains("sub"); |
contains := strings.Contains(inputString, "sub") |
contains = "sub" in inputString |
Split String by Delimiter | String[] parts = inputString.split(","); |
parts := strings.Split(inputString, ",") |
parts = inputString.split(",") |
Trim Whitespace | String trimmed = inputString.trim(); |
trimmed := strings.TrimSpace(inputString) |
trimmed = inputString.strip() |
String vs Char Array Comparison
Key Differences in JAVA, GOLANG, Python3
- Declaration and Initialization:
- Java:
String
is an immutable class andchar[]
is a mutable array. - Go:
string
is immutable and[]rune
is mutable. Note that Go usesrune
for characters which are essentially Unicode code points. - Python:
str
is immutable, and lists of characters (likearr
) are mutable.
- Java:
- Mutability:
- Java:
String
objects cannot be modified after creation. Any modification creates a newString
.char[]
can be changed directly. - Go:
string
cannot be modified after creation, but[]rune
can be changed directly. - Python: Strings (
str
) are immutable, but lists of characters can be modified directly.
- Java:
- Conversion Between Types:
- Java: Conversion between
String
andchar[]
requires explicit methods or constructors. - Go: Conversion between
string
and[]rune
is straightforward with type conversion. - Python: Conversion between
str
and a list of characters involveslist()
and''.join()
.
- Java: Conversion between
- Operations:
- Operations like concatenation, substring extraction, and character replacement are straightforward with strings but may require additional steps or methods when using character arrays or rune slices.
Aspect | Java (String vs Char Array) | Go (Golang) (String vs Rune Slice) | Python 3 (String vs List of Characters) |
---|---|---|---|
Declaration | String s; vs char[] arr; |
var s string vs var arr []rune |
s = "" vs arr = [] |
Initialization | String s = "hello"; vs char[] arr = {'h', 'e', 'l', 'l', 'o'}; |
s := "hello" vs arr := []rune{'h', 'e', 'l', 'l', 'o'} |
s = "hello" vs arr = list("hello") |
Mutability | Immutable (cannot change individual characters) | Immutable (strings are immutable, but rune slices are mutable) | Immutable (strings are immutable, but lists are mutable) |
Access | s.charAt(index) vs arr[index] |
s[index] vs arr[index] |
s[index] vs arr[index] |
Length | s.length() vs arr.length |
len(s) vs len(arr) |
len(s) vs len(arr) |
Concatenation | s.concat(" world") vs Manual (e.g., using StringBuilder ) |
s + " world" vs string(arr) + " world" |
s + " world" vs ''.join(arr) + " world" |
Substring Extraction | s.substring(start, end) vs Manual (e.g., using StringBuilder ) |
s[start:end] vs arr[start:end] |
s[start:end] vs arr[start:end] |
Replace Substring | s.replace("old", "new") vs Manual manipulation |
strings.Replace(s, "old", "new", -1) vs Manual manipulation |
s.replace("old", "new") vs Manual manipulation |
Convert to Uppercase | s.toUpperCase() vs Manual (e.g., using Character.toUpperCase ) |
strings.ToUpper(s) vs Manual (e.g., using []rune and unicode.ToUpper ) |
s.upper() vs Manual (e.g., arr = [c.upper() for c in arr] ) |
Convert to Lowercase | s.toLowerCase() vs Manual (e.g., using Character.toLowerCase ) |
strings.ToLower(s) vs Manual (e.g., using []rune and unicode.ToLower ) |
s.lower() vs Manual (e.g., arr = [c.lower() for c in arr] ) |
Check for Substring | s.contains("sub") vs Manual scanning |
strings.Contains(s, "sub") vs Manual scanning |
"sub" in s vs Manual scanning |
Split String | s.split(",") vs Manual manipulation |
strings.Split(s, ",") vs Manual manipulation |
s.split(",") vs Manual manipulation |
Trim Whitespace | s.trim() vs Manual manipulation |
strings.TrimSpace(s) vs Manual manipulation |
s.strip() vs Manual manipulation |
Leave a Reply