I compared the basic grammars of Java and Python. I would appreciate it if you could point out if the Java grammar is dirty or if there are any differences!
[1. Output](# 1 output) [2. Variable](# 2 variable) [3. Combining strings and numbers](# 3 Combining strings and numbers) [4.if statement](# 4if statement) [5.for statement](# 5for statement) [6.while statement](# 6while statement) [7. break, continue with for and while](break-continue with # 7 for and while) 8.list [9. Dictionary](# 9 dictionary)
python
print('123')
print(123)
>>>123
>>>123
java
public class Main {
public static void main(String[] args){
System.out.println("123");
System.out.println(123);
}
}
>>>123
>>>123
python
mystr = 'hello'
myint = 123
print(mystr)
print(myint)
>>>hello
>>>123
java
public class Main {
public static void main(String[] args){
String mystr = "hello";
int myint = 123;
System.out.println(mystr);
System.out.println(myint);
}
}
>>>hello
>>>123
python
myint = 123
print('The numbers are' + str(myint))
>>>The number is 123
java
public class Main {
public static void main(String[] args){
int myint = 123;
System.out.println("The numbers are" + myint);
}
}
>>>The number is 123
python
myint = 10
if myint == 10:
print('myint is 10')
>>>myint is 10
java
public class Main {
public static void main(String[] args){
int myint = 10;
if(myint == 10){
System.out.println("myint is 10");
}
}
}
>>>myint is 10
python
for char in 'Hello':
print(char)
>>>H
>>>e
>>>l
>>>l
>>>o
java
//split and extended for statement
public class Main {
public static void main(String[] args){
String mystr = "hello";
String[] strArray = mystr.split("");
for(String s : strArray) {
System.out.println(s);
}
}
}
//charAt and for statement
public class Main {
public static void main(String[] args){
String mystr = "hello";
for(int i = 0; i < mystr.length(); i++) {
System.out.println(mystr.charAt(i));
}
}
}
>>>h
>>>e
>>>l
>>>l
>>>o
python
for i in range(3):
print(i)
>>>0
>>>1
>>>2
java
public class Main {
public static void main(String[] args){
for(int i = 0; i < 3; i++) {
System.out.println(i);
}
}
}
>>>0
>>>1
>>>2
python
list = ['item1', 'item2', 'item3']
for item in list:
print(item)
>>>item1
>>>item2
>>>item3
java
public class Main {
public static void main(String[] args){
String[] list = {"item1", "item2", "item3"};
for(int i = 0; i <list.length; i++){
System.out.println(list[i]);
}
}
}
>>>item1
>>>item2
>>>item3
python
val = 0
while val < 4:
val += 1
print(val)
print('val is' + str(val))
>>>1
>>>2
>>>3
>>>4
>>>val is 4
java
public class Main {
public static void main(String[] args){
int val = 0;
while(val < 4){
val++;
System.out.println(val);
}
System.out.println("val is" + val);
}
}
>>>1
>>>2
>>>3
>>>4
>>>val is 4
python
list = [1,2,3,4,5]
for i in list:
if i == 3:
break
print(i)
>>>1
>>>2
>>>Repeat processing ends with break
java
public class Main {
public static void main(String[] args){
int[] list = {1,2,3,4,5};
for(int i = 1; i < list.length; i++){
if(i == 3){
break;
}
System.out.println(i);
}
}
}
>>>1
>>>2
>>>Repeat processing ends with break
python
val = 1
while val < 10:
if val == 3:
break
print(val)
val += 1
>>>1
>>>2
>>>Repeat processing ends with break
java
public class Main {
public static void main(String[] args){
int val = 1;
while(val < 10){
if(val == 3){
break;
}
System.out.println(val);
val++;
}
}
}
>>>1
>>>2
>>>Repeat processing ends with break
python
#continue with a for statement
list = [1,2,3,4,5]
for i in list:
if i == 2:
continue
print(i)
>>>1
>>>3
>>>4
>>>5
java
public class Main {
public static void main(String[] args){
int[] list = {1,2,3,4,5};
for(int i = 1; i < list.length+1; i++){
if(i == 2){
continue;
}
System.out.println(i);
}
}
}
>>>1
>>>3
>>>4
>>>5
python
val = 0
while val < 5:
val += 1
if val == 2:
continue
print(val)
>>>1
>>>3
>>>4
>>>5
java
public class Main {
public static void main(String[] args){
int val = 1;
while(val < 6){
if(val == 2){
val++;
continue;
}
System.out.println(val);
val++;
}
}
}
>>>1
>>>3
>>>4
>>>5
8.list# In python, lists play the role of arrays.
python
list = ['a','b','c']
print(list)
>>>['a','b','c']
In Java, you can't get all the contents of the list at once, so you need to call them one by one using for.
java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args){
List<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
for(int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
>>>a
>>>b
>>>c
python
list = ['a','b','c']
#First element
print(list[0])
#Second element
print(list[2])
>>>a
>>>c
java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args){
List<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
//First element
System.out.println(list.get(0));
//Second element
System.out.println(list.get(2));
}
}
>>>a
>>>c
python
list = ['a','b','c']
#Add to the end
list.append('d')
#Add to specified position
list.insert(2, 'd')
print(list)
>>>['a','b','d','c','d']
java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args){
List<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
//Add to the end
list.add("d");
//Add to specified position
list.add(2, "d");
}
}
>>>{"a","b","d","c","d"}
python
list = ['a','b','c']
#Remove trailing element
list.pop()
#Delete the element at the specified position
list.pop(1)
print(list)
>>>['a']
java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args){
List<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
//Delete the element at the specified position
list.remove(0);
}
}
>>>{"b","c"}
python
list = ['a','b','c']
list[1] = 'x'
print(list)
>>>['a','x','c']
java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args){
List<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
list.set(1,"x");
}
}
>>>{"a","x","c"}
python
dict = { 'apple':1, 'orange':2, 'banana':3 }
print(dict['apple'])
>>>1
java
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args){
LinkedHashMap<String, Integer> map = new LinkedHashMap<>() ;
map.put("apple", 1);
map.put("orange", 2);
map.put("banana", 3);
System.out.println(map);
System.out.println(map.get("apple"));
}
}
>>>{apple=1, orange=2, banana=3}
>>>1
I used LinkedHashMap this time because I wanted to keep the order.
** Map types and features ** LinkedHashMap: Hold in the order in which the keys are stored. TreeMap: Hold in ascending order of keys. HashMap: Do not keep order.
python
dict = { 'apple':1, 'orange':2, 'banana':3 }
dict['apple'] = 5
print(dict)
>>>{ 'apple':5, 'orange':2, 'banana':3 }
java
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args){
LinkedHashMap<String, Integer> map = new LinkedHashMap<>() ;
map.put("apple", 1);
map.put("orange", 2);
map.put("banana", 3);
map.put("apple",5);
System.out.println(map);
}
}
>>>{apple=5, orange=2, banana=3}
python
dict = { 'apple':1, 'orange':2, 'banana':3 }
del dict['apple']
print(dict)
>>>{'orange':2, 'banana':3 }
java
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args){
LinkedHashMap<String, Integer> map = new LinkedHashMap<>() ;
map.put("apple", 1);
map.put("orange", 2);
map.put("banana", 3);
map.remove("apple");
System.out.println(map);
}
}
>>>{orange=2, banana=3}
python
dict = { 'apple':1, 'orange':2, 'banana':3 }
if 'apple' in dict:
print('Exists')
else:
print('do not exist')
>>>Exists
java
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args){
LinkedHashMap<String, Integer> map = new LinkedHashMap<>() ;
map.put("apple", 1);
map.put("orange", 2);
map.put("banana", 3);
if(map.containsKey("apple")){
System.out.println("Exists");
}else{
System.out.println("do not exist");
}
}
}
>>>Exists
Recommended Posts